"import cv2 import torch import numpy as np Load YOLOv5 model model =…
- User
"import cv2 import torch import numpy as np
Load YOLOv5 model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s6')
Your supervision library would be used here
import supervision as sv
Initialize the camera (0 for default camera, 1 for external cameras, and so on)
camera = cv2.VideoCapture(0)
Define the BoxAnnotator from the supervision library
box_annotator = sv.BoxAnnotator(thickness=4, text_thickness=4, text_scale=2)
Define the ROI polygon points (clockwise or counter-clockwise)
roi_polygon = np.array([ [100, 300], # Point 1 (x1, y1) [300, 200], # Point 2 (x2, y2) [400, 400], # Point 3 (x3, y3) [200, 500] # Point 4 (x4, y4) ], dtype=np.int32)
def create_mask(frame, roi_polygon): # Create a black mask with the same dimensions as the frame mask = np.zeros_like(frame, dtype=np.uint8)
code# Fill the ROI polygon with white color cv2.fillPoly(mask, [roi_polygon], (255, 255, 255)) # Mask the frame to only keep ROI and set everything else to black frame_roi = cv2.bitwise_and(frame, mask) return frame_roidef live_inference(camera): while True: # Capture frame-by-frame from camera ret, frame = camera.read() if not ret: print("Failed to grab frame") break
code - Assistant