- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用带有 picamera 和 opencv python 模块的树莓派尝试进行一些快速捕获和处理。目前我正在使用 http://picamera.readthedocs.org/en/latest/recipes2.html#rapid-capture-and-processing 中的食谱将每个图像捕获到 BytesIO 流。然后,我在 ImageProccessor 类中添加了代码,以将每个流转换为 opencv 对象并“即时”进行一些分析。
因此我当前的代码看起来像这样:
import io
import time
import threading
import picamera
import cv2
import picamera.array
import numpy as np
# Create a pool of image processors
done = False
lock = threading.Lock()
pool = []
class ImageProcessor(threading.Thread):
def __init__(self):
super(ImageProcessor, self).__init__()
self.stream = io.BytesIO()
self.event = threading.Event()
self.terminated = False
self.start()
def run(self):
# This method runs in a separate thread
global done
while not self.terminated:
# Wait for an image to be written to the stream
if self.event.wait(1):
try:
self.stream.seek(0)
# Read the image and do some processing on it
# Construct a numpy array from the stream
data = np.fromstring(self.stream.getvalue(), dtype=np.uint8)
# "Decode" the image from the array, preserving colour
image = cv2.imdecode(data, 1)
# Here goes more opencv code doing image proccessing
# Set done to True if you want the script to terminate
# at some point
#done=True
finally:
# Reset the stream and event
self.stream.seek(0)
self.stream.truncate()
self.event.clear()
# Return ourselves to the pool
with lock:
pool.append(self)
def streams():
while not done:
with lock:
if pool:
processor = pool.pop()
else:
processor = None
if processor:
yield processor.stream
processor.event.set()
else:
# When the pool is starved, wait a while for it to refill
print ("Waiting")
time.sleep(0.1)
with picamera.PiCamera() as camera:
pool = [ImageProcessor() for i in range(4)]
camera.resolution = (640, 480)
camera.framerate = 30
camera.start_preview()
time.sleep(2)
camera.capture_sequence(streams(), use_video_port=True)
# Shut down the processors in an orderly fashion
while pool:
with lock:
processor = pool.pop()
processor.terminated = True
processor.join()
问题在于,这涉及到每张图像的 JPEG 编码和解码,这是有损且耗时的。建议的替代方法是捕获到 picamera.array:http://picamera.readthedocs.org/en/latest/recipes1.html#capturing-to-an-opencv-object ,对于单个图像代码:
import time
import picamera
import picamera.array
import cv2
with picamera.PiCamera() as camera:
camera.start_preview()
time.sleep(2)
with picamera.array.PiRGBArray(camera) as stream:
camera.capture(stream, format='bgr')
# At this point the image is available as stream.array
image = stream.array
效果很好,但我不知道如何组合这两段代码,以便 ImageProcessor 类定义 picamera.array 而不是 BytesIO 流。需要使用“with”语句来为 picamera.array 生成流,这让我很困惑(我是 python 的新手...;))。感谢您的指点。天使
最佳答案
我发现你可以只引用picamera模块的源。
def raw_resolution(resolution):
"""
Round a (width, height) tuple up to the nearest multiple of 32 horizontally
and 16 vertically (as this is what the Pi's camera module does for
unencoded output).
"""
width, height = resolution
fwidth = (width + 31) // 32 * 32
fheight = (height + 15) // 16 * 16
return fwidth, fheight
def bytes_to_rgb(data, resolution):
"""
Converts a bytes objects containing RGB/BGR data to a `numpy`_ array.
"""
width, height = resolution
fwidth, fheight = raw_resolution(resolution)
if len(data) != (fwidth * fheight * 3):
raise PiCameraValueError(
'Incorrect buffer length for resolution %dx%d' % (width, height))
# Crop to the actual resolution
return np.frombuffer(data, dtype=np.uint8).\
reshape((fheight, fwidth, 3))[:height, :width, :]
可以调用转换
image = bytes_to_rgb(self.stream.getvalue(),resolution)
分辨率是(宽度,高度)。 camera
传递给 PiRGBArray
的原因是能够引用相机的分辨率。
关于python - 在 python + picamera + opencv 的线程内使用 "with",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29065624/
我正在使用 python 的 picamera 模块捕捉延时摄影: from picamera import PiCamera, Color class EggAlertCam: def __
以下示例代码将图像保存到流中。我想知道如何将此流中的图像保存到我的 Pi SD 卡上的图像文件(.jpg 等)中,最好是在捕获所有图像后以保持高 FPS。 import io import time
我正在使用带有摄像头模块(Raspberry pi Camera)的 Raspberry pi 无人机安装在无人机上,并编程为在固定间隔后拍照。然而,图像似乎有点不一致,如下所示 . 与其他图片相比,
我有一个带有摄像头模块的 Raspberry Pi,我想将 PiCamera 的视频返回从 Python 流式传输到另一台运行 Python 服务器的计算机。 我成功地使用 OpenCV 返回了视频(
我关注了这个网站( https://www.pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and
我正在 Flask 中创建一个小网络界面,以使用 PiCamera python 模块控制 Raspberry Pi 相机。我有一个工作索引页面,它显示来自相机的流。但是,当我通过输入按钮 POST
我想启动相机预览,使用 PiCamera 模块一段时间后拍摄一张照片,当拍摄照片时,它应该卡住预览以便用户可以查看它,我如何卡住预览? 目前我使用 matplotlib.image 显示它: from
我最近开始使用 Raspberry PiCamera 模块,并且遇到了图像预览问题。使用 start_preview() 可以很好地处理 Raspberry 的物理视频输出。现在我想通过 TightV
我上周刚拿到我的第一个 Raspberry Pi 产品。我有一个 Raspberry Pi Zero W 和一个 PiCamera。我创建了一个程序(由 crontab 每天运行),它将拍摄照片,将照
尝试使用 picamera 在树莓派 3 上进行线程图像采集。编写这段代码的原始程序员消失了,所以我不得不重新学习 python,我有点迷路了。现在我只是想做一些非常基本的事情,并希望从中扩展。 我这
我正在使用 Raspberry 来简单地显示一个视频(目前只是这个)。为此,我必须使用 opencv (cv2)。我尝试了很多解决方案,但现在我想使用 Picamera 库捕获视频。我会告诉你我的代码
您好,我正在尝试使用 PiCamera 模块录制期间捕获低分辨率图像。然而它在camera.capture线上崩溃并给出以下错误 File "/usr/lib/python3/dist-package
我要安装picamera在 Ubuntu 20.04 上。 在 sudo pip install picamera 之后出现以下错误. 如何安装 picamera ?. ERROR: Command
我正在尝试使用 python 覆盆子相机模块录制视频,并且 然后将每一帧转换为一个openCV帧,但没有成功: import time import picamera import cv2 impor
我正在编写一个 python 脚本,它将有一个 ASCII 菜单来设置延时摄影。如果服务器不可用,将图像发送到我的网络服务器或 USB。 我正在做线程脚本。所以我不确定它是线程问题还是什么,但我收到错
我看到大量关于将 raspivid 流直接传输到 FFMPEG 以进行编码、混合和重新流式传输的信息,但这些用例主要来自 bash;类似于: raspivid -n -w 480 -h 320 -b
我正在使用带有 picamera 和 opencv python 模块的树莓派尝试进行一些快速捕获和处理。目前我正在使用 http://picamera.readthedocs.org/en/late
我对 Python 相当陌生,正在尝试编写一种 PiCamera 来拾取不同颜色并据此使用react的方法。我目前对每种颜色的尝试是拍照并检测所涉及的颜色。下面是一些示例代码。 def red_det
我正在尝试获取我的 PiCamera 模块 v2.1 在我的 RaspberryPi4 上运行。不幸的是,我必须安装 Ubuntu 19.10 64 位 分配。到目前为止,一切都很好。 我已经安装了
我正在与一些 friend 一起开发一个项目,我们在实现 picamera 时遇到了一些问题。 我们尝试在程序开始时导入 cv2 和 picamera(使用 Python 3),到目前为止导入 cv2
我是一名优秀的程序员,十分优秀!