Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
# Load your
|
| 7 |
model_path = 'best_hat_detection_model_100.pt' # Your model file on Hugging Face Space
|
| 8 |
-
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
model = torch.load(model_path, map_location=device)['model'].float()
|
| 12 |
-
model.eval()
|
| 13 |
-
|
| 14 |
-
# Define transformations needed for your model
|
| 15 |
-
transform = transforms.Compose([
|
| 16 |
-
transforms.Resize((640, 640)),
|
| 17 |
-
transforms.ToTensor(),
|
| 18 |
-
])
|
| 19 |
-
|
| 20 |
-
# Function to draw bounding boxes on the image
|
| 21 |
-
def draw_bboxes(image, predictions, threshold=0.5):
|
| 22 |
-
draw = ImageDraw.Draw(image)
|
| 23 |
-
|
| 24 |
-
for pred in predictions:
|
| 25 |
-
# YOLO model typically returns a list or tensor of bounding boxes and confidence scores
|
| 26 |
-
# pred: [x1, y1, x2, y2, confidence, class_id]
|
| 27 |
-
# Make sure to unpack these values correctly.
|
| 28 |
-
|
| 29 |
-
x1, y1, x2, y2, confidence = pred[:4] # Coordinates and confidence score
|
| 30 |
-
if confidence > threshold: # Only draw boxes above the threshold
|
| 31 |
-
draw.rectangle([x1, y1, x2, y2], outline="red", width=3)
|
| 32 |
-
draw.text((x1, y1), f"{confidence:.2f}", fill="red")
|
| 33 |
-
|
| 34 |
-
return image
|
| 35 |
-
|
| 36 |
-
# Function to predict and return image with bounding boxes
|
| 37 |
def predict_safety_hat(image):
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
with torch.no_grad():
|
| 42 |
-
predictions = model(input_tensor)[0] # Model output
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
| 52 |
-
return draw_bboxes(pil_image, predictions)
|
| 53 |
|
| 54 |
# Gradio Interface
|
| 55 |
-
inputs = gr.Image(label="Input Image")
|
| 56 |
-
outputs = gr.Image(label="Output Image with Bounding Boxes")
|
| 57 |
|
| 58 |
-
title = "Safety Hat
|
| 59 |
-
description = "Upload an image to detect safety hats and
|
| 60 |
|
| 61 |
-
gr.Interface(predict_safety_hat, inputs, outputs, title=title, description=description).launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
|
| 7 |
+
# Load your YOLO model
|
| 8 |
model_path = 'best_hat_detection_model_100.pt' # Your model file on Hugging Face Space
|
| 9 |
+
model = YOLO(model_path)
|
| 10 |
|
| 11 |
+
# Function to perform object detection and return the image with bounding boxes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def predict_safety_hat(image):
|
| 13 |
+
# Convert image from numpy array (Gradio format) to a format YOLO can process
|
| 14 |
+
image = np.array(image)
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Run inference using the YOLO model
|
| 17 |
+
results = model(image)
|
| 18 |
|
| 19 |
+
# Get the image with predictions drawn (bounding boxes, etc.)
|
| 20 |
+
result_image = results[0].plot() # YOLOv8 returns an image with the bounding boxes drawn
|
| 21 |
|
| 22 |
+
# Convert the result image back to PIL format for Gradio
|
| 23 |
+
return Image.fromarray(result_image)
|
|
|
|
| 24 |
|
| 25 |
# Gradio Interface
|
| 26 |
+
inputs = gr.Image(label="Input Image", type="numpy")
|
| 27 |
+
outputs = gr.Image(label="Output Image with Bounding Boxes")
|
| 28 |
|
| 29 |
+
title = "Safety Hat Detection"
|
| 30 |
+
description = "Upload an image to detect safety hats and see bounding boxes."
|
| 31 |
|
| 32 |
+
gr.Interface(fn=predict_safety_hat, inputs=inputs, outputs=outputs, title=title, description=description).launch()
|