gaw*_*103 5 python opencv flask python-3.x raspberry-pi
在我当前的烧瓶项目中,我正在尝试流式传输两个现场视频,这听起来很简单。问题是我只有一个视频源(相机)。目的是有两个视频流:一个没有任何修改,一个应用了人脸检测。如果用户想要进行人脸检测,那么通过单击按钮,他的相机视图将更改为应用了人脸检测的流。如果用户不想拥有它,那么他将看到没有它的流。非常重要的是 - 多个用户可以一次查看流。整个程序适用于 RPi 4B 4gb。
我有一个 CamerasPool 类:
from .CameraHandler import CameraHandler
import cv2
class CamerasPool:
def __init__(self):
self.__cameras = []
def registerCamera(self, id, detection):
self.__cameras.append(CameraHandler(id, cv2.VideoCapture(0), detection))
print('Camera registered')
def getCamWithParameters(self, detection):
camera = None
for cam in self.__cameras:
if cam.getDetectionState() == detection:
camera = cam
break
return camera
Run Code Online (Sandbox Code Playgroud)
和 CamerasHandler 类:
import cv2
from time import sleep
class CameraHandler():
def __init__(self, id, cam, detectionState):
self.__id = id
self.__cam = cam
self.__current_frame = None
self.__shouldDetect = detectionState
print(f'Camera created with id {id} created')
def __del__(self):
self.__cam.release()
def getDetectionState(self):
return self.__shouldDetect
def __detectFace(self, img):
faces, confidences = cv.detect_face(img)
for face in faces:
(startX, startY) = face[0], face[1]
(endX, endY) = face[2], face[3]
cv2.rectangle(img, (startX, startY), (endX, endY), (0, 255, 0), 2)
return img
def __getFrame(self):
rval, frame = self.__cam.read()
if rval:
frame = cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
try:
if self.__shouldDetect:
frame = self.__detectFace(frame)
except:
print('Face detection exception')
(flag, encodedImage) = cv2.imencode(".jpg", frame)
self.__current_frame = bytearray(encodedImage)
def gen(self):
while True:
self.__getFrame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + self.__current_frame + b'\r\n')
Run Code Online (Sandbox Code Playgroud)
我正在尝试按如下方式创建相机:
# Create two cameras
print('Before cameras creation')
camerasPool = CamerasPool()
print('After cameras pool creation')
camerasPool.registerCamera(0, False)
camerasPool.registerCamera(1, True)
print('Created both cameras')
Run Code Online (Sandbox Code Playgroud)
正如您在 CamerasPool 类中看到的,我正在创建这样的相机对象self.__cameras.append(CameraHandler(id, cv2.VideoCapture(0), detection)),它创建了两个想要访问相同资源的对象 - 相机。
当我启动整个程序时,我可以看到以下输出:
* Serving Flask app "server/" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://192.168.0.151:7070/ (Press CTRL+C to quit)
* Restarting with stat
Before cameras creation
After cameras pool creation
* Debugger is active!
* Debugger PIN: 196-285-435
Before cameras creation
After cameras pool creation
Run Code Online (Sandbox Code Playgroud)
程序冻结,仅此而已。在输出中,我也应该看到Camera created with id 0 createdand Camera created with id 1 created,但据我所知它们还没有被创建 - 程序在这个陡峭的地方冻结。我猜这个问题是因为两个 VideoCapture 对象。有人可以帮我解决这个问题吗?也许其他一些解决方案如何从一台相机获得两个流?
最好的问候,彼得
您无法实例化两个CamerasPool对象,因为您只有一台相机。但你能做的是:
CamerasPool,使其成为自己的线程并且不会阻止 python 应用程序的执行。此类的目的是简单地从相机读取帧,复制每个帧并将put()它们放入两个单独的queue对象中。队列在程序中应该是全局的,以便需要并发运行来处理数据和传输数据的其他线程可以访问它们。VideoStream来负责get()来自特定的帧queue,处理它并流式传输它。处理意味着在将帧传输到网络之前您想要对帧执行的任何操作:转换为灰度、绘制矩形、检测面部等。此类还需要在单独的线程中运行,并且构造函数需要接收两个参数:第一个参数指示应使用两个全局队列中的哪一个;第二个参数指示帧在流式传输之前是否应进行处理;如果您正在寻找有关如何使用多线程从相机检索帧并将其存储在队列中的代码示例,请检查此答案,特别是以下部分:
如何以相机支持的最接近的最大fps进行拍摄?线程和队列示例。
请记住,您的应用程序将有 4 个线程:
CamerasPool;VideoStream将被实例化的对象;| 归档时间: |
|
| 查看次数: |
374 次 |
| 最近记录: |