Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,52 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
-
import
|
|
|
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
def
|
| 7 |
"""
|
| 8 |
-
|
| 9 |
"""
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
with gr.Blocks() as demo:
|
| 26 |
-
gr.Markdown("#
|
| 27 |
-
gr.Markdown("Upload an image or capture one
|
| 28 |
|
| 29 |
# Input section
|
| 30 |
image_input = gr.Image(type="pil", label="Upload or Capture Image")
|
| 31 |
|
| 32 |
# Output section
|
| 33 |
-
output_image = gr.Image(type="pil", label="Processed Image")
|
| 34 |
|
| 35 |
-
# Button
|
| 36 |
-
|
| 37 |
|
| 38 |
-
# Link button to the function
|
| 39 |
-
|
| 40 |
|
| 41 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# Load the YOLOv5 model (pre-trained on COCO dataset)
|
| 8 |
+
model = YOLO('yolov8n.pt') # You can replace with your custom model if available
|
| 9 |
|
| 10 |
+
def detect_objects(image):
|
| 11 |
"""
|
| 12 |
+
Detect suspicious objects in the image using YOLO.
|
| 13 |
"""
|
| 14 |
+
# Convert PIL image to OpenCV format (numpy array)
|
| 15 |
+
image_np = np.array(image)
|
| 16 |
+
results = model(image_np) # Perform detection
|
| 17 |
+
|
| 18 |
+
# Draw bounding boxes on the detected objects
|
| 19 |
+
for result in results:
|
| 20 |
+
boxes = result.boxes # Bounding boxes
|
| 21 |
+
for box in boxes:
|
| 22 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0]) # Extract box coordinates
|
| 23 |
+
label = box.cls[0] # Class label
|
| 24 |
+
confidence = box.conf[0] # Confidence score
|
| 25 |
+
|
| 26 |
+
# Draw rectangle and label on the image
|
| 27 |
+
cv2.rectangle(image_np, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 28 |
+
text = f"{model.names[int(label)]} ({confidence:.2f})"
|
| 29 |
+
cv2.putText(image_np, text, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
|
| 30 |
+
|
| 31 |
+
# Convert back to PIL image
|
| 32 |
+
processed_image = Image.fromarray(cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB))
|
| 33 |
+
return processed_image
|
| 34 |
+
|
| 35 |
+
# Create a Gradio interface
|
| 36 |
with gr.Blocks() as demo:
|
| 37 |
+
gr.Markdown("# Suspicious Object Detection")
|
| 38 |
+
gr.Markdown("Upload an image or use your webcam to capture one. The app will detect objects using YOLOv5.")
|
| 39 |
|
| 40 |
# Input section
|
| 41 |
image_input = gr.Image(type="pil", label="Upload or Capture Image")
|
| 42 |
|
| 43 |
# Output section
|
| 44 |
+
output_image = gr.Image(type="pil", label="Processed Image with Annotations")
|
| 45 |
|
| 46 |
+
# Button for detection
|
| 47 |
+
detect_button = gr.Button("Detect Suspicious Objects")
|
| 48 |
|
| 49 |
+
# Link the button to the detection function
|
| 50 |
+
detect_button.click(detect_objects, inputs=[image_input], outputs=[output_image])
|
| 51 |
|
| 52 |
demo.launch()
|