资源简介

python3.6基于Opencv写的一个视频直播小程序,分别用UDP和TCP实现,包括传输屏幕直播和摄像头直播,有兴趣的可以看看

资源截图

代码片段和文件信息

import socket
import cv2
import numpy
from io import BytesIO
from PIL import Image
import struct

IP = “127.0.0.1“
PORT = 9999
address = (IPPORT)
HEADSIZE=12

def byte_to_img(byte):
    str_buf = BytesIO(byte)
    img = numpy.array(Image.open(str_buf))
    str_buf.close()
    return img

def receive2(process=None):
    buff = bytes()
    sock = socket.socket(socket.AF_INETsocket.SOCK_STREAM)
    sock.bind(address)
    sock.listen(5)
    packageaddr = sock.accept()
    while True:
        img = package.recv(1024*1024)
        if img:
            print(type(img))
            buff = buff + img
            while True:
                #判断数据是否完整
                if len(buff) < HEADSIZE:
                    print()
                    break
                headPack = struct.unpack(‘!3I‘ buff[:HEADSIZE])
                cmd = headPack[2]
                bodySize = headPack[1]
                #判断数据包是否完整
                if len(buff) < HEADSIZE + bodySize:
                    break
                body = buff[HEADSIZE:HEADSIZE + bodySize]
                if(cmd==101):
                    img = byte_to_img(body)
                    cv2.imshow(“receive“ img)
                print(len(body))
                buff = buff[HEADSIZE + bodySize:]
            if (cv2.waitKey(1) & 0xFF) == ord(‘q‘):
                break
            else:
                continue
        break
    cv2.destroyAllWindows()
    sock.close()


if __name__==“__main__“:
    receive2()

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-07-19 19:37  TCP_UDP视频传输\
     目录           0  2017-07-19 19:38  TCP_UDP视频传输\TCP\
     文件        1558  2017-07-19 16:53  TCP_UDP视频传输\TCP\server.py
     文件        3699  2017-07-19 17:50  TCP_UDP视频传输\TCP\shexiangtou.py
     目录           0  2017-07-19 15:11  TCP_UDP视频传输\UDP\
     文件         680  2017-07-18 21:05  TCP_UDP视频传输\UDP\server.py
     文件        2005  2017-07-24 10:17  TCP_UDP视频传输\UDP\shexiangtou.py

评论

共有 条评论