Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I noticed that you left out semi-colon in line 3. Also your're missing Trap/except. When you click button s, you didn't called method method_webcam_trigger(). As @berak said, by removing release was wrong. I also added waitKey for 10 seconds. But remember, by clicking button s, you saved filename saved_img.jpg. When you clicking buitton again, it asking you to save as. So click filename, you will add index...saved_img1.jpg, and so on.

#!/usr/bin/env python37
#Raspberry pi 3/4, OpenCV 4.1.2
#Date: December 2nd, 2019

import cv2

def method_webcam_trigger():
    key = cv2. waitKey(10)
    webcam = cv2.VideoCapture(0)
    while True:
        try:
            check, frame = webcam.read()
            cv2.imshow("Capturing", frame)
            key = cv2.waitKey(1)
            if key == ord('s'): 
                cv2.imwrite(filename='saved_img.jpg', img=frame)
                print("Image saved!")
                webcam.release()
                cv2.destroyAllWindows()
                cv2.waitKey(10)
                method_webcam_trigger()
                webcam.release()
                cv2.destroyAllWindows()
                cv2.waitKey(10)               
                break                
            elif key == ord('q'):                 
                webcam.release()
                cv2.destroyAllWindows()
                break 
        except:
              pass

method_webcam_trigger()