Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
|
| 4 |
+
# Load YOLO model
|
| 5 |
+
model = YOLO('yolov8n.pt')
|
| 6 |
+
|
| 7 |
+
def detect_and_count_cars(image):
|
| 8 |
+
results = model.predict(image, conf=0.25)
|
| 9 |
+
detections = results[0].boxes.boxes.cpu().numpy()
|
| 10 |
+
car_count = 0
|
| 11 |
+
|
| 12 |
+
# Count cars
|
| 13 |
+
for detection in detections:
|
| 14 |
+
class_id = int(detection[5])
|
| 15 |
+
if model.names[class_id] == 'car':
|
| 16 |
+
car_count += 1
|
| 17 |
+
|
| 18 |
+
annotated_image = results[0].plot()
|
| 19 |
+
return annotated_image, f"Number of cars detected: {car_count}"
|
| 20 |
+
|
| 21 |
+
# Create Gradio interface
|
| 22 |
+
interface = gr.Interface(
|
| 23 |
+
fn=detect_and_count_cars,
|
| 24 |
+
inputs=gr.Image(type="numpy", label="Upload an Image"),
|
| 25 |
+
outputs=[
|
| 26 |
+
gr.Image(type="numpy", label="Annotated Image"),
|
| 27 |
+
gr.Textbox(label="Car Count")
|
| 28 |
+
],
|
| 29 |
+
title="Car Detection and Counting",
|
| 30 |
+
description="Upload an image to detect and count cars using YOLOv8."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Launch the interface
|
| 34 |
+
interface.launch()
|