VideoCapture 不播放视频

VideoCapture does not play the video

我遇到了 VideoCapture 问题,我流式传输的视频仅显示第一帧。在下面的代码中,我将视频叠加在对象检测产生的边界框之上:

if view_img:
            ####video_name is a path to my video 
            img = cv2.VideoCapture(video_name)
            ret_video, frame_video = img.read()
            if not ret_video: ######so that the video can be played in a loop
                img = cv2.VideoCapture(video_name)
                ret_video, frame_video = img.read()
            
            ###here I look for the bounding boxes and superimpose the video 
            hsv = cv2.cvtColor(im0, cv2.COLOR_BGR2HSV)
            mask = cv2.inRange(hsv, (0, 120, 120), (10, 255, 255))#(110, 120, 120), (130, 255, 255))#<- blue # RED: (0, 120, 120), (10, 255, 255)) 
            thresh = cv2.dilate(mask, None, iterations=2)
            contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
            #contours = contours[0]
            contours = imutils.grab_contours(contours)
            #frame_counter = 0
            for contour in contours:
                if cv2.contourArea(contour) < 750:
                    continue
                (x, y, w, h) = cv2.boundingRect(contour)                                        
                height = 480
                width = 640                                     
                if y + h < height and x + w < width:                                            
                        logo = cv2.resize(frame_video, (w, h))###frame_video is the frame from the video which I superimpose
                        img2gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY)
                        _, logo_mask = cv2.threshold(img2gray, 1, 255, cv2.THRESH_BINARY)
                        roi = im0[y:y+h, x:x+w]
                        roi[np.where(logo_mask)] = 0
                        roi += logo            
        cv2.imshow(str(p), im0)# im0 is the webcam frame         
        cv2.waitKey(25)

当我 运行 这段代码时会发生什么,它不是在网络摄像头帧的顶部显示整个视频,而是只显示该视频的第一帧。

叠加视频在另一个脚本中工作正常,修改原始:source

我认为这个问题与叠加视频的waitKey()有关,因为它没有指定。

如果我尝试使用 while (cap.isopened()):while (True) 初始化视频,则程序会冻结并且根本没有输出。

每个设备源

cv2.VideoCapture 应该 运行 一次。使用 while loop 应该排除 cv2.VideoCapture (在循环外初始化它)。它挂在 while loop 上的原因是因为您多次打开同一设备的连接而没有关闭它。

我没有测试过。你只需这样做:Btwe

if view_img:
            ####video_name is a path to my video 
            img = cv2.VideoCapture(video_name)
            while img.isOpened():
                ret_video, frame_video = img.read()
                if not ret_video: ######so that the video can be played in a loop
                    break
                
                ###here I look for the bounding boxes and superimpose the video 
                hsv = cv2.cvtColor(im0, cv2.COLOR_BGR2HSV)
                mask = cv2.inRange(hsv, (0, 120, 120), (10, 255, 255))#(110, 120, 120), (130, 255, 255))#<- blue # RED: (0, 120, 120), (10, 255, 255)) 
                thresh = cv2.dilate(mask, None, iterations=2)
                contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
                #contours = contours[0]
                contours = imutils.grab_contours(contours)
                #frame_counter = 0
                for contour in contours:
                    if cv2.contourArea(contour) < 750:
                        continue
                    (x, y, w, h) = cv2.boundingRect(contour)                                        
                    height = 480
                    width = 640                                     
                    if y + h < height and x + w < width:                                            
                            logo = cv2.resize(frame_video, (w, h))###frame_video is the frame from the video which I superimpose
                            img2gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY)
                            _, logo_mask = cv2.threshold(img2gray, 1, 255, cv2.THRESH_BINARY)
                            roi = im0[y:y+h, x:x+w]
                            roi[np.where(logo_mask)] = 0
                            roi += logo            
                cv2.imshow(str(p), im0)# im0 is the webcam frame         
                cv2.waitKey(25)

顺便说一句,如果您使用的是 OpenCV4.5.5。您可能需要添加:

ret,contours = cv2.findContours