How can I take multiple pictures with one button press?

asked 2018-07-27 14:11:35 -0600

accdev gravatar image

I would like to have a "burst" effect and take multiple pictures when the user presses a button once. What I currently have is the picture function in a loop, which requires the user to press a button for every picture, which also closes and reopens the camera feed.

pictures = 4
for i in range(pictures):
    camera = cv2.VideoCapture(1) #if there are two cameras, usually this is the front facing one
    if camera.read() == (False,None):
        camera= cv2.VideoCapture(0) 
    else:
        pass
    while True:
    return_value,image = camera.read()
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    cv2.imshow('image',gray)

    if cv2.waitKey(1)& 0xFF == ord('s'): #take a screenshot if 's' is pressed
        cv2.imwrite('burst'+str(i)+'.png',image) #save screenshot as test.jpg
        break

camera.release()
cv2.destroyAllWindows()

To get a better burst effect, I changed the code to include time.sleep functions between when imwrite is being used. However, when I use this code, all three pictures are exactly the same. I can see that after I press 's' the camera feed is open, but the frame freezes. I don't know how to keep the frame live so that all the burst pictures are actually different.

 camera = cv2.VideoCapture(1)
 if camera.read() == (False,None):
     camera= cv2.VideoCapture(0)
 else:
      pass
 while True:
         return_value,image = camera.read()
         gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
         cv2.imshow('image',gray)

         if cv2.waitKey(1)& 0xFF == ord('s'): #take a screenshot if 's' is pressed
             cv2.imwrite('burst.png,image) 
             time.sleep(1)
             cv2.imwrite('burst1.png',image)
             time.sleep(1)
             cv2.imwrite('burst2.png',image)
             break

 camera.release()
 cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

1

Check return_value after each camera.read and try

       cv2.imwrite('burst.png',image) 
       return_value,image = camera.read()
       cv2.imwrite('burst1.png',image)
       return_value,image = camera.read()
       cv2.imwrite('burst2.png',image)
LBerger gravatar imageLBerger ( 2018-07-28 00:03:38 -0600 )edit

Works perfect, thanks!

accdev gravatar imageaccdev ( 2018-07-30 10:22:56 -0600 )edit