File size: 2,596 Bytes
8f10b45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bcef02
 
 
 
8f10b45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dbc407b
8f10b45
 
 
 
 
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
import gradio as gr
import cv2
import numpy as np
from huggingface_hub import hf_hub_download
import tensorflow as tf

# Load model
model = tf.keras.models.load_model(
    hf_hub_download("nharshavardhana/quickdraw_classifier", "quickdraw_classifier.keras")
)

# Class names (replace with your 50 classes)
class_names = ['anvil','banana','bowtie','butterfly','cake','carrot','cat','clock','mushroom','cup','door', 'dog','eye','fish','hexagon','moon','ice cream','pizza','umbrella','circle','star','triangle','apple', 'car', 'house', 'tree', 'cloud', 'face', 'flower', 'bird']  # Add all 50 labels

def predict_uploaded_image(img):
    # Preprocess image
    img = img.astype("float32") / 255.0
    img = 1.0 - img  # Invert colors (if needed)
    img = cv2.resize(img, (28, 28))
    img = np.expand_dims(img, axis=(0, -1))
    
    # Predict
    preds = model.predict(img)[0]
    top5 = np.argsort(preds)[::-1][:5]
    return {class_names[i]: float(preds[i]) for i in top5}

# Create a detailed UI with Blocks
with gr.Blocks(title="DoodleSense") as demo:
    gr.Markdown("# 🎨 DoodleSense")
    gr.Markdown("""
    **Draw a sketch in paint application with brush(black) of 30 px(pixels) against white background and upload the saved image** to see the top 5 predictions!  
    Try to sketch and upload any of these : 'anvil','banana','bowtie','butterfly','cake','carrot','cat','clock','mushroom','cup','door', 'dog','eye','fish','hexagon','moon','ice cream','pizza','umbrella','circle','star','triangle','apple', 'car', 'house', 'tree', 'cloud', 'face', 'flower', 'bird'.
    """)
    gr.Markdown("""
    Currently this model is trained on the [QuickDraw Dataset](https://quickdraw.withgoogle.com/data) for 30 classes.
    """)
    
    with gr.Row():
        with gr.Column():
            input_image = gr.Image(
                image_mode="L",
            )
            gr.Examples(
                examples=["examples/butterfly.png", "examples/car.png"],  # Add your example images
                inputs=input_image,
                label="Try these examples:"
            )
        with gr.Column():
            output_label = gr.Label(num_top_classes=5, label="Top 5 Predictions")
    
    gr.Markdown("""
    ## πŸ“– About This Project
    - **Model**: Trained using TensorFlow/Keras on 30 QuickDraw classes.
    - **Input**: 28x28 grayscale sketches (black strokes on white background).
    - **Training Data**: 5000 samples per class from the QuickDraw dataset.
    """)

    input_image.change(predict_uploaded_image, inputs=input_image, outputs=output_label)

demo.launch(share=True)