'Getting error: (-215:Assertion failed) !dsize.empty() in function 'cv::resize' even when I make sure that the frame to be resized is not none

I've been stuck in this issue for weeks, I would really appreciate it if you help me in it. So here is the workflow of my project in a nutshell:

  1. Get input stream from live cameras
  2. Extract frame
  3. Run an AI pipeline over that frame (object detection, object tracking)
  4. Save the results
  5. Call function (visualize) that will change the color of the bounding box from AI results based on a condition (either red or green)

Long story short, the whole workflow used to work well until I did some changes in IPC, where previously, each camera used to connect to the system through separate ports, while I unified the port for both of them (using ZMQ multi-pup model), now I get this error in the resize function whenever I call the visualizer function:

cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-u4kjpz2z\opencv\modules\imgproc\src\resize.cpp:4057: error: (-215:Assertion failed) !dsize.empty() in function 'cv::resize'

Here is the code of the visualizer function:

    def visualize_intrusion(self, frame, intrusion_rois, tracks, stats, polygon_color):
            red_marked = []
            rois = []
            for intrusion_roi in intrusion_rois:
                for obj in tracks.Tracked_Objects:
                    ########       GETTING BBOX AND ID       ########
                    left, top, right, bottom = int(obj.bbox[0]), int(obj.bbox[1]), int(obj.bbox[2]), int(obj.bbox[3])
                    label = str(obj.Id)
    
                    ########       SETTING BOX COLOR       ########
                    if obj.Id in red_marked:
                        continue
                    box_color = green if not stats[intrusion_roi['region_id']][obj.Id]['in_roi'] else red
                    if box_color == red:
                        red_marked.append(obj.Id)
    
                    ########       PUTTING CONTENT ON THE FRAME       ########
                    self.draw_curved_bbox(frame, (left, top), (right, bottom), box_color, 3, 10, 15)
                    label_size, base_line = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
                    top = max(top, label_size[1])
                    cv2.rectangle(frame, (left, top - label_size[1]), (left + label_size[0], top + int(base_line * 0.3)), (255, 255, 255), cv2.FILLED)
                    cv2.putText(frame, label, (left, top), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2)
    
                # if is_intrusion:
                rois.append(intrusion_roi['polygon'])
    
            self.draw_polygon(frame, rois, type=polygon_color)
    
            frame = cv2.resize(frame, None, fx=self.resize_factor, fy=self.resize_factor, interpolation=cv2.INTER_LINEAR)
            
            return frame

Although I have a condition that skips the frame if it is none, this error continues to occur. I tried to print the frame.shape that is corresponding to the error and I got

(1, 2, 3)

meaning it was too small to get resized but I don't know how did that happen or how to stop the frames from getting this small in the iteration!

Here is the trace of frame size before and after calling the function visualize:

Frame Shape (Before Visualizer):===> (1080, 1920, 3)
Resize factor repr ===> 0.47
Frame Shape (After Visualizer):===> (508, 902, 3)
Frame Shape (Before Visualizer):===> (1080, 1920, 3)
Resize factor repr ===> 0.47
Frame Shape (After Visualizer):===> (508, 902, 3)
Frame Shape (Before Visualizer):===> (508, 902, 3)
Resize factor repr ===> 0.47
Frame Shape (After Visualizer):===> (239, 424, 3)
Frame Shape (Before Visualizer):===> (1080, 1920, 3)
Resize factor repr ===> 0.47
Frame Shape (After Visualizer):===> (508, 902, 3)
Frame Shape (Before Visualizer):===> (239, 424, 3)
Resize factor repr ===> 0.47
Frame Shape (After Visualizer):===> (112, 199, 3)
Frame Shape (Before Visualizer):===> (1080, 1920, 3)
Resize factor repr ===> 0.47
Frame Shape (After Visualizer):===> (508, 902, 3)
Frame Shape (Before Visualizer):===> (112, 199, 3)
Resize factor repr ===> 0.47
Frame Shape (After Visualizer):===> (53, 94, 3)
Frame Shape (Before Visualizer):===> (1080, 1920, 3)
Resize factor repr ===> 0.47
Frame Shape (After Visualizer):===> (508, 902, 3)
Frame Shape (Before Visualizer):===> (53, 94, 3)
Resize factor repr ===> 0.47
Frame Shape (After Visualizer):===> (25, 44, 3)
Frame Shape (Before Visualizer):===> (1080, 1920, 3)
Resize factor repr ===> 0.47
Frame Shape (After Visualizer):===> (508, 902, 3)
Frame Shape (Before Visualizer):===> (25, 44, 3)
Resize factor repr ===> 0.47
Frame Shape (After Visualizer):===> (12, 21, 3)
Frame Shape (Before Visualizer):===> (1080, 1920, 3)
Resize factor repr ===> 0.47
Frame Shape (After Visualizer):===> (508, 902, 3)
Frame Shape (Before Visualizer):===> (12, 21, 3)
Resize factor repr ===> 0.47
Frame Shape (After Visualizer):===> (6, 10, 3)
Frame Shape (Before Visualizer):===> (1080, 1920, 3)
Resize factor repr ===> 0.47
Frame Shape (After Visualizer):===> (508, 902, 3)
Frame Shape (Before Visualizer):===> (6, 10, 3)
Resize factor repr ===> 0.47
Frame Shape (After Visualizer):===> (3, 5, 3)
Frame Shape (Before Visualizer):===> (1080, 1920, 3)
Resize factor repr ===> 0.47
Frame Shape (After Visualizer):===> (508, 902, 3)
Frame Shape (Before Visualizer):===> (3, 5, 3)
Resize factor repr ===> 0.47
Frame Shape (After Visualizer):===> (1, 2, 3)
Frame Shape (Before Visualizer):===> (1080, 1920, 3)
Resize factor repr ===> 0.47
Frame Shape (After Visualizer):===> (508, 902, 3)
Frame Shape (Before Visualizer):===> (1, 2, 3)

The ideal approach is that it always be 1080x1920 before, because I always get the freshest frame from the live stream, and always be 508x902 after so I send it and display it in the gui.

Thank you in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source