本篇文章小编给大家分享一下python实现不同电脑之间视频传输功能代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
1. imageZMQ库实现
imageZMQ库链接:https://github.com/jeffbass/imagezmq
该库原本是用于树莓派上的视频传输,其包含很多示例,有兴趣可以去看看。
上图中间的笔记本为发送端,其余两个屏幕显示的是接收端视频。本文视频传输实现的前提是确保发送端和接收端接在同一个局域网下。
发送端
import socket import time import cv2 import imagezmq import traceback import simplejpeg # capture=cv2.VideoCapture(0) # 获取摄像头视频 capture=cv2.VideoCapture(r"D:projectdatasetvideo测试.mp4") # 192.168.100.104 为发送端主机ip地址 sender = imagezmq.ImageSender(connect_to='tcp://192.168.100.104:5555', REQ_REP=False) rpi_name = socket.gethostname() # 获取主机名 time.sleep(2.0) jpeg_quality = 95 #调整图片压缩质量,95% while(True): try: ref, frame=capture.read(0) time.sleep(1/60) image = cv2.resize(frame,(1280,720)) curtime = time.time() msg = rpi_name+'*'+str(curtime) # 通过simplejpeg函数将图片编码为jpeg格式,提高传输效率 jpg_buffer = simplejpeg.encode_jpeg(image, quality=jpeg_quality, colorspace='BGR') sender.send_jpg(msg, jpg_buffer) cv2.imshow(rpi_name, image) cv2.waitKey(1) except: print(traceback.print_exc()) break
接收端(可以多台设备同时接收)
import cv2 import imagezmq import traceback import time import simplejpeg # 接收发送端数据,输入发送端的ip地址 image_hub = imagezmq.ImageHub(open_port='tcp://192.168.100.104:5555',REQ_REP=False) frame_count = 1 time1 = 0 while True: try: time1 = time.time() if frame_count == 1 else time1 name, image = image_hub.recv_jpg() # 解码 image = simplejpeg.decode_jpeg(image, colorspace='BGR') cv2.imshow(name.split('*')[0], image) cv2.waitKey(1) time2 = time.time() print(image.shape[:2], int(frame_count/(time2-time1))) frame_count += 1 except: print(traceback.format_exc()) break
2. TCP协议实现
发送端
import socket import cv2 import sys import numpy as np address = ('192.168.100.104', 5005) # 服务端地址和端口 ser = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ser.bind(address) ser.listen(5) # 阻塞式 print('waiting。。。') conn, addr = ser.accept() print('建立连接...') print('连接对象:', addr) cap = cv2.VideoCapture(r"D:projectdatasetvideo测试.mp4") # cap = cv2.VideoCapture(0) frames_num=cap.get(7) print('视频总帧数:',frames_num) print('发送目标...') count = 0 while cap.isOpened(): try: data = conn.recv(1024) data = data.decode() if not data: break ret, frame = cap.read() frame = cv2.resize(frame,(1280,720)) cv2.imshow('send', frame) cv2.waitKey(1) count += 1 # 数据打包有很多方式,也可以用json打包 img_encode = cv2.imencode('.jpg', frame)[1] data_encode = np.array(img_encode) str_encode = data_encode.tostring() conn.sendall(str_encode) except KeyboardInterrupt: print('KeyboardInterrupt') sys.exit(0)
接收端
import socket import sys import cv2 import numpy as np import time address = ('192.168.100.104', 5005) # 服务端地址和端口 cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: cli.connect(address) # 尝试连接服务端 except Exception: print('[!] Server not found or not open') sys.exit() frame_count = 1 while True: time1 = time.time() if frame_count == 1 else time1 trigger = 'ok' cli.sendall(trigger.encode()) data = cli.recv(1024*1024*20) image = np.frombuffer(data, np.uint8) image = cv2.imdecode(image,cv2.IMREAD_COLOR) cv2.imshow('video',image) cv2.waitKey(1) end_time = time.time() time2 = time.time() print(image.shape[:2], int(frame_count / (time2 - time1))) frame_count += 1 cli.close()