Delete app.py
Browse files
app.py
DELETED
@@ -1,52 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import torch
|
3 |
-
import cv2
|
4 |
-
import numpy as np
|
5 |
-
from PIL import Image
|
6 |
-
from ultralytics import YOLO
|
7 |
-
|
8 |
-
# Load YOLOv11 Model
|
9 |
-
model_path = "best.pt"
|
10 |
-
model = YOLO(model_path)
|
11 |
-
|
12 |
-
def predict(image):
|
13 |
-
image = np.array(image)
|
14 |
-
results = model(image, conf=0.85)
|
15 |
-
|
16 |
-
detected_classes = set() # Track unique detected classes
|
17 |
-
labels = []
|
18 |
-
|
19 |
-
# Draw bounding boxes and extract labels
|
20 |
-
for result in results:
|
21 |
-
for box in result.boxes:
|
22 |
-
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
23 |
-
conf = box.conf[0]
|
24 |
-
cls = int(box.cls[0])
|
25 |
-
class_name = model.names[cls]
|
26 |
-
|
27 |
-
detected_classes.add(class_name) # Store detected class
|
28 |
-
label = f"{class_name} {conf:.2f}"
|
29 |
-
labels.append(label)
|
30 |
-
|
31 |
-
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
32 |
-
cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
33 |
-
|
34 |
-
# Define possible classes (adjust based on your dataset)
|
35 |
-
possible_classes = {"front", "back"}
|
36 |
-
|
37 |
-
# Identify missing class if any
|
38 |
-
missing_classes = possible_classes - detected_classes
|
39 |
-
if missing_classes:
|
40 |
-
labels.append(f"Missing: {', '.join(missing_classes)}")
|
41 |
-
|
42 |
-
return Image.fromarray(image), labels
|
43 |
-
|
44 |
-
# Gradio Interface
|
45 |
-
iface = gr.Interface(
|
46 |
-
fn=predict,
|
47 |
-
inputs="image",
|
48 |
-
outputs=["image", "text"], # Returning both image and detected labels
|
49 |
-
title="YOLOv11 Object Detection (Front & Back Card)"
|
50 |
-
)
|
51 |
-
|
52 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|