尝试通过按键输入更改视频帧

Trying to change Video Frame by key input

我正在尝试对每一帧执行裁剪操作,因此我希望循环中的每一帧都保持暂停状态,直到按下某个键。循环部分代码给出如下:

while (True):

    # display the image and wait for a keypress
    ret, frame = cap.read()
    if not ret:
        print ('Process completed')
        break

    clone = frame.copy()
    cv2.imshow('frame',frame)

    if len(refPt) == 2:
        roi = clone[refPt[0][1]:refPt[1][1], refPt[0][0]:refPt[1][0]]
        cv2.imshow("ROI", roi)
        cv2.imwrite('New folder\'+str(fileNum)+'.png',roi)
        fileNum += 1
        refPt.clear()
        #cv2.waitKey(0)

    key = cv2.waitKey(1) & 0xFF

    # if the 'q' key is pressed, exit from loop
    if key == ord("q"):
        break

    #if the 'n' key is pressed, go to next frame
    if key == ord("n"):
        continue

当视频进入循环时,它不会停止并等待 'n' 按键改变,而是帧会快速传递,直到我单击并拖动激活区域选择的某个点部分。

我觉得我的 while 条件不正确。请帮我解决 while 条件。

这条命令只等待1ms然后继续

key = cv2.waitKey(1) & 0xFF 

尝试将其更改为

key = cv2.waitKey(0) & 0xFF

如果 cv window 可用,这应该等到按下某个键。