Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,49 @@
|
|
| 1 |
-
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, ImageOps
|
| 6 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
#
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def predict(image: Image.Image):
|
| 16 |
-
#
|
| 17 |
img = ImageOps.exif_transpose(image).convert("RGB")
|
| 18 |
img = ImageOps.fit(img, IMG_SIZE, Image.Resampling.LANCZOS)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
arr = img_to_array(img)
|
| 22 |
arr = np.expand_dims(arr, 0)
|
| 23 |
-
probs = model.predict(arr)[0]
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
mapping = {
|
| 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
|
| 36 |
-
outputs=[
|
| 37 |
-
|
| 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__
|
| 45 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
+
from tensorflow.keras.models import load_model
|
| 3 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
| 4 |
+
from PIL import ImageOps, Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
import gradio as gr
|
| 7 |
|
| 8 |
+
# ——————————————————————
|
| 9 |
+
# 1) CARREGA O MAPEAMENTO EXATO de classes
|
| 10 |
+
# no teu treino, geraste um label_map.json com:
|
| 11 |
+
# json.dump(enc.classes_.tolist(), open("label_map.json","w"))
|
| 12 |
+
# subiste esse label_map.json junto do modelo
|
| 13 |
+
CLASS_ORDER = json.load(open("label_map.json"))
|
| 14 |
|
| 15 |
+
# ——————————————————————
|
| 16 |
+
# 2) PRÉ-PROCESSAMENTO robusto para fotos de telemóvel
|
| 17 |
+
IMG_SIZE = (224, 224)
|
| 18 |
+
MODEL_PATH= "cropvision_model.keras"
|
| 19 |
+
model = load_model(MODEL_PATH)
|
| 20 |
|
| 21 |
def predict(image: Image.Image):
|
| 22 |
+
# corrige rotação EXIF e faz um crop centrado
|
| 23 |
img = ImageOps.exif_transpose(image).convert("RGB")
|
| 24 |
img = ImageOps.fit(img, IMG_SIZE, Image.Resampling.LANCZOS)
|
| 25 |
|
| 26 |
+
# normaliza e infere
|
| 27 |
+
arr = img_to_array(img)/255.0
|
| 28 |
arr = np.expand_dims(arr, 0)
|
| 29 |
+
probs = model.predict(arr)[0]
|
| 30 |
|
| 31 |
+
# escolhe a classe e devolve também todas as probabilidades
|
| 32 |
+
idx = int(np.argmax(probs))
|
| 33 |
+
label = CLASS_ORDER[idx]
|
| 34 |
+
mapping = {CLASS_ORDER[i]: float(probs[i]) for i in range(len(probs))}
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
return label, f"{mapping}"
|
| 37 |
+
|
| 38 |
+
# ——————————————————————
|
| 39 |
+
# 3) Gradio UI
|
| 40 |
demo = gr.Interface(
|
| 41 |
fn=predict,
|
| 42 |
+
inputs=gr.Image(type="pil", label="Carrega folha"),
|
| 43 |
+
outputs=[gr.Textbox(label="Classe"), gr.Code(label="Probabilidades")],
|
| 44 |
+
title="CropVision"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
+
if __name__=="__main__":
|
| 48 |
demo.launch()
|
| 49 |
+
|