我有一个获取图像并为其提供文件名的功能。该功能是获取多个图像文件的循环的一部分:
def cameracycle(self):
print "cam cycle start"
self.capture(30)
print "cam cycle sleep"
sleep(270)
print "cam cycle second capture"
self.capture(30)
def capture (self,length):
self.camera.start_recording('1.h264')
self.camera.wait_recording(length)
self.camera.stop_recording()
每次调用'capture'时,程序都会写入文件“1.h264”。如何编写它以便文件名递增?
谢谢!
答案 0 :(得分:1)
您可以使用全局变量video_count
来跟踪下一个数字。
video_count = 0
def capture (self,length):
video_count+=1
self.camera.start_recording(str(video_count) + '.h264')
self.camera.wait_recording(length)
self.camera.stop_recording()
你可能会觉得把它作为函数的一个属性更合适,如下所示:
def capture (self,length):
capture.video_count+=1
self.camera.start_recording(str(video_count) + '.h264')
self.camera.wait_recording(length)
self.camera.stop_recording()
capture.video_count = 0
答案 1 :(得分:1)
您可以使用全局变量:
currentid = 0
def capture(self, length):
global currentid
currentid += 1
self.camera.start_recording(str(currentid) + '.h264')
...
答案 2 :(得分:0)
您可以通过以下方式为您的班级添加捕获计数器:
class MyClassName(object):
counter = 0
def cameracycle(self):
print "cam cycle start"
self.capture(30)
print "cam cycle sleep"
sleep(270)
print "cam cycle second capture"
self.capture(30)
def capture (self,length):
MyClassName.counter += 1
self.camera.start_recording('{}.h264'.format(MyClassName.counter))
self.camera.wait_recording(length)
self.camera.stop_recording()