[英]RaspberryPi camera stream processing with Python 2.7
我在這里找到了以下代碼,用於捕獲來自RaspberryPi攝像機的視頻流。
現在,我要處理視頻流(例如,找到BLOB或顯示視頻)。
為此,我需要將視頻的內容捕獲為數組,例如在此示例中使用名稱frame
:
cv2.imshow('stream', frame)
我通過將其轉換為數組來嘗試
frame = self.stream.array
但有些時候我無法正常工作。
輸出告訴我無法使用流創建數組:
File "Raspberry.py", line 35, in run
frame = self.stream.array #creating an array for the stream
AttributeError: '_io.BytesIO' object has no attribute 'array'
有什么想法如何解決嗎?
碼:
import io
import time
import threading
import picamera
# 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)
frame = self.stream.array #creating an array for the stream
cv2.imshow('stream', frame) #should show the livestream
# Read the image and do some processing on it
#Image.open(self.stream)
#...
#...
# 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
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()
如果您確實不需要,基本的picamera軟件包不提供數組功能以防止在Raspberry Pi上進行冗長的numpy編譯。
要安裝numpy數組支持,請嘗試:
pip install --user "picamera[array]"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.