RTC提交

This commit is contained in:
ZZX9599
2025-09-02 21:42:09 +08:00
parent fbada1aa9f
commit 805d6b60c4
2 changed files with 62 additions and 24 deletions

View File

@ -1,6 +1,6 @@
import asyncio
import aiohttp
import cv2 # 导入OpenCV库
import cv2
import numpy as np
from aiortc import RTCPeerConnection, RTCSessionDescription, RTCConfiguration
from aiortc.mediastreams import MediaStreamTrack
@ -97,7 +97,7 @@ async def frame_consumer(frame_queue):
# 创建OCR检测器实例请替换为实际的违禁词文件路径
ocr_detector = OCRViolationDetector(
forbidden_words_path=r"D:\Git\bin\video\ocr\forbidden_words.txt", # 替换为实际路径
ocr_confidence_threshold=0.5,)
ocr_confidence_threshold=0.5, )
while True:
# 从队列中获取cv2帧队列为空时会阻塞等待新帧
@ -116,24 +116,31 @@ async def frame_consumer(frame_queue):
# print("帧处理完成、队列已清空")
async def main():
# WebRTC服务器地址
url = "http://192.168.110.25:1985/rtc/v1/whep/?app=live&stream=677a4845aa48cb8526c811ad56fc5e60"
def process_webrtc_stream(ip, webrtc_url):
"""
处理WEBRTC流并持续打印OCR检测结果
Args:
ip: IP地址预留参数
webrtc_url: WEBRTC服务器地址
"""
# 创建队列
frame_queue = asyncio.Queue(maxsize=1)
# 创建任务
receiver_task = asyncio.create_task(rtc_frame_receiver(url, frame_queue))
consumer_task = asyncio.create_task(frame_consumer(frame_queue))
# 定义事件循环中的主任务
async def main_task():
# 创建任务
receiver_task = asyncio.create_task(rtc_frame_receiver(webrtc_url, frame_queue))
consumer_task = asyncio.create_task(frame_consumer(frame_queue))
# 等待任务完成
await asyncio.gather(receiver_task, consumer_task)
# 等待任务完成
await asyncio.gather(receiver_task, consumer_task)
if __name__ == "__main__":
try:
asyncio.run(main())
# 运行事件循环
asyncio.run(main_task())
except KeyboardInterrupt:
print("用户中断处理流程")
finally:
# 确保关闭所有cv2窗口
cv2.destroyAllWindows()