美文网首页Python 学习
python+opencv+mjpeg

python+opencv+mjpeg

作者: bianruifeng | 来源:发表于2022-12-13 17:07 被阅读0次

    实现了从硬件udp推送照片到服务器,其他设备访问http://127.0.0.1:8080/1.mjpeg 查看图片流的效果
    学习方案
    esp32cam 服务端远程视频方案
    米尔行车记录仪 代码
    python引用库

    pillow
    opencv
    numpy
    

    展示图片推流

    import cv2
    import requests
    import numpy as np
    from io import BytesIO
    from PIL import Image
    
    res = requests.get('http://127.0.0.1:8080/1.mjpeg', stream=True)
    print(res.status_code)
    
    imageBytes = bytes()
    for data in res.iter_content(chunk_size=300):
        # 输出data 查看每一张图片的开始与结尾,查找图片的头与尾截取jpg。并把剩余部分imageBytes做保存
        imageBytes += data
        a = imageBytes.find(b'\xff\xd8')
        b = imageBytes.find(b'\xff\xd9')
        if a != -1 and b != -1:
            jpg = imageBytes[a:b+2]
            imageBytes = imageBytes[b+2:]
    
            bytes_stream = BytesIO(jpg)
            img = Image.open(bytes_stream)
            img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
            cv2.imshow('img', img)
            if cv2.waitKey(10) & 0XFF == ord('q'):
                break
    
    cv2.destroyAllWindows()
    
    

    对视频切片,模拟硬件设备拍照获取图片

    import cv2
    import os
    import threading
    
    
    def video_to_frames(video_path, outPutDirName):
        times = 0
    
        # 提取视频的频率,每1帧提取一个
        frame_frequency = 1
    
        # 如果文件目录不存在则创建目录
        if not os.path.exists(outPutDirName):
            os.makedirs(outPutDirName)
    
        # 读取视频帧
        camera = cv2.VideoCapture(video_path)
    
        while True:
            times = times + 1
            res, image = camera.read()
            if not res:
                print('not res , not image')
                break
            if times % frame_frequency == 0:
                cv2.imwrite(outPutDirName + '\\' + str(times) + '.jpg', image)
    
        print('图片提取结束')
        camera.release()
    
    
    if __name__ == "__main__":
        input_dir = r'D:\video'  # 输入的video文件夹位置
        save_dir = r'D:\video\img'  # 输出图片到当前目录video文件夹下
        count = 0  # 视频数
        for video_name in os.listdir(input_dir):
            video_path = os.path.join(input_dir, video_name)
            outPutDirName = os.path.join(save_dir, video_name[:-4])
            threading.Thread(target=video_to_frames, args=(video_path, outPutDirName)).start()
            count = count + 1
            print("%s th video has been finished!" % count)
    
    

    模拟硬件设备udp图片推流到服务器

    import socket
    import os
    import sys
    
    # 服务器的地址
    server_address = ('127.0.0.1', 8000)
    
    
    def send(dir_name, data_format, file_name):
        # 与接收端建立socket通信
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # AF_INET(TCP/IP – IPv4)协议
        sock.connect(server_address)
    
        # 每次通信都带一个通信头,表明数据源的类型(红外还是可见光),要保存数据帧的文件夹名file_name
        # 你可以不要数据格式,这里可以定义成你自己的形式,也算是一种安全机制
        sock.send('{}|{}'.format(data_format, file_name).encode())  # 默认编码 utf-8,发送文件长度和文件名
        reply = sock.recv(1024)
    
        # 按照文件名排序,0.png,1.png
        file_list = os.listdir(dir_name)
        file_list.sort(key=lambda x: int(x[:-4]))
        if 'ok' == reply.decode():  # 确认一下服务器get到文件长度和文件名数据
            i = 0
            print(len(file_list))
            for file_name in file_list:
                data = file_deal(os.path.join(dir_name, file_name))
                sock.send('{}|{}'.format(len(data), file_name).encode())
                sock.recv(1024)
                go = 0
                total = len(data)
                while go < total:  # 发送文件
                    data_to_send = data[go:go + total // 2]
                    sock.send(data_to_send)
                    go += len(data_to_send)
                sock.recv(1024).decode()
                i += 1
                if i < len(file_list):
                    sock.send(b'continue')
            sock.send(b'over')
            sock.close()
            sys.exit(0)
    
    
    def file_deal(file_path):  # 读取文件的方法
        mes = b''
        try:
            file = open(file_path, 'rb')
            mes = file.read()
        except:
            print('error{}'.format(file_path))
        else:
            file.close()
            return mes
    
    # D:\video\test 内部是命名有序的一组图片
    send(r'D:\video\test', '', 'jpg')
    
    

    模拟服务器接收硬件设备udp图片推流

    import socket
    import os
    import cv2
    import numpy as np
    '''
    接收udp 推送过来的图片 20221213
    
    '''
    LOCAL_IP = '127.0.0.1'  # 本机测试使用ip,局域网中使用需更换ip
    PORT = 8000  # 随意指定一个端口
    
    def server():
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # socket.AF_INET 指ipv4  socket.SOCK_STREAM 使用tcp协议
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)  # 设置端口
        sock.bind((LOCAL_IP, PORT))  # 绑定端口
        sock.listen(3)  # 监听端口
        while True:
            sc, sc_name = sock.accept()  # 当有请求到指定端口是 accept()会返回一个新的socket和对方主机的(ip,port)
            print('收到{}机器请求'.format(sc_name))
            info = sc.recv(1024)  # 接受客户端发来的协议头,区分数据源
            # 安全处理:如果不是以这个协议头开始,认为是非法接入,就直接断掉。这里可以自己定义一些安全消息机制
            print(info)
            try:
                data_format, directory_name = info.decode().split("|")
                sc.send(b'ok')  # 表示收到文件长度和文件名
            except:
                print('协议头不对,自动断开连接')
                sc.close()
                continue
    
            if not os.path.exists(directory_name):
                os.mkdir(directory_name)
            # 协议头正确之后,不断接收发来的数据帧
            while True:
                head_info = sc.recv(1024)
                # print(data_info)
                length, file_name = head_info.decode().split('|')
                sc.send(b'ok')
                if length and file_name:
                    print(file_name)
                    newfile = open(os.path.join(directory_name, file_name), 'wb')  # 这里可以使用从客户端解析出来的文件名
                    file = b''
                    total = int(length)
                    get = 0
                    while get < total:  # 接收文件
                        data = sc.recv(total//2)
                        file += data
                        get = get + len(data)
                    sc.send(b'ok')
                    print('应该接收{},实际接收{}'.format(length, len(file)))
                    if file:
                        imgstring = np.array(file).tobytes()
                        imgstring = np.asarray(bytearray(imgstring), dtype="uint8")
                        image = cv2.imdecode(imgstring, cv2.IMREAD_COLOR)
                        # # 展示多个
                        # cv2.imshow("mutil_pic", image)
                        # # 等待关闭
                        # cv2.waitKey(0)
                        print('actually length:{}'.format(len(file)))
                        newfile.write(file[:])
                        newfile.close()
                reply = sc.recv(1024)
                if reply.decode() == "over":
                    break
    
    server()
    
    
    

    模拟服务器提供mjpeg 图片推流服务

    # -*- coding: utf-8 -*-
    
    # MJPEG Server for the webcam
    """
    mjpeg 推流服务 20221213
    """
    
    import cgi, time
    from os import curdir, sep
    from http.server import BaseHTTPRequestHandler, HTTPServer
    from socketserver import ThreadingMixIn
    import cv2 as cv
    import re
    import sys
    import socket
    import os
    
    # capture = cv.CaptureFromCAM(0)
    # img1 = cv.QueryFrame(capture)
    #
    # if img1 is None:
    #     print("No WebCam Found!")
    #     sys.exit()
    import numpy as np
    
    if len(sys.argv) < 2:
        print("Usage : webcamserver <quality> <port>")
        cameraQuality = 100
        port = 8080
    else:
        cameraQuality = sys.argv[1]
        port = int(sys.argv[2])
    
    
    def file_deal(file_path):  # 读取文件的方法
        mes = b''
        try:
            file = open(file_path, 'rb')
            mes = file.read()
        except:
            print('error{}'.format(file_path))
        else:
            file.close()
            return mes
    
    
    class MyHandler(BaseHTTPRequestHandler):
    
        def do_GET(self):
            global cameraQuality
            try:
                self.path = re.sub('[^.a-zA-Z0-9]', "", str(self.path))
                if self.path == "" or self.path is None or self.path[:1] == ".":
                    return
                if self.path.endswith(".html"):
                    f = open(curdir + sep + self.path)
                    self.send_response(200)
                    self.send_header('Content-type', 'text/html')
                    self.end_headers()
                    self.wfile.write(f.read())
                    f.close()
                    return
                if self.path.endswith(".mjpeg"):
    
                    self.wfile.write(b"HTTP/1.1 200 OK\r\n")
                    self.wfile.write(b'Content-Type: multipart/x-mixed-replace; boundary=frame\r\n\r\n')
    
                    file_list = os.listdir(r'D:\video\test')
                    file_list.sort(key=lambda x: int(x[:-4]))
                    for file_name in file_list:
                        data = file_deal(os.path.join(r'D:\video\test', file_name))
    
                        self.wfile.write(b"--frame\r\nContent-Type: image/jpeg\r\n\r\n" + data)
                        # self.wfile.write(b'--frame\r\n')
                        # self.wfile.write(b"Content-Type: image/jpeg\r\n\r\n")
                        # self.wfile.write(data)
    
                        time.sleep(0.025)
                    return
                if self.path.endswith(".jpeg"):
                    f = open(curdir + sep + self.path)
                    self.send_response(200)
                    self.send_header('Content-type', 'image/jpeg')
                    self.end_headers()
                    self.wfile.write(f.read())
                    f.close()
                    return
                return
            except IOError:
                self.send_error(404, 'File Not Found: %s' % self.path)
    
        def do_POST(self):
            global rootnode, cameraQuality
            try:
                ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
                if ctype == 'multipart/form-data':
                    query = cgi.parse_multipart(self.rfile, pdict)
                self.send_response(301)
    
                self.end_headers()
                upfilecontent = query.get('upfile')
                print("filecontent", upfilecontent[0])
                value = int(upfilecontent[0])
                cameraQuality = max(2, min(99, value))
                self.wfile.write("<HTML>POST OK. Camera Set to<BR><BR>");
                self.wfile.write(str(cameraQuality));
    
            except:
                pass
    
    
    class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
        # class ThreadedHTTPServer(HTTPServer):
        """Handle requests in a separate thread."""
    
        myname = socket.getfqdn(socket.gethostname())
        myaddr = socket.gethostbyname(myname)
    
    
    def main():
        while 1:
            try:
                server = ThreadedHTTPServer(('0.0.0.0', port), MyHandler)
                print('Starting httpServer...')
                print('See <Local IP>:' + str(port) + '/1.mjpeg')
                server.serve_forever()
            except KeyboardInterrupt:
                print('^C received, shutting down server')
                server.socket.close()
    
    
    if __name__ == '__main__':
        main()
    

    相关文章

      网友评论

        本文标题:python+opencv+mjpeg

        本文链接:https://www.haomeiwen.com/subject/rdqcqdtx.html