-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmobile_detection.py
More file actions
31 lines (23 loc) · 1014 Bytes
/
mobile_detection.py
File metadata and controls
31 lines (23 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import cv2
import torch
from ultralytics import YOLO
# Load YOLOv8n model - this will automatically download if not present
model = YOLO("yolov8n.pt")
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
def process_mobile_detection(frame):
results = model(frame, verbose=False)
mobile_detected = False
for result in results:
for box in result.boxes:
conf = box.conf[0].item()
cls = int(box.cls[0].item())
# Update class index for mobile phones in COCO dataset (67 is cell phone)
if conf < 0.8 or cls != 67:
continue
x1, y1, x2, y2 = map(int, box.xyxy[0])
label = f"Mobile ({conf:.2f})"
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 3)
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
mobile_detected = True
return frame, mobile_detected