File size: 1,158 Bytes
f4435a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
import json

# Load TFLite model
interpreter = tf.lite.Interpreter(model_path="plant_disease_model.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Load labels
with open("class_labels.json") as f:
    labels = json.load(f)

def predict(img):
    if isinstance(img, np.ndarray):
        img = Image.fromarray(img)
    img = img.resize((224, 224))  # adjust size to your model
    img = np.array(img, dtype=np.float32) / 255.0
    img = np.expand_dims(img, axis=0)

    interpreter.set_tensor(input_details[0]['index'], img)
    interpreter.invoke()
    output_data = interpreter.get_tensor(output_details[0]['index'])

    pred_class = int(np.argmax(output_data[0]))
    print("Input shape:", img.shape)
    print("Input dtype:", img.dtype)
    print("Output:", output_data)
    print("Predicted class:", pred_class)
    print("Label:", labels[str(pred_class)])
    return labels[str(pred_class)]
    

demo = gr.Interface(fn=predict, inputs="image", outputs="label")
demo.launch()