在opencv中保存视频,按q后我的程序没有终止

Posted

技术标签:

【中文标题】在opencv中保存视频,按q后我的程序没有终止【英文标题】:save video in opencv and my program is not terminating after pressing q 【发布时间】:2020-09-03 07:12:36 【问题描述】:
import numpy as np
import cv2
import time
import dlib

cap = cv2.VideoCapture(0) #initialize video capture
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)  # float
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)  # float
fourcc = cv2.VideoWriter_fourcc(*"XVID") 
out = cv2.VideoWriter('test1h1.avi', fourcc, 30.0, (640,480), 0)

left_counter=0           #counter for left movement
right_counter=0              #counter for right movement

th_value=5   #changeable threshold value 

def thresholding( value ):  # function to threshold and give either left or right
    global left_counter
    global right_counter

    if (value<=54):   #check the parameter is less than equal or greater than range to 
    left_counter=left_counter+1        #increment left counter 

    if (left_counter>th_value):  #if left counter is greater than threshold value 
        print ('RIGHT')  #the eye is left
        left_counter=0   #reset the counter

    elif(value>=54):  # same procedure for right eye
        right_counter=right_counter+1

    if(right_counter>th_value):
        print ('LEFT')
        right_counter=0
while 1:
    ret, frame = cap.read()
    cv2.line(frame, (320,0), (320,480), (0,200,0), 2)
    cv2.line(frame, (0,200), (640,200), (0,200,0), 2)
    if ret==True:
    col=frame

    frame =cv2.cvtColor(frame,cv2.COLOR_RGB2GRAY)
    #cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    faces = detector(frame)
    for face in faces:
        x1 = face.left()
        y1 = face.top()
        x2 = face.right()
        y2 = face.bottom()


        landmarks = predictor(frame, face)

        for n in range(36, 48):
            x = landmarks.part(n).x
            y = landmarks.part(n).y
            cv2.circle(frame, (x, y), 2, (255, 0, 0), -1)

        pupilFrame=frame
        clahe=frame
        blur=frame
        edges=frame
        eyes = cv2.CascadeClassifier('haarcascade_eye.xml')
        detected = eyes.detectMultiScale(frame, 1.3, 5)
        for (x,y,w,h) in detected: #similar to face detection
            cv2.rectangle(frame, (x,y), ((x+w),(y+h)), (0,0,255),1)     #draw rectangle around eyes
            cv2.line(frame, (x,y), ((x+w,y+h)), (0,0,255),1)   #draw cross
            cv2.line(frame, (x+w,y), ((x,y+h)), (0,0,255),1)
            pupilFrame = cv2.equalizeHist(frame[y+int(h*.25):(y+h), x:(x+w)]) #using histogram 
 equalization of better image. 
            cl1 = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)) #set grid size
            clahe = cl1.apply(pupilFrame)  #clahe
            blur = cv2.medianBlur(clahe, 7)  #median blur
            circles = cv2.HoughCircles(blur 
,cv2.HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=7,maxRadius=21) #houghcircles
            if circles is not None: #if atleast 1 is detected
                circles = np.round(circles[0, :]).astype("int") #change float to integer
                print ('integer'),circles
                for (x,y,r) in circles:
                    cv2.circle(pupilFrame, (x, y), r, (0, 255, 255), 2)
                    cv2.rectangle(pupilFrame, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
                    #set thresholds
                    thresholding(x)
                    print(height,width)





        cv2.imshow("Frame", frame)            
        cv2.imshow('image',pupilFrame)
        cv2.imshow('clahe', clahe)
        cv2.imshow('blur', blur)
        #out.write(pupilFrame)
        out.write(clahe)
        #out.write(blur)


        if cv2.waitKey(10) & 0xFF == ord('q'):
                break

cap.release()
out.release()
cv2.destroyAllWindows()

我想保存出现在 clahe 和 blur 中的唯一眼睛部分的记录。当我运行此代码时,保存的视频已损坏。如果我使用 out.write(pupilFrame) 则记录我不想要的原始帧大小的视频。另外,按 q 后,我的程序没有终止。

【问题讨论】:

clahe 的形状是什么? 为什么需要这样做?我必须保存瞳孔框架的视频,我的意思不是整个捕获区域,而是仅眼睛部分。 只是因为输出视频大小可能不等于clahe大小。 我试过了..而不是 480*640 我使用了 clahe 的形状 (46,61)....我没有工作..视频已损坏 【参考方案1】:

命令的位置在 Python 中至关重要。你的 if waitKey(0) == ord('q') 应该向左移动一个位置

【讨论】:

以上是关于在opencv中保存视频,按q后我的程序没有终止的主要内容,如果未能解决你的问题,请参考以下文章

如何从图像集合中保存视频文件?

如何在核心数据中保存单个实体

在 mp4 容器中保存视频时 FPS 太高

Agora 云录制未在 azure 中保存视频

通过共享扩展从图库中保存照片时 NSData WriteToFile 失败

在Flutter中保存照片(尤其是相机胶卷)