python --fastapi推流AI推理

前端demo

python 复制代码
<!DOCTYPE html>
<html>
<body>
    <img id="videoStream" style="width:100%; max-width:800px;">
    <script>
        const params = new URLSearchParams({
            url: 'rtsp://192.168.8.119:555/rtp/34020000001320000206_34020000001320000206',
            box_rgb: '1',
            algo: '1',
            threshold: '0.50',
            regions: JSON.stringify([])
        });
        document.getElementById('videoStream').src =
            'http://192.168.8.119:49351/instant_ai?' + params.toString();
    </script>
</body>
</html>

服务端

url

python 复制代码
@app.get('/instant_ai') # 实时AI
async def instant_ai(request: Request) -> dict: return await instant_ai_views(request)

views

python 复制代码
async def instant_ai_views(request: Request):
    '''
    实时AI
    @params  url            -->  摄像头地址;
    @params  box_rgb        -->  检测框颜色(1红 2绿  3蓝 4白 5黑 6紫);
    @params  algo           -->  算法(1: 人员越线; 2: 安全帽; 3: 明火; 4:未穿反光衣; 5: 未穿工服);
    @params  threshold      -->  阈值;
    @params  regions        -->  区域;
    '''
    params = request.query_params  # 请求参数
    rtx = {k: params.get(k) for k in ('url', 'box_rgb', 'algo', 'threshold', 'regions')}
    if rtx['threshold'] in ('0', 0, None, ''):
        rtx['threshold'] = 0.5

    if rtx['box_rgb'] in ('0', 0, None, ''):
        rtx['box_rgb'] = 1

    return StreamingResponse(
        reid_event_generator(rtx),
        media_type='multipart/x-mixed-replace; boundary=frame')

推流

python 复制代码
# coding: utf-8
import ast
import asyncio

import cv2
from loguru import logger
from yolo import utils


def get_func(rtx: dict):
    '''
    根据枚举获取算法
    算法(1: 人员越线; 2: 安全帽; 3: 明火; 4:未穿反光衣; 5: 未穿工服);
    '''
    algo: str = rtx['algo']
    if algo in (1, '1'):
        func =  utils.yuexian_yolo.yuexian_predict
    elif algo in (2, '2'):
        func = utils.anquanmao_yolo.aqm_predict
    elif algo in (3, '3'):
        func = utils.minghuo_yolo.flame_predict
    elif algo in (4, '4'):
        func = utils.fanguangyi_yolo.fanguangyi_predict
    else:
        func = utils.gongfu_yolo.gongfu_predict
    return func

def get_color(rtx: dict):
    '''
    获取颜色
    红(255, 0, 0) 绿(0, 255, 0) 蓝(0, 0, 255) 白(255, 255, 255) 黑(0, 0, 0) 紫(255, 0, 255)
    '''
    box_rgb = int(rtx['box_rgb'])
    color = {
        1: (255, 0, 0), # 红
        2: (0, 255, 0), # 绿
        3: (0, 0, 255), # 蓝
        4: (255, 255, 255), # 白
        5: (0, 0, 0), # 黑
        6: (255, 0, 255) # 紫
    }
    return color[box_rgb]


async def reid_event_generator(rtx: dict):
    '''响应层'''
    func = get_func(rtx)
    color = get_color(rtx)
    threshold = float(rtx['threshold'])
    regions = ast.literal_eval(rtx['regions'])
    cap = cv2.VideoCapture(rtx['url'])
    cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)  # 缓冲区只留1帧

    try:
        while True:
            for _ in range(3): # 跳帧
                cap.grab()
            success, frame = cap.retrieve()
            if not success:
                await asyncio.sleep(0.01)
                continue

            _, frame = func(frame, regions, threshold, color) # 自己写的AI推理 返回检测的图片
            ret, buffer = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 70])
            jpg_bytes = buffer.tobytes()
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + jpg_bytes + b'\r\n')
            await asyncio.sleep(0.01)
    finally:
        cap.release()
        logger.success(f'浏览器已被关闭,退出拉流,摄像头资源已释放!')
相关推荐
大今野1 小时前
人工智能与区块链核心考点全解析
人工智能·区块链·uml·专转本
专注仿真1 小时前
仿真智能体训练:强化学习中的生存、任务、杀伤、能耗与规则惩罚算法详解与实战
python·算法·afsim智能体·仿真训练·afsim智能体训练
腾讯云开发者2 小时前
当 30 年 ERP 功力遇上 WorkBuddy ,哪些活儿该交出去?
人工智能
腾讯云开发者2 小时前
老板带头,全员跟上:1药网用 WorkBuddy 跑通 AI 落地的一年
人工智能
腾讯云开发者2 小时前
腾讯云 TVP 澳门生态启航!联结本地技术力量共筑 AI 新生态
人工智能
腾讯云开发者2 小时前
腾讯云 TVP 走进影石 Insta360,共探 AI+硬件的无限可能
人工智能
QiLinkOS2 小时前
行业规则的“强制升级”:从“数量竞赛”到“质量博弈”
大数据·人工智能·产业链坐标定位·矩规双螺旋归因模型·矩规评级·科创技术评级·技术坐标刻度模型
腾讯云开发者2 小时前
腾讯云 TVP走进vivo,共启端侧到具身的万物智能
人工智能
RobinDevNotes2 小时前
黄仁勋复职之外,AI这周还有6件大事
人工智能·rust·人工智能开发工具
cpolar技术支持2 小时前
datetime.now() 和 datetime.now(timezone.utc) 混进 SQLite:零报错,读出来差 8 小时
python·sqlite·时间戳·datetime·时区