Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +45 -0
- best.pt +3 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import torch
|
4 |
+
from ultralytics import YOLO
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
# Load the YOLOv8 model (replace 'best.pt' with the path to your model)
|
8 |
+
model = YOLO('best.pt')
|
9 |
+
|
10 |
+
# Function to perform object detection using YOLOv8
|
11 |
+
def detect_objects(image):
|
12 |
+
# Convert the image to RGB (OpenCV loads images as BGR)
|
13 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
14 |
+
|
15 |
+
# Perform inference on the image
|
16 |
+
results = model.predict(image_rgb)
|
17 |
+
|
18 |
+
# Get bounding boxes and class labels from the results
|
19 |
+
annotated_image = results[0].plot() # YOLOv8 has a plot method that returns an annotated image
|
20 |
+
|
21 |
+
return annotated_image
|
22 |
+
|
23 |
+
# Gradio Interface (take a photo using webcam or upload an image)
|
24 |
+
def take_photo_and_detect():
|
25 |
+
# OpenCV function to take a photo using the webcam
|
26 |
+
def capture_image():
|
27 |
+
cap = cv2.VideoCapture(0)
|
28 |
+
ret, frame = cap.read()
|
29 |
+
cap.release()
|
30 |
+
return frame
|
31 |
+
|
32 |
+
# Set up the Gradio interface
|
33 |
+
demo = gr.Interface(
|
34 |
+
fn=detect_objects, # The function to perform object detection
|
35 |
+
inputs=gr.Image(source="webcam", tool="editor", label="Take a photo or upload an image"),
|
36 |
+
outputs=gr.Image(label="Detected Objects"),
|
37 |
+
title="YOLOv8 Object Detection",
|
38 |
+
description="Take a photo or upload an image to detect objects using the YOLOv8 model."
|
39 |
+
)
|
40 |
+
|
41 |
+
demo.launch()
|
42 |
+
|
43 |
+
# Start the app
|
44 |
+
if __name__ == "__main__":
|
45 |
+
take_photo_and_detect()
|
best.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7834784fd9d0a1d69d5c698637a998f256023f8ae5fc4c7809f27cd0f0834334
|
3 |
+
size 46786711
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
ultralytics==8.3.11
|
2 |
+
opencv-python
|
3 |
+
gradio
|