我正在嘗試根據我的需要調整這個答案。我正在嘗試撰寫一個小程式來操作一些實驗室設備,而不是預先錄制的視頻,我想顯示相機的輸出。該部分使用以下代碼運行良好:
import numpy as np
import pandas as pd
import holoviews as hv
hv.extension('bokeh')
from holoviews.streams import Pipe, Buffer
from tornado.ioloop import IOLoop
from tornado import gen
import cv2
from instrumental.drivers.cameras import uc480
instruments = uc480.list_instruments()
@gen.coroutine
def f():
#async def f():
while cam.is_open:
frame = cam.grab_image(timeout='10s', copy=True, exposure_time='10ms')
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
yield pipe.send(rgb)
#await pipe.send(rgb)
cv2.destroyAllWindows()
cam = uc480.UC480_Camera(instruments[0],reopen_policy='reuse')
cam.start_live_video(framerate = "10Hz")
frame0 = cam.grab_image(timeout='10s', copy=True, exposure_time='10ms')
rgb = cv2.cvtColor(frame0, cv2.COLOR_BGR2RGBA)
pipe = Pipe(data=rgb)
#camera_task = asyncio.gather(f())#doesn't work?
camera_loop = IOLoop.current().add_callback(f)
hv.DynamicMap(hv.RGB, streams=[pipe])
除了撰寫小腳本之外,我幾乎沒有什么經驗,所以我選擇使用Panel來制作我的簡單 UI,并asyncio讓一切順利運行。我開始修改代碼以更好地理解它,但到目前為止我失敗了。我的問題如下:
- 據我所知,可能不需要龍卷風,因為
asyncio它提供了相似/相同的功能。我非常想只asyncio在可能的情況下使用,或者在這種情況下龍卷風會增加任何實質性的東西嗎? - 根據檔案,我應該能夠用我已經知道的and替換
@gen.coroutine裝飾器和yield關鍵字,但是這樣做時,回圈永遠不會開始。如何以正確的方式啟動回圈?asyncawaitasyncio - 最后,如果需要龍卷風,在這個例子中如何停止回圈運行?在
asyncio我只是.cancel()任務,但在這種情況下沒有作業。
編輯:更多資訊:
- 現在,在開發程序中,我正在 Jupyter 筆記本中運行代碼。
- 完成后(或測驗時),我運行代碼
panel serve(如果我理解正確,它將在瀏覽器選項卡中顯示所有內容,在后臺運行龍卷風服務器) - 我的想法是使用相機在樣品表面上成像激光點。
- 我想實時使用相機,以便能夠通過肉眼檢查激光是否在焦點上。--- 我的問題和疑問是關于這一步的,因為我沒有異步(?)編程的經驗(比沒有 UI 的簡單腳本更復雜)。這樣做的標準方法是什么?
- 然后,我還將使用一次性影像來提取和處理資料(例如,使用全息視圖的激光輪廓的橫截面等。)---這已經奏效了。
uj5u.com熱心網友回復:
如果您只想查看相機,我認為您最好放棄tornado 和Panel,只運行一個小型Web 服務器(例如Flask)并在網頁中流式傳輸視頻。聽起來您實際上并不想使用 Panel 或 Holoview 的任何功能。
但是,既然您問了,這就是您可以使用 asyncio 代替 tornado 來顯示 mp4 視頻的方式
import asyncio
import cv2
import holoviews as hv
from holoviews.streams import Pipe
hv.extension("bokeh")
# Use a video file for testing
from pathlib import Path
video_path = Path("path/to/a/file.mp3")
async def f():
vd = cv2.VideoCapture(str(video_path))
frames = int(vd.get(cv2.CAP_PROP_FRAME_COUNT))
while frames > 0:
ret, frame = vd.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
pipe.send(rgb)
frames -= 1
await asyncio.sleep(1)
vd.release()
cv2.destroyAllWindows()
pipe = Pipe(data=[])
所以我想你可以為你的網路攝像頭做非常相似的事情。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/517756.html
上一篇:blitopencvcameracapturewithpygamethrowsTypeError:argument1mustbepygame.Surface,notcv2.VideoCapture
