异步网络摄像机/视频/图像/截图库。
acapture的Python项目详细描述
https://github.com/aieater/python_acapture
acapture(异步捕获python库)
说明
acapture是一个实时的python相机/视频捕获库。 当python应用程序实现视频、网络摄像机和屏幕截图捕获时,它的fps速度太慢,性能也很差。 另外,python是非事件驱动的体系结构,总是存在i/o阻塞问题,这将阻碍并行性。
acapture(asynchronuscapture)库提供异步视频/摄像机捕获实现,可以解决阻塞和性能问题。
acapture库是有用的,而不是opencv视频捕获api。
opencv有阻塞问题。
import cv2
cap = cv2.VideoCapture(0)
check,frame = cap.read() # blocking!! and depends on camera FPS.
acapture库可以解决实时应用程序中的阻塞问题。
import acapture
cap = acapture.open(0)
check,frame = cap.read() # non-blocking
另请参阅“pyglview”软件包。
由于cv2.waitkey(1),opencv3呈现程序太慢。 如果想提高性能,应该使用opencv4+或“pyglview”包。
https://github.com/aieater/python_glview.git
这个包支持最快的opengl直接查看器和opencv渲染器。 如果您的环境不支持opengl,它将自动切换到cpu渲染器(opencv),也可以使用远程桌面(xserver),如vnc。
开始
ubuntu16.04上的基本库
Library | installation |
---|---|
Camera | sudo apt install -y libv4l-dev libdc1394-22 libdc1394-22-dev v4l-utils |
Video | sudo apt install -y ffmpeg libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libfaac-dev libmp3lame-dev mplayer |
MacOSX上的基本库
Library | installation |
---|---|
Brew | /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" |
Camera | - |
Video | brew install ffmpeg mplayer |
python包依赖项
Version | Library | installation |
---|---|---|
v3.x/v4.x | OpenCV | pip3 install opencv-python |
v4.x | mss | pip3 install mss |
v1.1x.x | numpy | pip3 install numpy |
v1.9.x | pygame | pip3 install pygame |
v3.7.x | configparser | pip3 install configparser |
最后,安装孔盖。
pip3 install acapture
示例
视频流(异步)
import acapture
import cv2
cap = acapture.open("test.mp4")
while True:
check,frame = cap.read() # non-blocking
if check:
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
cv2.imshow("test",frame)
cv2.waitKey(1)
视频帧(异步)
cap = acapture.open("test.mp4",frame_capture=True)
相机流(异步)
import acapture
import cv2
cap = acapture.open(0) # /dev/video0
while True:
check,frame = cap.read() # non-blocking
if check:
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
cv2.imshow("test",frame)
cv2.waitKey(1)
截图流(同步)
import acapture
import cv2
cap = acapture.open(-1)
while True:
check,frame = cap.read() # blocking
if check:
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
cv2.imshow("test",frame)
cv2.waitKey(1)
目录图像(同步)
import acapture
import cv2
cap = acapture.open("images/")
while True:
check,frame = cap.read() # blocking
if check:
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
cv2.imshow("test",frame)
cv2.waitKey(1)
单位图像(预加载)
import acapture
import cv2
cap = acapture.open("images/test.jpg")
while True:
check,frame = cap.read()
if check:
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
cv2.imshow("test",frame)
cv2.waitKey(1)
将视频提取到jpg图像。
import acapture
acapture.extract_video2images("test.mp4",format="jpg",quality=2)
原料药
Version | Function | Required | Description |
---|---|---|---|
v1.0 | open(f,**kargs) | f | Open stream. [-1=>screenshot], [0=>camera0], [1=>camera1], [dirpath=>images], [path=>image],[path=>video] |
v1.0 | extract_video2images(path,**kargs) | path | Extract video to images. |
v1.0 | camera_info() | Display camera information on Ubuntu. | |
v1.0 | compress_images2video(path,**kargs) | path | Make video from images. |
v1.0 | extract_video2audio(f) | path | Extract audio file as mp3. |
v1.0 | join_audio_with_video(vf,af) | vf, af | Join video file and audio file. |
另请参阅“pyglview”软件包。
OpenCV3渲染器太慢。 如果想提高性能,应该使用opencv4或pyglview包。
https://github.com/aieater/python_glview.git
这个包支持最快的opengl查看器和opencv渲染器。 如果您的环境不支持opengl,它将自动切换到cpu渲染器(opencv),也可以使用远程桌面(xserver),如vnc。
acapture+pyglview+webcamera示例。
import cv2
import acapture
import pyglview
viewer = pyglview.Viewer()
cap = acapture.open(0) # Camera 0, /dev/video0
def loop():
check,frame = cap.read() # non-blocking
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
if check:
viewer.set_image(frame)
viewer.set_loop(loop)
viewer.start()
支持60fps的Logicool C922 1280x720(HD)。 这种相机设备和opengl直接渲染器是最佳实践。 Logicool Brio 90fps相机也不错!,但有点贵。
许可证
这个项目是在麻省理工学院的许可下授权的-请参见LICENSE文件了解详细信息