Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,48 +2,43 @@ import gradio as gr
|
|
2 |
import numpy as np
|
3 |
from tensorflow.keras.preprocessing.image import img_to_array
|
4 |
from tensorflow.keras.models import load_model
|
5 |
-
from PIL import Image
|
|
|
6 |
|
7 |
-
#
|
8 |
-
IMG_SIZE
|
9 |
-
|
10 |
-
|
11 |
-
MODEL_PATH = "cropvision_model.keras" # ensure this file is in the same folder
|
12 |
|
|
|
13 |
|
14 |
-
# --- Load model once at startup ---
|
15 |
-
model = load_model(MODEL_PATH)
|
16 |
-
import hashlib, pathlib, json
|
17 |
-
|
18 |
-
# --- debug ---
|
19 |
-
h = hashlib.md5(pathlib.Path(MODEL_PATH).read_bytes()).hexdigest()
|
20 |
-
print("📝 MD5 do modelo:", h)
|
21 |
-
print("Camadas:", len(model.layers), "| Params:", model.count_params())
|
22 |
-
print("class_indices que o generator gravou:", json.dumps(model.class_indices if hasattr(model, 'class_indices') else "N/A"))
|
23 |
-
# -------------
|
24 |
-
|
25 |
-
|
26 |
-
# --- Prediction function ---
|
27 |
def predict(image: Image.Image):
|
28 |
-
#
|
29 |
-
img = image.convert("RGB")
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
|
38 |
-
|
|
|
39 |
|
40 |
-
# --- Gradio UI ---
|
41 |
demo = gr.Interface(
|
42 |
fn=predict,
|
43 |
-
inputs=gr.Image(type="pil", label="Carrega uma
|
44 |
-
outputs=
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
47 |
)
|
48 |
|
49 |
if __name__ == "__main__":
|
|
|
2 |
import numpy as np
|
3 |
from tensorflow.keras.preprocessing.image import img_to_array
|
4 |
from tensorflow.keras.models import load_model
|
5 |
+
from PIL import Image, ImageOps
|
6 |
+
import json
|
7 |
|
8 |
+
# Constantes (mantém IMG_SIZE, CLASS_ORDER, etc)
|
9 |
+
IMG_SIZE = (224, 224)
|
10 |
+
CLASS_ORDER = sorted(['Healthy','Leaf Blight','Black Rot','ESCA'])
|
11 |
+
# → ['Black Rot','ESCA','Healthy','Leaf Blight']
|
|
|
12 |
|
13 |
+
model = load_model("cropvision_model.keras")
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
def predict(image: Image.Image):
|
16 |
+
# pré-processamento robusto
|
17 |
+
img = ImageOps.exif_transpose(image).convert("RGB")
|
18 |
+
img = ImageOps.fit(img, IMG_SIZE, Image.Resampling.LANCZOS)
|
19 |
+
|
20 |
+
# inferência
|
21 |
+
arr = img_to_array(img) / 255.0
|
22 |
+
arr = np.expand_dims(arr, 0)
|
23 |
+
probs = model.predict(arr)[0] # vetor de 4 probabilidades
|
24 |
|
25 |
+
idx = int(np.argmax(probs))
|
26 |
+
cls = CLASS_ORDER[idx]
|
27 |
+
# mapeamento label → probabilidade
|
28 |
+
mapping = {label: float(probs[i]) for i,label in enumerate(CLASS_ORDER)}
|
29 |
|
30 |
+
# devolve (rótulo, JSON-formatado das probabilidades)
|
31 |
+
return cls, json.dumps(mapping, indent=2)
|
32 |
|
|
|
33 |
demo = gr.Interface(
|
34 |
fn=predict,
|
35 |
+
inputs=gr.Image(type="pil", label="Carrega uma folha"),
|
36 |
+
outputs=[
|
37 |
+
gr.Textbox(label="Classe predita"),
|
38 |
+
gr.Code(label="Probabilidades (label:valor)")
|
39 |
+
],
|
40 |
+
title="CropVision – classificação de folhas",
|
41 |
+
description="Healthy / Leaf Blight / Black Rot / ESCA"
|
42 |
)
|
43 |
|
44 |
if __name__ == "__main__":
|