Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,28 @@
|
|
| 1 |
-
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from ultralytics import YOLO
|
| 3 |
-
import cv2
|
| 4 |
-
import numpy as np
|
| 5 |
-
import torch
|
| 6 |
-
import time
|
| 7 |
|
| 8 |
-
# π Load model
|
| 9 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 10 |
print(f"π Using device: {device}")
|
| 11 |
|
|
@@ -15,34 +32,32 @@ model.to(device)
|
|
| 15 |
print("β
Model loaded successfully!")
|
| 16 |
print("Model class names:", model.names)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def detect(image):
|
| 19 |
"""
|
| 20 |
-
|
| 21 |
-
returns: annotated image and detected conclusion text
|
| 22 |
"""
|
| 23 |
try:
|
| 24 |
start_time = time.time()
|
| 25 |
|
| 26 |
-
#
|
| 27 |
results = model.predict(
|
| 28 |
-
image,
|
| 29 |
imgsz=640,
|
| 30 |
conf=0.25,
|
| 31 |
iou=0.45,
|
| 32 |
augment=True,
|
| 33 |
-
verbose=False
|
|
|
|
| 34 |
)
|
| 35 |
|
| 36 |
annotated = results[0].plot()
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
for box in results[0].boxes:
|
| 40 |
-
cls = int(box.cls[0])
|
| 41 |
-
conf = float(box.conf[0])
|
| 42 |
-
label = model.names[cls].strip().lower()
|
| 43 |
-
detected_classes.append(label)
|
| 44 |
-
|
| 45 |
-
# β
Decision logic
|
| 46 |
conclusion = "No Tomato Detected"
|
| 47 |
if any("damaged" in c for c in detected_classes):
|
| 48 |
conclusion = "Damaged π"
|
|
@@ -60,7 +75,10 @@ def detect(image):
|
|
| 60 |
print("β Error:", e)
|
| 61 |
return image, f"Error: {str(e)}"
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 64 |
interface = gr.Interface(
|
| 65 |
fn=detect,
|
| 66 |
inputs=gr.Image(type="numpy", label="Upload Tomato Image"),
|
|
@@ -70,7 +88,6 @@ interface = gr.Interface(
|
|
| 70 |
],
|
| 71 |
title="Tomato Quality Detector π
",
|
| 72 |
description="Upload a tomato image to detect its quality (Ripe / Unripe / Damaged) using a YOLOv8 model fine-tuned on a custom dataset.",
|
| 73 |
-
examples=None,
|
| 74 |
theme="default"
|
| 75 |
)
|
| 76 |
|
|
|
|
| 1 |
+
import os, sys, subprocess, torch, time, cv2, numpy as np, gradio as gr
|
| 2 |
+
|
| 3 |
+
# ==============================================================
|
| 4 |
+
# π Force correct Ultralytics version (prevents HF auto-upgrade)
|
| 5 |
+
# ==============================================================
|
| 6 |
+
subprocess.run(
|
| 7 |
+
[sys.executable, "-m", "pip", "uninstall", "-y", "ultralytics"],
|
| 8 |
+
stdout=subprocess.DEVNULL,
|
| 9 |
+
stderr=subprocess.DEVNULL,
|
| 10 |
+
)
|
| 11 |
+
subprocess.run(
|
| 12 |
+
[
|
| 13 |
+
sys.executable, "-m", "pip", "install",
|
| 14 |
+
"--no-deps", "--force-reinstall",
|
| 15 |
+
"git+https://github.com/ultralytics/ultralytics.git@v8.0.20"
|
| 16 |
+
],
|
| 17 |
+
stdout=subprocess.DEVNULL,
|
| 18 |
+
stderr=subprocess.DEVNULL,
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# ==============================================================
|
| 22 |
+
# π Import YOLO safely (after version is locked)
|
| 23 |
+
# ==============================================================
|
| 24 |
from ultralytics import YOLO
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
|
|
|
| 26 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 27 |
print(f"π Using device: {device}")
|
| 28 |
|
|
|
|
| 32 |
print("β
Model loaded successfully!")
|
| 33 |
print("Model class names:", model.names)
|
| 34 |
|
| 35 |
+
|
| 36 |
+
# ==============================================================
|
| 37 |
+
# π§ Inference Function
|
| 38 |
+
# ==============================================================
|
| 39 |
def detect(image):
|
| 40 |
"""
|
| 41 |
+
Run YOLO inference on the uploaded image and classify tomato quality.
|
|
|
|
| 42 |
"""
|
| 43 |
try:
|
| 44 |
start_time = time.time()
|
| 45 |
|
| 46 |
+
# Run YOLO prediction
|
| 47 |
results = model.predict(
|
| 48 |
+
source=image,
|
| 49 |
imgsz=640,
|
| 50 |
conf=0.25,
|
| 51 |
iou=0.45,
|
| 52 |
augment=True,
|
| 53 |
+
verbose=False,
|
| 54 |
+
device=device
|
| 55 |
)
|
| 56 |
|
| 57 |
annotated = results[0].plot()
|
| 58 |
+
detected_classes = [model.names[int(box.cls[0])].strip().lower() for box in results[0].boxes]
|
| 59 |
|
| 60 |
+
# Quality classification logic
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
conclusion = "No Tomato Detected"
|
| 62 |
if any("damaged" in c for c in detected_classes):
|
| 63 |
conclusion = "Damaged π"
|
|
|
|
| 75 |
print("β Error:", e)
|
| 76 |
return image, f"Error: {str(e)}"
|
| 77 |
|
| 78 |
+
|
| 79 |
+
# ==============================================================
|
| 80 |
+
# π₯οΈ Gradio Interface
|
| 81 |
+
# ==============================================================
|
| 82 |
interface = gr.Interface(
|
| 83 |
fn=detect,
|
| 84 |
inputs=gr.Image(type="numpy", label="Upload Tomato Image"),
|
|
|
|
| 88 |
],
|
| 89 |
title="Tomato Quality Detector π
",
|
| 90 |
description="Upload a tomato image to detect its quality (Ripe / Unripe / Damaged) using a YOLOv8 model fine-tuned on a custom dataset.",
|
|
|
|
| 91 |
theme="default"
|
| 92 |
)
|
| 93 |
|