-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectDetectNano.py
More file actions
69 lines (53 loc) · 2.32 KB
/
ObjectDetectNano.py
File metadata and controls
69 lines (53 loc) · 2.32 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from ultralytics import YOLO
import cv2
import numpy as np
# Load model
model = YOLO("yolov8n-seg.pt") # Use smaller model for Surface Pro
# Open webcam
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
results = model(frame)[0] # Get the first result from model inference
overlay = frame.copy() # Copy frame for transparent mask blending
if results.masks is not None:
for i, mask in enumerate(results.masks.data):
# Convert mask to a binary mask
mask = mask.cpu().numpy().astype("uint8") * 255
mask_resized = cv2.resize(mask, (frame.shape[1], frame.shape[0]))
# Create empty color mask
colored_mask = np.zeros_like(frame, dtype=np.uint8)
# Get class ID and assign consistent color
class_id = int(results.boxes.cls[i].item())
np.random.seed(class_id) # Ensure same color for same class
color = tuple(np.random.randint(0, 256, size=3).tolist())
# Apply color to the mask
for c in range(3):
colored_mask[:, :, c] = (mask_resized / 255) * color[c]
# Blend the color mask with the original frame
alpha = 0.4
overlay = cv2.addWeighted(overlay, 1, colored_mask, alpha, 0)
# Find mask center for label placement
M = cv2.moments(mask_resized)
if M["m00"] != 0:
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
else:
cX, cY = frame.shape[1] // 2, frame.shape[0] // 2
# Get class label
label = results.names[class_id]
# Font settings (slightly larger + thin outline)
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 0.85 # Slightly larger
text_color = (255, 255, 255)
outline_color = (30, 30, 30)
# Draw label with thin outline
cv2.putText(overlay, label, (cX - 30, cY), font, font_scale, outline_color, 2, cv2.LINE_AA)
cv2.putText(overlay, label, (cX - 30, cY), font, font_scale, text_color, 1, cv2.LINE_AA)
# Show result with transparent masks
cv2.imshow("Masked Segmentation", overlay)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()