用Python中的Watchdog处理图像

用Python中的Watchdog处理图像,python,watchdog,simplecv,python-watchdog,Python,Watchdog,Simplecv,Python Watchdog,我正在使用看门狗库来检测在特定文件夹中创建新图像的时间。当watchdog检测到新创建的图像时,我使用SimpleCV/OpenCV启动一些图像处理功能 然而,这些照片是从Raspberry Pi相机拍摄的,根据下面的错误,我不相信当它第一次出现在目录中时,整个图像都被保存了。(基本上,文件是以“片断”或多种形式保存的) 请注意当我将图像复制并粘贴到相应文件夹中时,脚本会成功运行 询问:是否有办法仅在保存整个文件后启动图像处理 import time from SimpleCV import I

我正在使用看门狗库来检测在特定文件夹中创建新图像的时间。当watchdog检测到新创建的图像时,我使用SimpleCV/OpenCV启动一些图像处理功能

然而,这些照片是从Raspberry Pi相机拍摄的,根据下面的错误,我不相信当它第一次出现在目录中时,整个图像都被保存了。(基本上,文件是以“片断”或多种形式保存的)

请注意当我将图像复制并粘贴到相应文件夹中时,脚本会成功运行

询问:是否有办法仅在保存整个文件后启动图像处理

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~'

请注意,我正在使用以下Pi命令捕获图像:
raspistill-o'test.jpg'

虽然可能无法确定,但如果在不修改写入程序的情况下完成文件写入,则可以监视文件大小:

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

您可能希望通过适当的线程来执行此操作

,或者,您可以尝试一下。。。如果你不在乎其他的错误。