前言

本项目应用于目标检测在服务器端,没有gui界面查看实时结果的项目上。

如果考虑自己用,可以直接使用flask-video-stream简易视频流搭建来进行轻量化的搭建,但是带宽开销比较大,没有使用H264等协议,720清晰度大约带宽开销15MB,现场项目上无法使用,因此切换成ffmpeg+videojs(vlc也可以,客户端查看)+python的方式实现目标检测的视频流展示。

rtmp 服务器搭建

在服务器端搭建一套rtmp,接收视频流的传输,可以采用docker的方式实现一键化部署,实现链接放这里了rtsp+docker实现服务器推流

web

采用videojs进行视频流的展示,需要服务端nginx一起搭配使用。具体可以参考网上其他人的博客进行搭建,服务器端的代码如下

<html>  
  <head>  
  <title>视频直播</title>  
  <meta charset="utf-8">  
  <link href="http://vjs.zencdn.net/5.5.3/video-js.css" rel="stylesheet">  
  <!-- If you'd like to support IE8 -->  
  <script src="http://vjs.zencdn.net/ie8/1.1.1/videojs-ie8.min.js"></script>  
</head>  
<body>  
  <h1>直播间</h1>  
    <video id="my-video" class="video-js" controls preload="auto" width="640" height="300"  data-setup="{}">  
        <source src="rtmp://192.168.0.132/live/" type="rtmp/flv">  
        <!-- 如果上面的rtmp流无法播放,就播放hls流 -->  
        <!-- <source src="http://10.10.5.119/live/livestream.m3u8" type='application/x-mpegURL'> -->  
        <p class="vjs-no-js">播放视频需要启用 JavaScript,推荐使用支持HTML5的浏览器访问。  
        To view this video please enable JavaScript, and consider upgrading to a web browser that  
        <a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a>  
        </p>  
    </video>  
 <script src="http://vjs.zencdn.net/5.5.3/video.js"></script>  
</body>  
</html> 

将其中<source src="rtmp://192.168.0.132/live/" type="rtmp/flv">修改为你自己的视频流就可以了。

python后端

后端是参考
读取多个(海康\大华)网络摄像头的视频流 (使用opencv-python),解决实时读取延迟问题
python利用ffmpeg进行rtmp推流直播来进行搭建的,第一篇是多进程实现,第二篇是在1的基础上优化成多线程,但不支持多路,于是我修改为了多线程多路摄像头的直播搭建,并且带宽优化为了5MB(就是0.625mb/s的网速)

import cv2
import time
import numpy as np
import subprocess as sp
import threading
import queue

lock = threading.Lock()

class Live(object):
    def __init__(self):
        self.state = 0
        self.frame_queue = []
        self.dic_frame = dict()
    
    def image_put(self,frame_queue,command_queue,url):
        while True:
            frame = self.dic_frame.get(url,np.array(0))
            if frame.all() == True:
                continue
            height,width = int(frame.shape[0]),int(frame.shape[1])
            break

        fps = 25
        rtmpUrl = "rtmp://192.168.0.132/live/" + url

        # ffmpeg command
        command = ['ffmpeg',
            '-y',
            '-f', 'rawvideo',
            '-vcodec','rawvideo',
            '-pix_fmt', 'bgr24',
            '-s', "{}x{}".format(width, height),
            '-r', str(fps),
            '-i', '-',
            '-c:v', 'libx264',
            '-pix_fmt', 'yuv420p',
            '-preset', 'ultrafast',
            '-f', 'flv', 
            rtmpUrl]
        command_queue.put(command)

        while True:
            with lock:
                frame_te = self.dic_frame.get(url,None)
                if frame_te.all() == False:
                    frame = frame_te
                frame_queue.put(frame)
            # print("cost time = {}".format(time.time() - t0))
            time.sleep(1.0/(fps+10))
            # frame_queue.get() if frame_queue.qsize() > 1 else time.sleep(0.01)

    def image_get(self,frame_queue,command_queue,url):
        fps = 25
        while True:
            command = command_queue.get()
            if len(command) > 0:
                p = sp.Popen(command,stdin=sp.PIPE)
                break

        while True:
            with lock:
                if frame_queue.empty() != True:
                    frame = frame_queue.get()
                    # process frame
                    # 你处理图片的代码
                    # write to pipe
                    p.stdin.write(frame.tostring())
            time.sleep(1.0/(fps+10))

    def refresh(self,temp_plotted_image_frames):
        with lock:
            for key, val in temp_plotted_image_frames.items():
                self.dic_frame[key] = val

    def run_multi_camera(self, temp_plotted_image_frames):
        camera_ip_l = []
        self.state = 1
        for key, val in temp_plotted_image_frames.items():
            camera_ip_l.append(key)
        
        self.frame_queues = [queue.Queue() for _ in camera_ip_l]
        self.command = [queue.Queue() for _ in camera_ip_l]
        self.refresh(temp_plotted_image_frames)

        threads = []
        for frame_queue,command,url in zip(self.frame_queues,self.command,camera_ip_l):
            threads.append(threading.Thread(target=self.image_put,args=(frame_queue,command,url)))
            threads.append(threading.Thread(target=self.image_get,args=(frame_queue,command,url)))
        
        [thread.setDaemon(True) for thread in threads]
        [thread.start() for thread in threads]

    def run(self):
        url = "rtmp://192.168.0.134/live/test11"
        uid = "402880f076ade2da0176b27950b10023"
        url2 = "rtmp://192.168.0.134/live/test6"
        uid2 = "402880f076ade2da0176b279ac930024"
        cap = cv2.VideoCapture(url)
        cap2 = cv2.VideoCapture(url2)
        temp_plotted_image_frames = dict()
        while True:
            ret, frame = cap.read()
            if ret == False:
                break
            temp_plotted_image_frames[uid] = frame
            ret2, frame2 = cap2.read()
            if ret2 == False:
                break
            temp_plotted_image_frames[uid2] = frame2
            if live.state == 0:
                self.run_multi_camera(temp_plotted_image_frames)
            else:
                self.refresh(temp_plotted_image_frames)


if __name__ == '__main__':
    live = Live()
    live.run()

url可以改成你自己的摄像头编号,都一样的,我这个是因为之前有转换好的视频流,就直接用这个了。

uid随便你修改的。

可以通过vlc进行查看,rtmp://192.168.0.132/live/402880f076ade2da0176b27950b10023,就可以通过vlc来进行查看了。

发现一些小bug,还没来得及修改,刷新率25可能会跟不上,fps那里可以自己优化下,原因是线程共享资源的占用,已经锁上了,但是读取也要占用零点几毫秒的时间,导致帧数与预设的不符,会造成视频流中断或者停顿,优化不动了- -

代码不多解释了,有问题私信我,我一般都会回答的。ddddhm┭┮﹏┭┮

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐