Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,58 @@
|
|
1 |
import pandas as pd
|
2 |
import numpy as np
|
3 |
import tensorflow as tf
|
|
|
|
|
4 |
|
5 |
-
# classes
|
6 |
classes = [
|
7 |
-
'car',
|
8 |
-
'
|
9 |
-
'
|
10 |
-
'chair',
|
11 |
-
'table',
|
12 |
-
'tree',
|
13 |
-
'camera',
|
14 |
-
'fish',
|
15 |
-
'rain',
|
16 |
-
'clock',
|
17 |
-
'hat'
|
18 |
]
|
19 |
|
20 |
-
|
21 |
-
labels = {
|
22 |
-
'car': 0,
|
23 |
-
'house': 1,
|
24 |
-
'wine bottle': 2,
|
25 |
-
'chair': 3,
|
26 |
-
'table': 4,
|
27 |
-
'tree': 5,
|
28 |
-
'camera': 6,
|
29 |
-
'fish': 7,
|
30 |
-
'rain': 8,
|
31 |
-
'clock': 9,
|
32 |
-
'hat': 10
|
33 |
-
}
|
34 |
-
|
35 |
num_classes = len(classes)
|
36 |
|
37 |
-
#
|
38 |
-
|
39 |
-
model = load_model('sketch_recogination_model_cnn.h5')
|
40 |
|
41 |
-
# Predict function for interface
|
42 |
def predict_fn(image):
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
#
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
#
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import pandas as pd
|
2 |
import numpy as np
|
3 |
import tensorflow as tf
|
4 |
+
from keras.models import load_model
|
5 |
+
import gradio as gr
|
6 |
|
7 |
+
# Extended classes and labels
|
8 |
classes = [
|
9 |
+
'car', 'house', 'wine bottle', 'chair', 'table',
|
10 |
+
'tree', 'camera', 'fish', 'rain', 'clock', 'hat',
|
11 |
+
'dog', 'cat', 'bicycle', 'plane', 'book', 'computer'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
]
|
13 |
|
14 |
+
labels = {name: index for index, name in enumerate(classes)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
num_classes = len(classes)
|
16 |
|
17 |
+
# Load the model
|
18 |
+
model = load_model('sketch_recognition_model_cnn.h5')
|
|
|
19 |
|
20 |
+
# Predict function for interface
|
21 |
def predict_fn(image):
|
22 |
+
"""
|
23 |
+
Predict the class of a drawn image.
|
24 |
+
|
25 |
+
Args:
|
26 |
+
image: The input image drawn by the user.
|
27 |
+
|
28 |
+
Returns:
|
29 |
+
The predicted class name.
|
30 |
+
"""
|
31 |
+
try:
|
32 |
+
# Preprocessing the image
|
33 |
+
resized_image = tf.image.resize(image, (28, 28)) # Resize image to (28, 28)
|
34 |
+
grayscale_image = tf.image.rgb_to_grayscale(resized_image) # Convert image to grayscale
|
35 |
+
image_array = np.array(grayscale_image) / 255.0 # Normalize the image
|
36 |
+
|
37 |
+
# Prepare image for model input
|
38 |
+
image_array = image_array.reshape(1, 28, 28, 1) # Add batch dimension
|
39 |
+
predictions = model.predict(image_array).reshape(num_classes) # 2D output to 1D
|
40 |
+
|
41 |
+
# Predict the class index
|
42 |
+
predicted_index = tf.argmax(predictions).numpy() # Get the index of the highest score
|
43 |
+
class_name = classes[predicted_index] # Retrieve the class name
|
44 |
+
|
45 |
+
return class_name
|
46 |
+
except Exception as e:
|
47 |
+
return f"Error in prediction: {str(e)}"
|
48 |
+
|
49 |
+
# Gradio application interface
|
50 |
+
gr.Interface(
|
51 |
+
fn=predict_fn,
|
52 |
+
inputs="paint",
|
53 |
+
outputs="label",
|
54 |
+
title="DoodleDecoder",
|
55 |
+
description="Draw something from: Car, House, Wine bottle, Chair, Table, Tree, Camera, Fish, Rain, Clock, Hat, Dog, Cat, Bicycle, Plane, Book, Computer",
|
56 |
+
interpretation='default',
|
57 |
+
article="Draw large with thick stroke."
|
58 |
+
).launch()
|