Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,33 +8,32 @@ from ultralytics import YOLO
|
|
8 |
# Load YOLO model
|
9 |
model = YOLO("best.pt").to("cpu")
|
10 |
|
11 |
-
def
|
12 |
-
"""Detect objects using YOLO and
|
13 |
image_np = np.array(image)
|
|
|
|
|
14 |
results = model(image_np, conf=0.85, device='cpu')
|
15 |
|
16 |
-
|
17 |
for result in results:
|
18 |
for box in result.boxes:
|
19 |
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
20 |
class_name = model.names[int(box.cls[0])]
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
23 |
|
24 |
-
return
|
25 |
-
|
26 |
-
def predict(image):
|
27 |
-
"""Process image: detect objects and crop them."""
|
28 |
-
cropped_images = detect_and_crop(image)
|
29 |
-
return list(cropped_images.values()) if cropped_images else image
|
30 |
|
31 |
# Gradio interface
|
32 |
iface = gr.Interface(
|
33 |
fn=predict,
|
34 |
inputs="image",
|
35 |
-
|
36 |
-
|
37 |
-
title="License Field Detection & Cropping"
|
38 |
)
|
39 |
|
40 |
iface.launch()
|
|
|
8 |
# Load YOLO model
|
9 |
model = YOLO("best.pt").to("cpu")
|
10 |
|
11 |
+
def predict(image):
|
12 |
+
"""Detect objects using YOLO and draw bounding boxes on the image."""
|
13 |
image_np = np.array(image)
|
14 |
+
|
15 |
+
# Perform inference
|
16 |
results = model(image_np, conf=0.85, device='cpu')
|
17 |
|
18 |
+
# Draw bounding boxes
|
19 |
for result in results:
|
20 |
for box in result.boxes:
|
21 |
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
22 |
class_name = model.names[int(box.cls[0])]
|
23 |
+
|
24 |
+
# Draw rectangle and label
|
25 |
+
cv2.rectangle(image_np, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
26 |
+
cv2.putText(image_np, class_name, (x1, y1 - 10),
|
27 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
28 |
|
29 |
+
return Image.fromarray(image_np)
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Gradio interface
|
32 |
iface = gr.Interface(
|
33 |
fn=predict,
|
34 |
inputs="image",
|
35 |
+
outputs="image",
|
36 |
+
title="YOLO Object Detection"
|
|
|
37 |
)
|
38 |
|
39 |
iface.launch()
|