RTC提交
This commit is contained in:
50
rtc/rtc.py
50
rtc/rtc.py
@ -1,8 +1,9 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
import cv2 # 导入OpenCV库
|
||||||
|
import numpy as np
|
||||||
from aiortc import RTCPeerConnection, RTCSessionDescription, RTCConfiguration
|
from aiortc import RTCPeerConnection, RTCSessionDescription, RTCConfiguration
|
||||||
from aiortc.mediastreams import MediaStreamTrack
|
from aiortc.mediastreams import MediaStreamTrack
|
||||||
from ocr.ocr_violation_detector import detect
|
|
||||||
|
|
||||||
|
|
||||||
class VideoTrack(MediaStreamTrack):
|
class VideoTrack(MediaStreamTrack):
|
||||||
@ -18,7 +19,7 @@ class VideoTrack(MediaStreamTrack):
|
|||||||
|
|
||||||
async def rtc_frame_receiver(url, frame_queue):
|
async def rtc_frame_receiver(url, frame_queue):
|
||||||
"""
|
"""
|
||||||
对每帧进行检查、只要接收到 RTC 帧且队列为空、就往队列放入数据
|
对每帧进行检查、只要接收到 RTC 帧且队列为空、就往队列放入cv2格式的帧数据
|
||||||
"""
|
"""
|
||||||
pc = RTCPeerConnection(RTCConfiguration(iceServers=[]))
|
pc = RTCPeerConnection(RTCConfiguration(iceServers=[]))
|
||||||
video_track = VideoTrack()
|
video_track = VideoTrack()
|
||||||
@ -35,17 +36,23 @@ async def rtc_frame_receiver(url, frame_queue):
|
|||||||
while True:
|
while True:
|
||||||
# 接收当前帧并累计计数
|
# 接收当前帧并累计计数
|
||||||
frame = await track.recv()
|
frame = await track.recv()
|
||||||
frame_bgr24 = frame.to_ndarray(format='bgr24')
|
# 转换为cv2兼容的BGR格式numpy数组
|
||||||
total_frames += 1
|
frame_cv2 = frame.to_ndarray(format='bgr24')
|
||||||
|
|
||||||
# 对每帧都检查队列状态、队列为空则放入
|
# 验证是否为cv2兼容格式
|
||||||
if frame_queue.empty():
|
if isinstance(frame_cv2, np.ndarray) and frame_cv2.ndim == 3 and frame_cv2.shape[2] == 3:
|
||||||
# 队列为空、放入当前帧
|
total_frames += 1
|
||||||
await frame_queue.put(frame_bgr24)
|
|
||||||
print(f"第{total_frames}帧:队列为空、已放入新帧")
|
# 对每帧都检查队列状态、队列为空则放入
|
||||||
|
if frame_queue.empty():
|
||||||
|
# 队列为空、放入当前cv2帧
|
||||||
|
await frame_queue.put(frame_cv2)
|
||||||
|
print(f"第{total_frames}帧:队列为空、已放入新的cv2帧,尺寸: {frame_cv2.shape}")
|
||||||
|
else:
|
||||||
|
# 队列非空、说明上一帧还未处理、跳过当前帧
|
||||||
|
print(f"第{total_frames}帧:队列非空、跳过该帧")
|
||||||
else:
|
else:
|
||||||
# 队列非空、说明上一帧还未处理、跳过当前帧
|
print("帧格式转换失败,不是有效的cv2格式")
|
||||||
print(f"第{total_frames}帧:队列非空、跳过该帧")
|
|
||||||
|
|
||||||
# 创建并设置本地offer
|
# 创建并设置本地offer
|
||||||
offer = await pc.createOffer()
|
offer = await pc.createOffer()
|
||||||
@ -82,18 +89,23 @@ async def rtc_frame_receiver(url, frame_queue):
|
|||||||
|
|
||||||
async def frame_consumer(frame_queue):
|
async def frame_consumer(frame_queue):
|
||||||
"""
|
"""
|
||||||
从队列中读取帧并处理(队列空时会阻塞等待)
|
从队列中读取cv2帧并处理(队列空时会阻塞等待)
|
||||||
|
|
||||||
Args: frame_queue: 帧队列
|
Args: frame_queue: 帧队列
|
||||||
"""
|
"""
|
||||||
while True:
|
while True:
|
||||||
# 从队列中获取帧(队列为空时会阻塞等待新帧)
|
# 从队列中获取cv2帧(队列为空时会阻塞等待新帧)
|
||||||
current_frame = await frame_queue.get()
|
current_frame = await frame_queue.get()
|
||||||
|
|
||||||
# 检测
|
# 验证这是cv2可以处理的帧
|
||||||
detect(current_frame)
|
print(f"从队列获取到cv2帧、尺寸: {current_frame.shape}、数据类型: {current_frame.dtype}")
|
||||||
|
|
||||||
print(f"从队列获取到帧、尺寸: {current_frame.shape}、进行处理")
|
# 这里可以添加cv2的处理代码,例如显示帧
|
||||||
|
# cv2.imshow('Received Frame', current_frame)
|
||||||
|
# if cv2.waitKey(1) & 0xFF == ord('q'):
|
||||||
|
# break
|
||||||
|
|
||||||
|
print("cv2帧处理完成")
|
||||||
|
|
||||||
# 标记任务完成
|
# 标记任务完成
|
||||||
frame_queue.task_done()
|
frame_queue.task_done()
|
||||||
@ -116,4 +128,8 @@ async def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
try:
|
||||||
|
asyncio.run(main())
|
||||||
|
finally:
|
||||||
|
# 确保关闭所有cv2窗口
|
||||||
|
cv2.destroyAllWindows()
|
||||||
|
Reference in New Issue
Block a user