Spaces:
Sleeping
Sleeping
File size: 1,442 Bytes
dae7c61 |
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 48 49 |
import gradio as gr
import torch
import cv2
import numpy as np
from PIL import Image
from pathlib import Path
import subprocess
import os
from ultralytics import YOLO
# β
Install flash_attn from local wheel (must be uploaded to the space)
whl_file = "flash_attn-2.7.3+cu11torch2.2cxx11abiFALSE-cp311-cp311-linux_x86_64.whl"
if os.path.exists(whl_file):
subprocess.run(["pip", "install", whl_file])
else:
print("β οΈ .whl file for flash_attn not found. Please upload it to the space.")
# β
Load YOLOv12 model
model_path = Path("runs/detect/train7/weights/best.pt")
assert model_path.exists(), "Model not found at runs/detect/train7/weights/best.pt"
model = YOLO(str(model_path))
# β
Detection function
def detect_damage(image: Image.Image):
results = model(image)
is_damaged = False
for result in results:
for box in result.boxes:
class_id = int(box.cls[0])
label = model.names[class_id]
print("Detected:", label)
if "apple_damaged" in label.lower():
is_damaged = True
break
return {"is_damaged": is_damaged}
# β
Gradio UI
demo = gr.Interface(
fn=detect_damage,
inputs=gr.Image(type="pil"),
outputs=gr.JSON(label="Detection Result"),
title="π Apple Damage Detector",
description="Upload an image of an apple to detect whether it is damaged using your trained YOLOv12 model."
)
demo.launch()
|