File size: 1,355 Bytes
5024345
 
 
 
 
 
b80818c
5024345
82389eb
19635a0
5024345
 
b80818c
5024345
b80818c
5024345
b80818c
 
5024345
 
 
 
 
 
82389eb
b80818c
82389eb
5024345
 
 
b80818c
5024345
 
b80818c
 
5024345
 
 
 
b80818c
5024345
 
b80818c
5024345
 
82389eb
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
import gradio as gr
import tensorflow as tf
from tensorflow.keras.preprocessing import image
from huggingface_hub import hf_hub_download
import numpy as np
import os
from PIL import Image  

MODEL_REPO = "zotthytt12/vegetable-classifier"
MODEL_FILENAME = "model/veg_model.h5"

# pobierz model z Hugging Face Hub
print("Pobieranie modelu...")
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILENAME)
print("Model pobrany, ładowanie...")
model = tf.keras.models.load_model(model_path)
print("Model załadowany.")


CLASS_NAMES = ['Bean', 'Bitter_Gourd', 'Bottle_Gourd', 'Brinjal', 'Broccoli',
               'Cabbage', 'Capsicum', 'Carrot', 'Cauliflower', 'Cucumber',
               'Papaya', 'Potato', 'Pumpkin', 'Radish', 'Tomato']
IMG_SIZE = (128, 128)

def predict(img_path):

    img = Image.open(img_path)
    img = img.resize(IMG_SIZE)
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0) / 255.0
    
    preds = model.predict(x)
    probs = preds[0]
    

    return {CLASS_NAMES[i]: float(probs[i]) for i in range(len(CLASS_NAMES))}

iface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="filepath"),
    outputs=gr.Label(num_top_classes=3),
    title="Vegetable Classifier",
    description="Wgraj zdjęcie warzywa, a model powie co to jest.")

if __name__ == "__main__":
    iface.launch(show_error=True)