Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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')