-1

I'm new here, this is my first post. I'm having a problem in VideoCapture: I want the names of the pictures i take change automaticaly in a "for" function and to save the pictures in a directory of my choice, but i couldn't figure out what i have to do and didn't find it on internet. If anyone knows, please, help me. Here is an example of the command lines:

from VideoCapture import Device
cam = Device()
cam.saveSnapshot('here i want to call a variable, but i don't know how.jpg')

So this is it. I also don't know where i have to write the directory.

Thanks

4

3 回答 3

0

In C++ you'd usually you'd put together you string with an ostringstream, something like this:

std::ostringstream filename;

for (int i=0; i<10; i++) {
    filename << "variable: " << i << ".jpg";
    cam.saveSnapshot(filename.str().c_str());
}

It's not entirely clear what parts you're doing with C++ (if any) and what with Python though...

于 2012-05-26T01:02:59.467 回答
0

saveSnapshot()方法采用文件名,因为它是第一个非self参数。
参考:http: //videocapture.sourceforge.net/html/VideoCapture.html

这意味着您可以执行以下操作:

import os
from VideoCapture import Device

save_dir = '/path/to/my/img/dir' # or for windows: `c:/path/to/my/img/dir` 
img_file_name = 'an_image.jpg'

cam = Device()
cam.saveSnapshot( os.path.join(save_dir, img_file_name) )
于 2012-05-26T01:19:55.280 回答
0

好的,这很容易。

from VideoCapture import Device
from os.path import join, exists
# We need to import these two functions so that we can determine if
# a file exists.

import time
# From your question I'm implying that you want to execute the snapshot
# every few seconds or minutes so we need the time.sleep function.

cam = Device()

# First since you want to set a variable to hold the directory into which we
# will be saving the snapshots.
snapshotDirectory = "/tmp/" # This assumes your on linux, change it to any 
# directory which exists.

# I'm going to use a while loop because it's easier...
# initialize a counter for image1.jpg, image2.jpg, etc...
counter = 0

# Set an amount of time to sleep!
SLEEP_INTERVAL = 60 * 5 # 5 minutes!

while True:

    snapshotFile = join(snapshotDirectory, "image%i.jpg" % counter)
    # This creates a string with the path "/tmp/image0.jpg"
    if not exists(snapshotFile):
        cam.saveSnapshot(snapshotFile)
        time.sleep(SNAPSHOT_INTERVAL)
    # if the snapshot file does not exist then create a new snapshot and sleep
    # for a few minutes.

    #finally increment the counter.
    counter = counter + 1

就是这样,那应该完全符合您的要求。

于 2012-05-26T04:45:40.840 回答