有没有办法使用 Python 来读取和处理相机的帧,然后将其保存到文件中.不使用 OpenCV 等库? [英] Is there a way to use Python to read and process a camera's frame then save it to a file. Without using libraries such as OpenCV?
问题描述
这是我目前所拥有的,但它的读取时间真的很慢.仅使用 opencv 读取帧就需要大约 6-8 秒,对于我的项目,我需要能够在读取压力传感器时以特定间隔获取图片.有没有办法用 cv2 使这个程序更快,或者有没有办法使用数组或什么不能更快地做到这一点.
This is what I have at the moment but its getting really slow read times. It takes about 6-8 seconds alone just to read the frame with opencv and for my project I need to be able to get a picture at specific intervals as it reads a pressure transducer. Is there a way to make this program faster with cv2 or can is there a way using arrays or what not to do this much quicker.
import cv2
import timeit
def main(): #Define camera function
start = timeit.default_timer() #Starts a runtime timer
hud_Cam = cv2.VideoCapture(0) #Call camera resource
gauge_Cam = cv2.VideoCapture(1)
rval, hud_Img = hud_Cam.read() #Read/Grab camera frame
rval, gauge_Img = gauge_Cam.read()
stop = timeit.default_timer() #Stops a runtime timer
print('Time: ', stop - start) #Calculate runtime timer
start1 = timeit.default_timer() #Starts a runtime timer
hud_name = ('HudPicture0.jpg') #Initialize file names
gauge_name = ('GaugePicture0.jpg')
cv2.imwrite(hud_name, hud_Img) #Write camera frame to file names
cv2.imwrite(gauge_name, gauge_Img)
print("Hud Picture written!") #Log to console
print("Gauge Picture written!")
hud_Cam.release() #Release camera resource to clear memory
gauge_Cam.release()
stop1 = timeit.default_timer() #Stops a runtime timer
print('Time: ', stop1 - start1) #Calculate runtime timer```
推荐答案
据我了解您的问题,您希望能够在 Labview 提出要求后尽快从两个相机拍摄两张图像.
As I understand your question, you want to be able to take two images from two cameras as soon as possible after Labview requests it.
在 Linux 或 macOS 上,我会尽快开始连续捕获,然后使用 Unix 信号通知捕获过程.不幸的是,您使用的是 Windows 并且信号在那里不能很好地工作.因此,我将使用文件系统发出信号 - 如果 Labview 想要拍摄照片,它只会创建一个包含或不包含名为 capture.txt
的内容的文件,这将使 Python 进程保存当前图像.还有其他更复杂的方法,但这演示了这个概念,随着您了解的更多,您可以用写入套接字、MQTT 消息或其他内容来替换信号机制.
On Linux or macOS, I would start capturing continuously as soon as possible and then signal the capturing process using Unix signals. Unfortunately, you are using Windows and signals don't work so well there. So, I am going to use the filesystem to signal - if Labview wants a picture taken, it just creates a file with or without content called capture.txt
and that makes the Python process save the current image. There are other more sophisticated methods, but this demonstrates the concept and as you learn more, you may replace the signalling mechanism with a write on a socket, or an MQTT message or something else.
我将两个摄像头放在两个独立的线程中,以便它们可以并行工作,即更快.
I put the two cameras in two independent threads so they can work in parallel, i.e. faster.
#!/usr/bin/env python3
import cv2
import threading
import logging
from pathlib import Path
def capture(stream, path):
"""Capture given stream and save to file on demand"""
# Start capturing to RAM
logging.info(f'[captureThread]: starting stream {stream}')
cam = cv2.VideoCapture(stream, cv2.CAP_DSHOW)
while True:
# Read video continuously
_, im = cam.read()
# Check if Labview wants it
if CaptureFile.exists():
# Intermediate filename
temp = Path(f'saving-{stream}.jpg')
# Save image with temporary name
cv2.imwrite(str(temp), im)
# Rename so Labview knows it is complete and not still being written
temp.rename(path)
logging.info(f'[captureThread]: image saved')
break
logging.info(f'[captureThread]: done')
if __name__=="__main__":
# Set up logging - advisable when threading
format = "%(asctime)s: %(message)s"
logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")
logging.info("[MAIN]: starting")
# Labview will create this file when a capture is required, ensure not there already
CaptureFile = Path('capture.txt')
CaptureFile.unlink(True)
# Create a thread for each camera and start them
HUDthread = threading.Thread(target=capture, args=(0, Path('HUD.jpg')))
Gaugethread = threading.Thread(target=capture, args=(1, Path('Gauge.jpg')))
HUDthread.start()
Gaugethread.start()
# Wait for both to exit
HUDthread.join()
Gaugethread.join()
logging.info("[MAIN]: done")
这篇关于有没有办法使用 Python 来读取和处理相机的帧,然后将其保存到文件中.不使用 OpenCV 等库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!