How to scale the zone dynamically?

I have the following package through which I am getting the screen size
from screeninfo import get_monitors
and then passing the screen size to the following function to get the full screen size while maintaining the aspect ratio

def ResizeWithAspectRatio(image, width=None, height=None, inter=cv2.INTER_AREA):
    dim = None
    (h, w) = image.shape[:2]

    if width is None and height is None:
        return image
    if width is None:
        r = height / float(h)
        dim = (int(w * r), height)
    else:
        r = width / float(w)
        dim = (width, int(h * r))

    resized_image = cv2.resize(image, dim, interpolation=inter)
    return resized_image,r

how do I scale the zone?

class CustomSink:
    def __init__(self, weights_path: str, zone_configuration_path: str, classes: List[int]):
        self._model = YOLO(weights_path)
        self.classes = classes
        self.tracker = sv.ByteTrack(minimum_matching_threshold=0.5)
        self.fps_monitor = sv.FPSMonitor()
        self.polygons = load_zones_config(file_path=zone_configuration_path)
        self.timers = [ClockBasedTimer() for _ in self.polygons]
        self.zones = [
            sv.PolygonZone(
                polygon=polygon,
                triggering_anchors=(sv.Position.CENTER,),
            )
            for polygon in self.polygons
        ]


    def infer(self, video_frames: List[VideoFrame]) -> List[any]: 
        # return self._model([v.image for v in video_frames], imgsz="800")
        resized_frames = [ResizeWithAspectRatio(v.image, width=screen_width)[0] for v in video_frames]
        results = self._model(resized_frames)

        return results

    def on_prediction(self, result: dict, frame: VideoFrame) -> None:
        self.fps_monitor.tick()
        fps = self.fps_monitor.fps
        detections = sv.Detections.from_ultralytics(result)
        detections = detections[find_in_list(detections.class_id, self.classes)]
        detections = self.tracker.update_with_detections(detections)
        // extracting the scale factor r 
        resized_frames, r = ResizeWithAspectRatio(frame.image, width=screen_width)
        annotated_frame =  resized_frames.copy()

        annotated_frame = sv.draw_text(
            scene=annotated_frame,
            text=f"{fps:.1f}",
            text_anchor=sv.Point(40, 30),
            background_color=sv.Color.from_hex("#A351FB"),
            text_color=sv.Color.from_hex("#000000"),
        )

        for idx, zone in enumerate(self.zones):
            annotated_frame = sv.draw_polygon(
           // if I multiply zone.polygon with r(scaling factor I get error 
                scene=annotated_frame, polygon=zone.polygon *r , color=COLORS.by_idx(idx)
            )

            detections_in_zone = detections[zone.trigger(detections)]
            time_in_zone = self.timers[idx].tick(detections_in_zone)
            custom_color_lookup = np.full(detections_in_zone.class_id.shape, idx)

            annotated_frame = COLOR_ANNOTATOR.annotate(
                scene=annotated_frame,
                detections=detections_in_zone,
                custom_color_lookup=custom_color_lookup,
            )
    
            labels = [
                f"#{tracker_id} {int(time // 60):02d}:{int(time % 60):02d}"
                for tracker_id, time in zip(detections_in_zone.tracker_id, time_in_zone)
            ]
            annotated_frame = LABEL_ANNOTATOR.annotate(
                scene=annotated_frame,
                detections=detections_in_zone,
                labels=labels,
                custom_color_lookup=custom_color_lookup,
            )
  
        cv2.imshow("Processed Video", annotated_frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            raise SystemExit("Program terminated by user")

following error

WARNING  Error in results dispatching - OpenCV(4.8.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\drawing.cpp:2463: error: inference_pipeline.py:905
                             (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in function 'cv::polylines'

How can I fix this?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.