from this code: import numpy as np import supervision as sv initiate polygon zone…
- User
from this code:
import numpy as np
import supervision as sv
initiate polygon zone
polygon = np.array([
[540, 985], [1620, 985], [2160, 1920], [1620, 2855], [540, 2855], [0, 1920]])
video_info = sv.VideoInfo.from_video_path(MARKET_SQUARE_VIDEO_PATH)
zone = sv.PolygonZone(polygon=polygon, frame_resolution_wh=video_info.resolution_wh)
extract video frame
generator = sv.get_video_frames_generator(MARKET_SQUARE_VIDEO_PATH)
iterator = iter(generator)
frame = next(iterator)
detect
results = model(frame, size=1280)
detections = sv.Detections.from_yolov5(results)
mask = zone.trigger(detections=detections)
detections = detections[(detections.class_id == 0) & (detections.confidence > 0.5) & mask]
annotate
box_annotator = sv.BoxAnnotator(thickness=4, text_thickness=4, text_scale=2)
frame = box_annotator.annotate(scene=frame, detections=detections)
frame = sv.draw_polygon(scene=frame, polygon=polygon, color=sv.Color.red(), thickness=6)
%matplotlib inline
sv.show_frame_in_notebook(frame, (16, 16))
they created a polygon where the inference/detection only runs there. how can i implement that here on my code where i intend to put a small square at the center given that the size=900? ``` # 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], [80, 80], [100, 100], [40, 40], [10, 10], [50, 50] ]) # 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=700) 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) ``` - Assistant
- User
what if we assume size is 900x720
- Assistant