File size: 1,791 Bytes
56bb508
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing import image
import gradio as gr

# Paintings' authors considered for this version 
artists = ['botero', 
          'davinci', 
          'elgreco', 
          'melzi', 
          'michelangelo',
          'modigliani', 
          'picasso', 
          'rembrandt', 
          'rubens', 
          'vermeer']

model = keras.models.load_model('models/model_2023-08-29T1856_ep40_bz32_img224_nc10.h5')

description = 'Welcome!!! This app was built based on Gradio. The aim of this App is to predict the author of a painting. In this first version of the App, we only considered 10 authors [botero, davinci, elgreco, melzi, michelangelo,modigliani, picasso, rembrandt, rubens, vermeer]'

def predicting_author(image):

    try:
        if input is None:
            return 'Please upload an image'
        
        x = image.img_to_array(input)
        x = np.expand_dims(x, axis=0)
        x = x.astype('float32') / 255.0
        
        prediction = model.predict(x)
        class_probabilities = prediction[0] 
        results = {artists[i]: float(class_probabilities[i]) for i in range(len(artists))}
        
        return results
    
    except Exception as e:
        print("An error occurred:", e)
        # Print traceback to see more details
        import traceback
        traceback.print_exc()
        return "An error occurred"

demo = gr.Interface(
    title='Predicting paintings authors',
    description=description,
    fn=predicting_author, 
    inputs=gr.Image(shape=(224, 224)), 
    outputs=gr.Label(num_top_classes=3),
    examples=['./test_images/image1.jpg', './test_images/image2.jpg', './test_images/image3.jpg']
    )
             
demo.launch()