Reputation: 793
I would like to create a program which saves .jpg images taken from the webcam(frames). What my program do for now is, opening a webcam, taking one and only one frame, and then everything stops.
What i would like to have is more than one frame My error-code is this one:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
count = 0
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imwrite("frame%d.jpg" % ret, frame) # save frame as JPEG file
count +=1
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(10):
break
Upvotes: 6
Views: 23075
Reputation: 71
Though this is late I have to say that prtkp's answer is what you needed... the , ret value you use to enumerate your images is wrong. Ret is only a boolean... so while it detects the image it its placing a one there for the name of the image...
I just used this... with a c=0 on the header
cv2.imwrite("img/frame %d.jpg" % c,img)
c=c+1
Upvotes: 1
Reputation: 570
use this -
count = 0
cv2.imwrite("frame%d.jpg" % count, frame)
count = count+1
Upvotes: 5
Reputation: 1053
Actually it sounds like you are always saving your image with the same name because you are concatenating ret instead of count in the imwrite method
try this :
name = "frame%d.jpg"%count
cv2.imwrite(name, frame) # save frame as JPEG file
Upvotes: 10
Reputation: 9379
When no key is pressed and the time delay expires, cv2.waitKey
returns -1
. You can check it in the doc.
Basically, all you have to do is changing slightly the end of your program:
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(10) != -1:
break
Upvotes: 2