6

我正在使用 python 的 picamera 模块捕捉延时摄影:

from picamera import PiCamera, Color
class EggAlertCam:

    def __init__(self):
        self.camera = PiCamera()
        self.camera.start_preview()

    def capture_thread_task(self):

        filename = os.path.join(self.SAVEPATH, self.safe_camname + '_{timestamp:%Y%m%d-%H%M%S}-{counter:03d}' + '.jpg' )
        starttime = dt.datetime.now()
        current_time = dt.datetime.now()

        for filename in self.camera.capture_continuous(filename):
            print('Captured {}'.format(filename))
            self.wait(current_time)
            current_time = dt.datetime.now()

    def get_frame(self, size=None):
        if size is None:
            size = (640,480)
        output = io.BytesIO()
        self.camera.capture(output, format='jpeg', resize=size, use_video_port=True)
        output.seek(0)
        return output

capture_thread_task在一个线程中运行,我有一个 Flask 应用程序,允许通过调用get_frame.

我正在使用video_port=True,希望即使在捕捉延时摄影时也能获取(更高的噪音,但我不在乎)图像,但我得到了一个例外:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/dist-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/local/lib/python3.7/dist-packages/xxx/webapp.py", line 53, in get_frame
    jpg = scheduler.cam.get_frame(size=size)
  File "/usr/local/lib/python3.7/dist-packages/xxx/xxx.py", line 124, in get_frame
    self.camera.capture(output, format='jpeg', resize=size, use_video_port=True)
  File "/usr/local/lib/python3.7/dist-packages/picamera/camera.py", line 1409, in capture
    camera_port, output_port = self._get_ports(use_video_port, splitter_port)
  File "/usr/local/lib/python3.7/dist-packages/picamera/camera.py", line 562, in _get_ports
    'The camera is already using port %d ' % splitter_port)
picamera.exc.PiCameraAlreadyRecording: The camera is already using port 0

我以为我会使用另一个端口进行捕获,但可惜它似乎不起作用。

从这里的文档:https ://picamera.readthedocs.io/en/release-1.10/fov.html?highlight= splitter#under-the-hood 我认为 capture_continuous 函数使用端口 0,我应该能够使用端口 1,这是在告诉时使用的端口use_video_port=True。但是异常消息告诉我它仍然想使用端口 0..

编辑:我尝试使用分离器端口 2,对于我的一些 Pi 似乎可以工作,而对于其他人来说,端口 2 似乎已使用。几分钟后,端口开始可用,我可以从中获取帧。

这看起来像一个需要很长时间的捕获,但我不知道如何正确地要求端口 2 释放自己以进行新的捕获。

我究竟做错了什么 ?

4

1 回答 1

0

您在哪里声明 EggAlertCam 对象 - 在视图方法内部还是外部?

在外面尝试或使用单例。
或者你可以在单独的线程/进程中运行它,然后从 Flask 向它发送命令。

于 2020-02-14T21:13:59.750 回答