File size: 1,419 Bytes
8ea1187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba93c8f
 
e0e635c
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
57
58
59
60
61
62
63
import pandas as pd
import numpy as np
import tensorflow as tf

# classes:
classes = [
'car',
'house',
'wine bottle',
'chair',
'table',
'tree',
'camera',
'fish',
'rain',
'clock',
'hat'
]

# labels :
labels = {
'car': 0,
'house': 1,
'wine bottle': 2,
'chair': 3,
'table': 4,
'tree': 5,
'camera': 6,
'fish': 7,
'rain': 8,
'clock': 9,
'hat': 10
}

num_classes = len(classes)

# load the model:
from keras.models import load_model
model = load_model('sketch_recogination_model_cnn.h5')

# Predict function for interface:
def predict_fn(image):

  # preprocessing the size:
  resized_image = tf.image.resize(image, (28, 28))              # Resize image to (28, 28)
  grayscale_image = tf.image.rgb_to_grayscale(resized_image)    # Convert image to grayscale

  image = np.array(grayscale_image)

  # model requirements:
  image = image.reshape(1,28,28,1)
  label = tf.constant(model.predict(image).reshape(num_classes))   # giving 2D output so 1D

  # predict:
  predicted_index = tf.argmax(label)
  class_name = [name for name, index in labels.items() if predicted_index == index][0]
  return class_name


# application interface:
import gradio as gr
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()