Spaces:
Sleeping
Sleeping
Commit
Β·
8f10b45
1
Parent(s):
ba88bbd
commit
Browse files- app.py +57 -0
- examples/butterfly.png +0 -0
- examples/car.png +0 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from huggingface_hub import hf_hub_download
|
5 |
+
import tensorflow as tf
|
6 |
+
|
7 |
+
# Load model
|
8 |
+
model = tf.keras.models.load_model(
|
9 |
+
hf_hub_download("nharshavardhana/quickdraw_classifier", "quickdraw_classifier.keras")
|
10 |
+
)
|
11 |
+
|
12 |
+
# Class names (replace with your 50 classes)
|
13 |
+
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
|
14 |
+
|
15 |
+
def predict_uploaded_image(img):
|
16 |
+
# Preprocess image
|
17 |
+
img = img.astype("float32") / 255.0
|
18 |
+
img = 1.0 - img # Invert colors (if needed)
|
19 |
+
img = cv2.resize(img, (28, 28))
|
20 |
+
img = np.expand_dims(img, axis=(0, -1))
|
21 |
+
|
22 |
+
# Predict
|
23 |
+
preds = model.predict(img)[0]
|
24 |
+
top5 = np.argsort(preds)[::-1][:5]
|
25 |
+
return {class_names[i]: float(preds[i]) for i in top5}
|
26 |
+
|
27 |
+
# Create a detailed UI with Blocks
|
28 |
+
with gr.Blocks(title="DoodleSense") as demo:
|
29 |
+
gr.Markdown("# π¨ DoodleSense")
|
30 |
+
gr.Markdown("""
|
31 |
+
**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!
|
32 |
+
This model is trained on the [QuickDraw Dataset](https://quickdraw.withgoogle.com/data) for 30 classes.
|
33 |
+
""")
|
34 |
+
|
35 |
+
with gr.Row():
|
36 |
+
with gr.Column():
|
37 |
+
input_image = gr.Image(
|
38 |
+
image_mode="L",
|
39 |
+
)
|
40 |
+
gr.Examples(
|
41 |
+
examples=["examples/butterfly.png", "examples/car.png"], # Add your example images
|
42 |
+
inputs=input_image,
|
43 |
+
label="Try these examples:"
|
44 |
+
)
|
45 |
+
with gr.Column():
|
46 |
+
output_label = gr.Label(num_top_classes=5, label="Top 5 Predictions")
|
47 |
+
|
48 |
+
gr.Markdown("""
|
49 |
+
## π About This Project
|
50 |
+
- **Model**: Trained using TensorFlow/Keras on 30 QuickDraw classes.
|
51 |
+
- **Input**: 28x28 grayscale sketches (black strokes on white background).
|
52 |
+
- **Training Data**: 50,000 samples per class from the QuickDraw dataset.
|
53 |
+
""")
|
54 |
+
|
55 |
+
input_image.change(predict_uploaded_image, inputs=input_image, outputs=output_label)
|
56 |
+
|
57 |
+
demo.launch(share=True)
|
examples/butterfly.png
ADDED
![]() |
examples/car.png
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio>=4.0.0
|
2 |
+
tensorflow
|
3 |
+
opencv-python-headless
|
4 |
+
huggingface_hub
|
5 |
+
numpy
|