"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', 'yolov5n6')
Your supervision library would be used here
import supervision as sv
Define the polygon points (example coordinates, your use-case might be different)
polygon = np.array([ [30, 30], [1620, 985], [2160, 1920], [1620, 2855], [540, 2855], [0, 1920] ])
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)
def live_inference(camera): while True: # Capture frame-by-frame from camera ret, frame = camera.read() if not ret: print("Failed to grab frame") break
# Convert the image from BGR to RGB frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Perform inference results = model(frame_rgb, size=900) detections = sv.Detections.from_yolov5(results) detections = detections[(detections.class_id == 0) & (detections.confidence > 0.5)] # Annotate frame_annotated = box_annotator.annotate(scene=frame_rgb, detections=detections) # Convert the image from RGB to BGR for OpenCV display frame_annotated_bgr = cv2.cvtColor(frame_annotated, cv2.COLOR_RGB2BGR) # Display the resulting frame cv2.imshow('YOLOv5 Live Detection', frame_annotated_bgr) # Break the loop if ‘q’ is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture camera.release() cv2.destroyAllWindows()Run the live inference function
live_inference(camera)"
please update the polygon size to a small square at the center with the size of the inference size=900
- Assistant
To update the polygon size to a small square at the center of the frame with size equal to 900, we have to identify the center of the frame first. Since we are not given the resolution of the camera, I will use common variables for frame width and height that we would have to determine at runtime.
Assuming you have a frame width (fw) and frame height (fh), the center coordinates can be identified as (fw/2, fh/2). The square with size 900 will have each side be 900 pixels, so we create a square that starts at the center minus half of 900 and ends at the center plus half of 900 on both axes.
Here's how you could set the polygon points:
# Get frame shape (assuming 1920x1080 for this example, replace with your camera resolution) ret, frame = camera.read() if ret: fh, fw = frame.shape[:2] else: print("Failed to grab frame") camera.release() cv2.destroyAllWindows() raise ValueError("Could not determine frame size.") # Calculate the top left and bottom right points for the square with size 900 size = 900 center_x, center_y = fw // 2, fh // 2 half_size = size // 2 top_left_x, top_left_y = center_x - half_size, center_y - half_size bottom_right_x, bottom_right_y = center_x + half_size, center_y + half_size # Define the polygon points polygon = np.array([ [top_left_x, top_left_y], [bottom_right_x, top_left_y], [bottom_right_x, bottom_right_y], [top_left_x, bottom_right_y] ]) # rest of the code remains unchanged