Hugo Guarin
commited on
Commit
·
56bb508
1
Parent(s):
a74c44e
First commit on huggingface
Browse files- app.py +56 -0
- requirements.txt +3 -0
- test_images/image1.jpg +0 -0
- test_images/image2.jpg +0 -0
- test_images/image3.jpg +0 -0
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import numpy as np
|
3 |
+
import tensorflow as tf
|
4 |
+
from tensorflow import keras
|
5 |
+
from tensorflow.keras.preprocessing import image
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
# Paintings' authors considered for this version
|
9 |
+
artists = ['botero',
|
10 |
+
'davinci',
|
11 |
+
'elgreco',
|
12 |
+
'melzi',
|
13 |
+
'michelangelo',
|
14 |
+
'modigliani',
|
15 |
+
'picasso',
|
16 |
+
'rembrandt',
|
17 |
+
'rubens',
|
18 |
+
'vermeer']
|
19 |
+
|
20 |
+
model = keras.models.load_model('models/model_2023-08-29T1856_ep40_bz32_img224_nc10.h5')
|
21 |
+
|
22 |
+
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]'
|
23 |
+
|
24 |
+
def predicting_author(image):
|
25 |
+
|
26 |
+
try:
|
27 |
+
if input is None:
|
28 |
+
return 'Please upload an image'
|
29 |
+
|
30 |
+
x = image.img_to_array(input)
|
31 |
+
x = np.expand_dims(x, axis=0)
|
32 |
+
x = x.astype('float32') / 255.0
|
33 |
+
|
34 |
+
prediction = model.predict(x)
|
35 |
+
class_probabilities = prediction[0]
|
36 |
+
results = {artists[i]: float(class_probabilities[i]) for i in range(len(artists))}
|
37 |
+
|
38 |
+
return results
|
39 |
+
|
40 |
+
except Exception as e:
|
41 |
+
print("An error occurred:", e)
|
42 |
+
# Print traceback to see more details
|
43 |
+
import traceback
|
44 |
+
traceback.print_exc()
|
45 |
+
return "An error occurred"
|
46 |
+
|
47 |
+
demo = gr.Interface(
|
48 |
+
title='Predicting paintings authors',
|
49 |
+
description=description,
|
50 |
+
fn=predicting_author,
|
51 |
+
inputs=gr.Image(shape=(224, 224)),
|
52 |
+
outputs=gr.Label(num_top_classes=3),
|
53 |
+
examples=['./test_images/image1.jpg', './test_images/image2.jpg', './test_images/image3.jpg']
|
54 |
+
)
|
55 |
+
|
56 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
tensorflow==2.10.0
|
2 |
+
keras==2.10.0
|
3 |
+
gradio==3.41.2
|
test_images/image1.jpg
ADDED
test_images/image2.jpg
ADDED
test_images/image3.jpg
ADDED