Breaking out nested loop

I’m trying to break out double loops in OpenCV, but it crashes every time I do so. First loop goes through the whole video files, second loop goes through a single video and performs motion detection. ‘q’ key for breaking out a single video, ‘w’ key for breaking out from the whole loop.

I tried first method by setting up variable break flag:

sub_type = 'MOG2' # 'KNN'


if sub_type == 'MOG2':
    backSub = cv2.createBackgroundSubtractorMOG2(varThreshold=16, detectShadows=False)
else:
    backSub = cv2.createBackgroundSubtractorKNN(dist2Threshold=1000, detectShadows=False)

thresh = 500

break_out_var = False
for video in video_files:

    cap = cv2.VideoCapture(video)
    
    while(cap.isOpened()):
        
        ret, frame = cap.read()
        if ret == True:
          
            height, width, layers = frame.shape
            new_h = height / 2
            new_w = width / 2
            frame = cv2.resize(frame, (int(new_w), int(new_h)))
            fgMask = backSub.apply(frame)
            motion_mask = get_motion_mask(fgMask, min_thresh = 30)
    
            contours, _ = cv2.findContours(motion_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)
            
            for cnt in contours:
                x,y,w,h = cv2.boundingRect(cnt)
                area = w*h
                if area > thresh:
                    frame = cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), thickness = 2)
    
            cv2.imshow('normal video',frame)
            cv2.imshow('fg_mask',motion_mask)
    
            k = cv2.waitKey(30) & 0xFF
            if k  == ord('q'):
                break
            elif k == ord('w'):
                break_out_var = True
                break
    
        else:
            break

    if (break_out_var):
        break
    
    cap.release()
    
    cv2.destroyAllWindows()

but it crashed.

then I tried putting whole thing into function and return, but it crashed again:

sub_type = 'MOG2' # 'KNN'


if sub_type == 'MOG2':
    backSub = cv2.createBackgroundSubtractorMOG2(varThreshold=16, detectShadows=False)
else:
    backSub = cv2.createBackgroundSubtractorKNN(dist2Threshold=1000, detectShadows=False)

thresh = 400

def detect(video_files):

    for video in video_files:
    
        cap = cv2.VideoCapture(video)
        
        while(cap.isOpened()):
            
            ret, frame = cap.read()
            if ret == True:
              
                height, width, layers = frame.shape
                new_h = height / 2
                new_w = width / 2
                frame = cv2.resize(frame, (int(new_w), int(new_h)))
                fgMask = backSub.apply(frame)
                motion_mask = get_motion_mask(fgMask, min_thresh = 30)
        
                contours, _ = cv2.findContours(motion_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)
                
                for cnt in contours:
                    x,y,w,h = cv2.boundingRect(cnt)
                    area = w*h
                    if area > thresh:
                        frame = cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), thickness = 2)
        
                cv2.imshow('normal video',frame)
                cv2.imshow('fg_mask',motion_mask)
        
                k = cv2.waitKey(25) & 0xFF
                if k  == ord('q'):
                    break
                elif k == ord('w'):
                    return
        
            else:
                break
        
        cap.release()
        
        cv2.destroyAllWindows()

is there any way to solve this? thanks

But that’s not “in OpenCV” though, that is just Python code.

Sorry, general beginner coding questions are off-topic for this forum, even if you happen to use OpenCV.

Further, “it crashed” is insufficient. What makes you say that it “crashed”? What does that crash look like?

The rules on Stack Overflow were developed over time. There is much thought behind them. That makes them generally a good idea to pay attention to. Since you’re a beginner, part of your programming education is to learn how to debug your code, figure out what’s happening, and how to best communicate that to others. A MRE isn’t a burden, it’s helping you learn the trade.

Breaking out of multiple loops has some shortcuts in some languages. A general approach is to refactor your code into functions.