Spaces:
Sleeping
Sleeping
jivaniyash
commited on
Commit
•
f9db8ce
1
Parent(s):
97d2907
Add Files
Browse files- app.py +60 -0
- requirements.txt +1 -0
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from tensorflow import keras
|
3 |
+
from keras import models
|
4 |
+
from keras.preprocessing import image
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
title = "Image Classifier"
|
9 |
+
|
10 |
+
description = '''This Project is used to predict gender - male/female & hand-written digit in an image. There are 2 models -
|
11 |
+
- Gender Model has 6-layered architecture. Model is trained using 4-CNN, 2-Dense-Fully connected layer Dataset - https://www.kaggle.com/datasets/gpiosenka/gender-classification-from-an-image. Notebook link - https://github.com/jivaniyash/image_classifier_app/blob/main/colab-notebook/Gender_Classifier.ipynb
|
12 |
+
- Digit Classifier Model has 3-layered architecture - trained using dataset - https://www.tensorflow.org/datasets/catalog/mnist. Notebook Link - https://github.com/jivaniyash/image_classifier_app/blob/main/colab-notebook/Digit_Classifier.ipynb
|
13 |
+
'''
|
14 |
+
|
15 |
+
article="<p style='text-align: center'><a href='https://github.com/jivaniyash/image_classifier_app' target='_blank'>Link to Git Repository</a <p> There are 2 different classification tasks merged over single endpoint function. This practice should not be adpoted in real use -case scenarios. Try creating different endpoint for each classification. This project is just for learning purposes.</p>"
|
16 |
+
|
17 |
+
|
18 |
+
def image_classifier(model_name, img):
|
19 |
+
|
20 |
+
if model_name == "Gender Classifier":
|
21 |
+
img_np = image.img_to_array(image.load_img(img, target_size=(64,64)))
|
22 |
+
test_img = img_np.reshape((1, 64, 64, 3))
|
23 |
+
|
24 |
+
pipeline = models.load_model('./models/gender-classifier.keras')
|
25 |
+
|
26 |
+
y = pipeline(test_img/255., training=False) # output prob between 0 to 1 , 0 indicates female & 1 indicates male
|
27 |
+
prob = y.numpy()[0][0]
|
28 |
+
predictions = [prob,1-prob]
|
29 |
+
|
30 |
+
labels = ["Male", "Female"]
|
31 |
+
|
32 |
+
return {labels[i]:float(predictions[i]) for i in range(len(labels))}
|
33 |
+
|
34 |
+
|
35 |
+
if model_name == "Digit Classifier":
|
36 |
+
img_np = image.img_to_array(image.load_img(img, target_size=(28,28)).convert('L'))
|
37 |
+
test_img = img_np.reshape((1,28,28,1))
|
38 |
+
|
39 |
+
pipeline = models.load_model('./models/digit-classifier.keras')
|
40 |
+
|
41 |
+
y = pipeline(test_img/255., training=False) # output list of 10 tensors
|
42 |
+
|
43 |
+
labels = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
|
44 |
+
|
45 |
+
return {labels[i]:float(y[0][i]) for i in range(len(labels))}
|
46 |
+
|
47 |
+
|
48 |
+
demo = gr.Interface(fn=image_classifier,
|
49 |
+
inputs=[gr.Dropdown(["Gender Classifier","Digit Classifier"], label="Select Model to predict", info="models"),
|
50 |
+
gr.Image(type='filepath')],
|
51 |
+
outputs=gr.Label(num_top_classes=2),
|
52 |
+
title=title,
|
53 |
+
description=description,
|
54 |
+
article=article,
|
55 |
+
examples=[["Gender Classifier","./images/gender/male01.jpg"],
|
56 |
+
["Gender Classifier","./images/gender/female01.jpg"],
|
57 |
+
["Digit Classifier", "./images/digit/1.png"],
|
58 |
+
["Digit Classifier", "./images/digit/3.png"]])
|
59 |
+
|
60 |
+
demo.launch(share=True, debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
tensorflow
|