Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,38 +4,43 @@ from tensorflow.keras.preprocessing import image
|
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
import numpy as np
|
| 6 |
import os
|
|
|
|
| 7 |
|
| 8 |
MODEL_REPO = "zotthytt12/vegetable-classifier"
|
| 9 |
MODEL_FILENAME = "model/veg_model.h5"
|
| 10 |
|
| 11 |
# pobierz model z Hugging Face Hub
|
|
|
|
| 12 |
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILENAME)
|
|
|
|
| 13 |
model = tf.keras.models.load_model(model_path)
|
|
|
|
|
|
|
| 14 |
|
| 15 |
CLASS_NAMES = ['Bean', 'Bitter_Gourd', 'Bottle_Gourd', 'Brinjal', 'Broccoli',
|
| 16 |
'Cabbage', 'Capsicum', 'Carrot', 'Cauliflower', 'Cucumber',
|
| 17 |
'Papaya', 'Potato', 'Pumpkin', 'Radish', 'Tomato']
|
| 18 |
-
|
| 19 |
IMG_SIZE = (128, 128)
|
| 20 |
|
| 21 |
def predict(img_path):
|
| 22 |
-
|
| 23 |
-
from PIL import Image
|
| 24 |
img = Image.open(img_path)
|
| 25 |
img = img.resize(IMG_SIZE)
|
| 26 |
x = image.img_to_array(img)
|
| 27 |
x = np.expand_dims(x, axis=0) / 255.0
|
|
|
|
| 28 |
preds = model.predict(x)
|
| 29 |
probs = preds[0]
|
|
|
|
|
|
|
| 30 |
return {CLASS_NAMES[i]: float(probs[i]) for i in range(len(CLASS_NAMES))}
|
| 31 |
|
| 32 |
iface = gr.Interface(
|
| 33 |
fn=predict,
|
| 34 |
-
inputs=gr.Image(type="filepath"),
|
| 35 |
outputs=gr.Label(num_top_classes=3),
|
| 36 |
title="Vegetable Classifier",
|
| 37 |
-
description="Wgraj zdj臋cie warzywa, a model powie co to jest."
|
| 38 |
-
)
|
| 39 |
|
| 40 |
if __name__ == "__main__":
|
| 41 |
iface.launch(show_error=True)
|
|
|
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
import numpy as np
|
| 6 |
import os
|
| 7 |
+
from PIL import Image
|
| 8 |
|
| 9 |
MODEL_REPO = "zotthytt12/vegetable-classifier"
|
| 10 |
MODEL_FILENAME = "model/veg_model.h5"
|
| 11 |
|
| 12 |
# pobierz model z Hugging Face Hub
|
| 13 |
+
print("Pobieranie modelu...")
|
| 14 |
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILENAME)
|
| 15 |
+
print("Model pobrany, 艂adowanie...")
|
| 16 |
model = tf.keras.models.load_model(model_path)
|
| 17 |
+
print("Model za艂adowany.")
|
| 18 |
+
|
| 19 |
|
| 20 |
CLASS_NAMES = ['Bean', 'Bitter_Gourd', 'Bottle_Gourd', 'Brinjal', 'Broccoli',
|
| 21 |
'Cabbage', 'Capsicum', 'Carrot', 'Cauliflower', 'Cucumber',
|
| 22 |
'Papaya', 'Potato', 'Pumpkin', 'Radish', 'Tomato']
|
|
|
|
| 23 |
IMG_SIZE = (128, 128)
|
| 24 |
|
| 25 |
def predict(img_path):
|
| 26 |
+
|
|
|
|
| 27 |
img = Image.open(img_path)
|
| 28 |
img = img.resize(IMG_SIZE)
|
| 29 |
x = image.img_to_array(img)
|
| 30 |
x = np.expand_dims(x, axis=0) / 255.0
|
| 31 |
+
|
| 32 |
preds = model.predict(x)
|
| 33 |
probs = preds[0]
|
| 34 |
+
|
| 35 |
+
|
| 36 |
return {CLASS_NAMES[i]: float(probs[i]) for i in range(len(CLASS_NAMES))}
|
| 37 |
|
| 38 |
iface = gr.Interface(
|
| 39 |
fn=predict,
|
| 40 |
+
inputs=gr.Image(type="filepath"),
|
| 41 |
outputs=gr.Label(num_top_classes=3),
|
| 42 |
title="Vegetable Classifier",
|
| 43 |
+
description="Wgraj zdj臋cie warzywa, a model powie co to jest.")
|
|
|
|
| 44 |
|
| 45 |
if __name__ == "__main__":
|
| 46 |
iface.launch(show_error=True)
|