Ask Your Question
0

closing video with any key

asked 2020-07-06 13:45:47 -0600

Hi, I want to close the video frame with any key from the keyboard. I have tried with waitKey(0), but it just displays an image. I want to stream the video from webcam and want to close the streaming when I press any key. Is there any better way than passing all the keys at ord()?

 # Press 'q' to quit
key = cv2.waitKey(1) & 0xFF
    # if the `q` key was pressed, break from the loop
if key == ord('q'):
    break
edit retag flag offensive close merge delete

Comments

try

if key > -1:
    break
sturkmen gravatar imagesturkmen ( 2020-07-06 13:48:30 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-07-07 04:27:25 -0600

supra56 gravatar image

I tested @sturkmen . But doesn't solved problem. I solved problem to fix. You can used both click or press any key to stop.

#!/usr/bin/python37
#OpenCV 4.3.0, Raspberry pi3B/+, 4b/4g/8g, Thonny 3.7.3
#Date: 7th July, 2020   
import cv2

clicked = False
def onMouse(event, x, y, flags, param):
    global clicked
    if event == cv2.EVENT_LBUTTONUP:
        clicked = True

cameraCapture = cv2.VideoCapture(0)
cv2.namedWindow('MyWindow')
cv2.setMouseCallback('MyWindow', onMouse)

print('Showing camera feed. Click window or press any key to stop.')
success, frame = cameraCapture.read()
while cv2.waitKey(1) is -1 and not clicked:
    if frame is not None:
        cv2.imshow('MyWindow', frame)
    success, frame = cameraCapture.read()

cv2.destroyWindow('MyWindow')
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-07-06 13:45:47 -0600

Seen: 7,586 times

Last updated: Jul 07 '20