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 天前
【回眸】Minicart 电商购物车核心功能落地指南
人工智能
一隅论数智1 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
默_笙1 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
AI的探索之旅1 天前
97 个 OpenCV 实例(三十):双目立体,从标定到点云
人工智能·opencv·计算机视觉
AlbertZein1 天前
Step-5-Preview 上手实测:3D 游戏、金融分析、网页设计一次跑完
人工智能·aigc
LaughingZhu1 天前
Product Hunt 每日热榜 | 2026-09-19
人工智能·深度学习·神经网络·搜索引擎·百度
qq_426003961 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫1 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
美狐美颜SDK开放平台1 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk