Files changed (1) hide show
  1. app.py +47 -51
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
- 'house',
9
- 'wine bottle',
10
- 'chair',
11
- 'table',
12
- 'tree',
13
- 'camera',
14
- 'fish',
15
- 'rain',
16
- 'clock',
17
- 'hat'
18
  ]
19
 
20
- # labels :
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
- # load the model:
38
- from keras.models import load_model
39
- model = load_model('sketch_recogination_model_cnn.h5')
40
 
41
- # Predict function for interface:
42
  def predict_fn(image):
43
-
44
- # preprocessing the size:
45
- resized_image = tf.image.resize(image, (28, 28)) # Resize image to (28, 28)
46
- grayscale_image = tf.image.rgb_to_grayscale(resized_image) # Convert image to grayscale
47
-
48
- image = np.array(grayscale_image)
49
-
50
- # model requirements:
51
- image = image.reshape(1,28,28,1)
52
- label = tf.constant(model.predict(image).reshape(num_classes)) # giving 2D output so 1D
53
-
54
- # predict:
55
- predicted_index = tf.argmax(label)
56
- class_name = [name for name, index in labels.items() if predicted_index == index][0]
57
- return class_name
58
-
59
-
60
- # application interface:
61
- import gradio as gr
62
- gr.Interface(fn=predict_fn, inputs="paint", outputs="label", title="DoodleDecoder", description="Draw something from: Car, House, Wine bottle, Chair, Table, Tree, Camera, Fish, Rain, Clock, Hat", interpretation='default', article="Draw large with thick stroke.").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()