python实现不同电脑之间视频传输功能
程序员文章站
2022-06-24 22:18:03
1. imagezmq库实现imagezmq库链接:https://github.com/jeffbass/imagezmq该库原本是用于树莓派上的视频传输,其包含很多示例,有兴趣可以去看看。上图中间...
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:\project\dataset\video\测试.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:\project\dataset\video\测试.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()
到此这篇关于python实现不同电脑之间视频传输的文章就介绍到这了,更多相关python视频传输内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!