在Python中使用Watchdog处理图像
我正在使用watchdog这个库来监测一个特定文件夹里新图片的生成。当watchdog发现有新图片创建时,我就会启动一些图像处理的功能,使用SimpleCV或OpenCV。
不过,这些图片是通过树莓派相机拍摄的。从下面的错误信息来看,我觉得在图片刚出现在文件夹时,整个图片并没有完全保存下来。(简单来说,文件是分成“几块”保存的,或者说是多种格式的。)
请注意,当我复制和粘贴一张图片到相应的文件夹时,脚本能够顺利运行。
问题:有没有办法确保只有在文件完全保存后再开始图像处理?
import time
from SimpleCV import Image
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class ExampleHandler(FileSystemEventHandler):
def on_created(self, event):
image = Image(event.src_path)
do image processing stuff
print "Got event for file %s" % event.src_path
observer = Observer()
event_handler = ExampleHandler() # create event handler
observer.schedule(event_handler, path='/path/to/images') # set observer to use created handler in directory
observer.start()
# sleep until keyboard interrupt, then stop + rejoin the observer
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
错误信息:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "/usr/local/lib/python2.7/dist-packages/watchdog/observers/api.py", line 241, in run
self.dispatch_events(self.event_queue, self.timeout)
File "/usr/local/lib/python2.7/dist-packages/watchdog/observers/api.py", line 408, in dispatch_events
self._dispatch_event(event, watch)
File "/usr/local/lib/python2.7/dist-packages/watchdog/observers/api.py", line 403, in _dispatch_event
handler.dispatch(event)
File "/usr/local/lib/python2.7/dist-packages/watchdog/events.py", line 361, in dispatch
_method_map[event_type](event)
File "picture_classifier_cube.py", line 11, in on_created
image = Image(event.src_path)
File "/usr/local/lib/python2.7/dist-packages/SimpleCV/ImageClass.py", line 1073, in __init__
self._pil = pil.open(self.filename).convert("RGB")
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1980, in open
raise IOError("cannot identify image file")
IOError: cannot identify image file
编辑 在更新处理程序后,让它在识别到新文件后暂停几秒钟,我收到了不同的错误。
class ExampleHandler(FileSystemEventHandler):
def on_created(self, event):
time.sleep(3)
cans = Image(event.src_path)
错误
IOError: [Errno 2] No such file or directory: '/path/to/images/test.jpg~'
请注意,我是用以下命令来捕捉图片的:raspistill -o 'test.jpg'
1 个回答
1
虽然没有办法绝对确定一个文件是否已经写完,而不去修改写入这个文件的程序,但你可以通过监控文件的大小来判断:
from os import stat
from time import sleep
def wait_for_write_finish( filename ):
last_size, size= -1, 0
while size!=last_size:
sleep(1)
last_size, size= size, stat(filename).st_size
你可能需要使用合适的线程来实现这个功能。