Update app.py
Browse files
app.py
CHANGED
@@ -6,27 +6,39 @@ 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)
|
15 |
|
|
|
16 |
labels = []
|
|
|
17 |
# Draw bounding boxes and extract labels
|
18 |
for result in results:
|
19 |
for box in result.boxes:
|
20 |
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
21 |
conf = box.conf[0]
|
22 |
cls = int(box.cls[0])
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
27 |
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
28 |
cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
return Image.fromarray(image), labels
|
31 |
|
32 |
# Gradio Interface
|
@@ -34,7 +46,7 @@ iface = gr.Interface(
|
|
34 |
fn=predict,
|
35 |
inputs="image",
|
36 |
outputs=["image", "text"], # Returning both image and detected labels
|
37 |
-
title="YOLOv11 Object Detection"
|
38 |
)
|
39 |
|
40 |
iface.launch()
|
|
|
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)
|
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
|
|
|
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()
|