Opencv imshow() freezes when updating

后端 未结 9 2352
Happy的楠姐
Happy的楠姐 2020-12-09 05:18

For my image processing algorithm I\'m using python / OpenCV. The output of my algorithm shall be updated im the same window over and over again.

However sometimes

9条回答
  •  醉话见心
    2020-12-09 05:58

    You can use while loop to take burst images without freezing. Here is an example for taking 10 images. You can also try to increase waitkey number and sleep time in while loop. This work for me.

    key = cv2.waitKey(1)
    webcam = cv2.VideoCapture(0)
    sleep(1)
    
    while True:
    
        try:
            check, frame = webcam.read()
            cv2.imshow("Capturing", frame)
            key = cv2.waitKey(1)
    
            img_counter = 0
    
            if key & 0xFF == ord('s'): #press s to take images
                while img_counter < 10:
                    check, frame = webcam.read()
                    cv2.imshow("Capturing", frame)
                    key = cv2.waitKey(1)
                    path = 'F:/Projects/' #folder path to save burst images
                    img_name = "burst_{}.png".format(img_counter)
                    cv2.imwrite(os.path.join(path, img_name), img=frame)
                    print("Processing image...")
                    img_ = cv2.imread(img_name, cv2.IMREAD_ANYCOLOR) #save as RGB color format
                    print("{} written!".format(img_name))
                    img_counter += 1
                    sleep(0.2)
                webcam.release()
                cv2.destroyAllWindows()
                break
    
            elif key == ord('q'): #press q to quit without taking images
                webcam.release()
                cv2.destroyAllWindows()
                break
    
        except(KeyboardInterrupt):
            print("Turning off camera.")
            webcam.release()
            print("Camera off.")
            print("Program ended.")
            cv2.destroyAllWindows()
            break
    

提交回复
热议问题