Spaces:
Build error
Build error
Commit
·
e91b01d
1
Parent(s):
24f982f
fix: hide labels
Browse files- app.py +25 -10
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -3,22 +3,24 @@ import torch
|
|
| 3 |
from PIL import Image
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
|
| 7 |
# Download the model from Hugging Face Hub
|
| 8 |
model_path = hf_hub_download(repo_id='ethandavey/yoloV5-coursework-model', filename='model.pt')
|
| 9 |
|
| 10 |
# Load the YOLOv5 model
|
| 11 |
model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path, source='github')
|
| 12 |
-
model.line_thickness = 3
|
| 13 |
-
model.hide_labels = True
|
| 14 |
-
|
| 15 |
|
| 16 |
# Print model class names
|
| 17 |
print("Model class names:", model.names)
|
| 18 |
-
|
| 19 |
print("Model loaded successfully.")
|
| 20 |
|
| 21 |
def detect(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# Run inference
|
| 23 |
results = model(image)
|
| 24 |
|
|
@@ -29,19 +31,32 @@ def detect(image):
|
|
| 29 |
if len(detections) > 0:
|
| 30 |
print(f"Number of detections: {len(detections)}")
|
| 31 |
for i, (*xyxy, conf, cls) in enumerate(detections):
|
| 32 |
-
# Extract box
|
| 33 |
-
xyxy = [coord.item() for coord in xyxy] # Coordinates
|
| 34 |
conf = conf.item() # Confidence score
|
| 35 |
cls = int(cls.item()) # Class index
|
| 36 |
class_name = model.names[cls] # Class name
|
| 37 |
print(f"Detection {i}: Class '{class_name}', Confidence {conf:.2f}, Coordinates {xyxy}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
else:
|
| 39 |
print("No detections were made.")
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
return
|
| 45 |
|
| 46 |
# Create Gradio Interface
|
| 47 |
iface = gr.Interface(
|
|
@@ -49,7 +64,7 @@ iface = gr.Interface(
|
|
| 49 |
inputs=gr.Image(type="pil"),
|
| 50 |
outputs=gr.Image(type="pil"),
|
| 51 |
title="YOLOv5 Object Detection",
|
| 52 |
-
description="Upload an image to detect objects using the YOLOv5 model."
|
| 53 |
)
|
| 54 |
|
| 55 |
iface.launch()
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
import numpy as np
|
| 6 |
+
import cv2 # Import OpenCV
|
| 7 |
|
| 8 |
# Download the model from Hugging Face Hub
|
| 9 |
model_path = hf_hub_download(repo_id='ethandavey/yoloV5-coursework-model', filename='model.pt')
|
| 10 |
|
| 11 |
# Load the YOLOv5 model
|
| 12 |
model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path, source='github')
|
| 13 |
+
model.line_thickness = 3 # Set the thickness of the bounding box lines
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Print model class names
|
| 16 |
print("Model class names:", model.names)
|
|
|
|
| 17 |
print("Model loaded successfully.")
|
| 18 |
|
| 19 |
def detect(image):
|
| 20 |
+
# Convert PIL Image to NumPy array (OpenCV uses BGR format)
|
| 21 |
+
image_np = np.array(image)
|
| 22 |
+
image_np = image_np[:, :, ::-1].copy() # Convert RGB to BGR
|
| 23 |
+
|
| 24 |
# Run inference
|
| 25 |
results = model(image)
|
| 26 |
|
|
|
|
| 31 |
if len(detections) > 0:
|
| 32 |
print(f"Number of detections: {len(detections)}")
|
| 33 |
for i, (*xyxy, conf, cls) in enumerate(detections):
|
| 34 |
+
# Extract box coordinates and convert to integers
|
| 35 |
+
xyxy = [int(coord.item()) for coord in xyxy] # Coordinates
|
| 36 |
conf = conf.item() # Confidence score
|
| 37 |
cls = int(cls.item()) # Class index
|
| 38 |
class_name = model.names[cls] # Class name
|
| 39 |
print(f"Detection {i}: Class '{class_name}', Confidence {conf:.2f}, Coordinates {xyxy}")
|
| 40 |
+
|
| 41 |
+
# Draw bounding box without label
|
| 42 |
+
cv2.rectangle(
|
| 43 |
+
image_np,
|
| 44 |
+
(xyxy[0], xyxy[1]),
|
| 45 |
+
(xyxy[2], xyxy[3]),
|
| 46 |
+
color=(0, 255, 0),
|
| 47 |
+
thickness=model.line_thickness,
|
| 48 |
+
lineType=cv2.LINE_AA,
|
| 49 |
+
)
|
| 50 |
else:
|
| 51 |
print("No detections were made.")
|
| 52 |
|
| 53 |
+
# Convert BGR back to RGB
|
| 54 |
+
image_np = image_np[:, :, ::-1]
|
| 55 |
+
|
| 56 |
+
# Convert NumPy array back to PIL Image
|
| 57 |
+
annotated_image = Image.fromarray(image_np)
|
| 58 |
|
| 59 |
+
return annotated_image
|
| 60 |
|
| 61 |
# Create Gradio Interface
|
| 62 |
iface = gr.Interface(
|
|
|
|
| 64 |
inputs=gr.Image(type="pil"),
|
| 65 |
outputs=gr.Image(type="pil"),
|
| 66 |
title="YOLOv5 Object Detection",
|
| 67 |
+
description="Upload an image to detect objects using the YOLOv5 model. Labels are hidden in the output."
|
| 68 |
)
|
| 69 |
|
| 70 |
iface.launch()
|
requirements.txt
CHANGED
|
@@ -9,4 +9,5 @@ seaborn
|
|
| 9 |
tqdm
|
| 10 |
pandas
|
| 11 |
ultralytics
|
| 12 |
-
huggingface-hub
|
|
|
|
|
|
| 9 |
tqdm
|
| 10 |
pandas
|
| 11 |
ultralytics
|
| 12 |
+
huggingface-hub
|
| 13 |
+
opencv-python
|