Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm using OpenCV and Python to take images. However currently I can only take one picture at a time. I would like to have OpenCV to take multiple pictures. This is my current code.

import cv2.cv as cv
import time

cv.NamedWindow("camera", 1)

capture = cv.CaptureFromCAM(0)

while True:
    img = cv.QueryFrame(capture)
    cv.ShowImage("camera", img)
    cv.SaveImage('pic.jpg', img)
    if cv.WaitKey(10) == 27:
        break 
share|improve this question
up vote 3 down vote accepted

Your code overwrite a file. Save to different file each time. For example:

import cv2.cv as cv
import time

cv.NamedWindow("camera", 1)

capture = cv.CaptureFromCAM(0)

i = 0
while True:
    img = cv.QueryFrame(capture)
    cv.ShowImage("camera", img)
    cv.SaveImage('pic{:>05}.jpg'.format(i), img)
    if cv.WaitKey(10) == 27:
        break
    i += 1
share|improve this answer
    
This is perfect, but when I run it, it keeps on taking photos. Is there anyway to have it only take 5 pictures? – ng150716 Jun 15 '13 at 19:05
1  
Then, instead of While True:, use for i in range(5):. – falsetru Jun 15 '13 at 19:07
    
    
Yup, that did the trick. But it only creates 5 images, then keeps on overwriting the same 5 files over and over. – ng150716 Jun 15 '13 at 19:20
    
You asked a way to have it only take 5 pictures? in comment. – falsetru Jun 15 '13 at 19:24

change the name of the image to be saved to " [image name] [a number which increase after every loop] " By doing this your image will be stored with a new name after every loop.. otherwise all the images will overwrite the same name !

import cv2.cv as cv
import time

cv.NamedWindow("camera", 1)

capture = cv.CaptureFromCAM(0)
num = 0
while True:
    img = cv.QueryFrame(capture)
    cv.ShowImage("camera", img)
    cv.SaveImage('pic'+str(num)+'.jpg', img)
    if cv.WaitKey(10) == 27:
        break
    num += 1

now your images will be saved as pic0.jpg, pic1.jpg, pic2.jpg and so on..

share|improve this answer

A minimal example of what you'd like to do, based on the c++ binded interface.

import cv2

cpt = 0
maxFrames = 5 # if you want 5 frames only.

try:
    vidStream = cv2.VideoCapture(0) # index of your camera
except:
    print "problem opening input stream"
    sys.exit(1)

while cpt < maxFrames:
    ret, frame = vidStream.read() # read frame and return code.
    if not ret: # if return code is bad, abort.
        sys.exit(0)
    cv2.imshow("test window", frame) # show image in window
    cv2.imwrite("image%04i.jpg" %cpt, frame)
    cpt += 1

A full example of script, able to read from a camera index, or a file. Includes some failsafes and some information about read device.

usage: record.py [source] [target folder]

#!/usr/bin/env python
import cv2
import sys
import os

cpt = 0
maxFrames = 30

try:
    targetDir = sys.argv[2]
except:
    targetDir = "" # if no argument, then use current directory

try: # read input. eval if to transform video index to int
    vidStream = cv2.VideoCapture(eval(sys.argv[1]))
except:
    print "problem opening input stream"
    sys.exit(1)
if not vidStream.isOpened():
    print "capture stream not open"
    sys.exit(1)

# informations in case the input is a video file.
nFrames = vidStream.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)
print "frame number: %s" %nFrames
fps = vidStream.get(cv2.cv.CV_CAP_PROP_FPS)
print "FPS value: %s" %fps

# note that we could use frame number here, or "while 1"
# so we could read from a live written file or capture devide.
while cpt < maxFrames:
    ret, frame = vidStream.read() # read frame and return code.
    if not ret:
        print "end of stream"
        sys.exit(0)
    cv2.imshow("test window", frame) # show image in window
    cv2.imwrite(os.path.join(targetDir, "image_%04i.jpg" %cpt), frame)
    cpt += 1
    keyPressed = cv2.waitKey(1) # time to wait between frames
    if keyPressed != -1: # if user pressed a key, stop recording.
        sys.exit(0)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.