Spaces:
Build error
Build error
Commit ·
62f55ce
1
Parent(s): e8abe65
fix: revert to use numpy for rendering
Browse files
app.py
CHANGED
|
@@ -10,23 +10,35 @@ model_path = hf_hub_download(repo_id='ethandavey/yoloV5-coursework-model', filen
|
|
| 10 |
# Load the YOLOv5 model
|
| 11 |
model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path, source='github')
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
print("Model class names:", model.names)
|
|
|
|
| 16 |
print("Model loaded successfully.")
|
| 17 |
|
| 18 |
def detect(image):
|
| 19 |
# Run inference
|
| 20 |
results = model(image)
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
results.
|
| 24 |
-
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Create Gradio Interface
|
| 32 |
iface = gr.Interface(
|
|
|
|
| 10 |
# Load the YOLOv5 model
|
| 11 |
model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path, source='github')
|
| 12 |
|
| 13 |
+
# Print model class names
|
|
|
|
| 14 |
print("Model class names:", model.names)
|
| 15 |
+
|
| 16 |
print("Model loaded successfully.")
|
| 17 |
|
| 18 |
def detect(image):
|
| 19 |
# Run inference
|
| 20 |
results = model(image)
|
| 21 |
|
| 22 |
+
# Get detections
|
| 23 |
+
detections = results.xyxy[0] # xyxy format for bounding boxes
|
| 24 |
+
|
| 25 |
+
# Check if any detections were made
|
| 26 |
+
if len(detections) > 0:
|
| 27 |
+
print(f"Number of detections: {len(detections)}")
|
| 28 |
+
for i, (*xyxy, conf, cls) in enumerate(detections):
|
| 29 |
+
# Extract box information
|
| 30 |
+
xyxy = [coord.item() for coord in xyxy] # Coordinates
|
| 31 |
+
conf = conf.item() # Confidence score
|
| 32 |
+
cls = int(cls.item()) # Class index
|
| 33 |
+
class_name = model.names[cls] # Class name
|
| 34 |
+
print(f"Detection {i}: Class '{class_name}', Confidence {conf:.2f}, Coordinates {xyxy}")
|
| 35 |
+
else:
|
| 36 |
+
print("No detections were made.")
|
| 37 |
+
|
| 38 |
+
# Annotate image
|
| 39 |
+
annotated_image = np.squeeze(results.render()) # Render the detections on the image
|
| 40 |
+
|
| 41 |
+
return Image.fromarray(annotated_image)
|
| 42 |
|
| 43 |
# Create Gradio Interface
|
| 44 |
iface = gr.Interface(
|