From df74b688fa9c2e01dff9ea0d9c7d78b875123f49 Mon Sep 17 00:00:00 2001 From: ZZX9599 <536509593@qq.com> Date: Tue, 2 Sep 2025 20:14:40 +0800 Subject: [PATCH] =?UTF-8?q?RTC=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rtc/rtc.py | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/rtc/rtc.py b/rtc/rtc.py index 01bf1d7..aa4a78d 100644 --- a/rtc/rtc.py +++ b/rtc/rtc.py @@ -1,8 +1,9 @@ import asyncio import aiohttp +import cv2 # 导入OpenCV库 +import numpy as np from aiortc import RTCPeerConnection, RTCSessionDescription, RTCConfiguration from aiortc.mediastreams import MediaStreamTrack -from ocr.ocr_violation_detector import detect class VideoTrack(MediaStreamTrack): @@ -18,7 +19,7 @@ class VideoTrack(MediaStreamTrack): async def rtc_frame_receiver(url, frame_queue): """ - 对每帧进行检查、只要接收到 RTC 帧且队列为空、就往队列放入数据 + 对每帧进行检查、只要接收到 RTC 帧且队列为空、就往队列放入cv2格式的帧数据 """ pc = RTCPeerConnection(RTCConfiguration(iceServers=[])) video_track = VideoTrack() @@ -35,17 +36,23 @@ async def rtc_frame_receiver(url, frame_queue): while True: # 接收当前帧并累计计数 frame = await track.recv() - frame_bgr24 = frame.to_ndarray(format='bgr24') - total_frames += 1 + # 转换为cv2兼容的BGR格式numpy数组 + frame_cv2 = frame.to_ndarray(format='bgr24') - # 对每帧都检查队列状态、队列为空则放入 - if frame_queue.empty(): - # 队列为空、放入当前帧 - await frame_queue.put(frame_bgr24) - print(f"第{total_frames}帧:队列为空、已放入新帧") + # 验证是否为cv2兼容格式 + if isinstance(frame_cv2, np.ndarray) and frame_cv2.ndim == 3 and frame_cv2.shape[2] == 3: + total_frames += 1 + + # 对每帧都检查队列状态、队列为空则放入 + 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: - # 队列非空、说明上一帧还未处理、跳过当前帧 - print(f"第{total_frames}帧:队列非空、跳过该帧") + print("帧格式转换失败,不是有效的cv2格式") # 创建并设置本地offer offer = await pc.createOffer() @@ -82,18 +89,23 @@ async def rtc_frame_receiver(url, frame_queue): async def frame_consumer(frame_queue): """ - 从队列中读取帧并处理(队列空时会阻塞等待) + 从队列中读取cv2帧并处理(队列空时会阻塞等待) Args: frame_queue: 帧队列 """ while True: - # 从队列中获取帧(队列为空时会阻塞等待新帧) + # 从队列中获取cv2帧(队列为空时会阻塞等待新帧) current_frame = await frame_queue.get() - # 检测 - detect(current_frame) + # 验证这是cv2可以处理的帧 + 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() @@ -116,4 +128,8 @@ async def main(): if __name__ == "__main__": - asyncio.run(main()) + try: + asyncio.run(main()) + finally: + # 确保关闭所有cv2窗口 + cv2.destroyAllWindows()