1. imageZMQ庫實現(xiàn)
imageZMQ庫鏈接:https://github.com/jeffbass/imagezmq
該庫原本是用于樹莓派上的視頻傳輸,其包含很多示例,有興趣可以去看看。
上圖中間的筆記本為發(fā)送端,其余兩個屏幕顯示的是接收端視頻。本文視頻傳輸實現(xiàn)的前提是確保發(fā)送端和接收端接在同一個局域網(wǎng)下。
發(fā)送端
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 為發(fā)送端主機(jī)ip地址
sender = imagezmq.ImageSender(connect_to='tcp://192.168.100.104:5555', REQ_REP=False)
rpi_name = socket.gethostname() # 獲取主機(jī)名
time.sleep(2.0)
jpeg_quality = 95 #調(diào)整圖片壓縮質(zhì)量,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函數(shù)將圖片編碼為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
接收端(可以多臺設(shè)備同時接收)
import cv2
import imagezmq
import traceback
import time
import simplejpeg
# 接收發(fā)送端數(shù)據(jù),輸入發(fā)送端的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協(xié)議實現(xiàn)
發(fā)送端
import socket
import cv2
import sys
import numpy as np
address = ('192.168.100.104', 5005) # 服務(wù)端地址和端口
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('視頻總幀數(shù):',frames_num)
print('發(fā)送目標(biāo)...')
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
# 數(shù)據(jù)打包有很多方式,也可以用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) # 服務(wù)端地址和端口
cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
cli.connect(address) # 嘗試連接服務(wù)端
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()
到此這篇關(guān)于python實現(xiàn)不同電腦之間視頻傳輸?shù)奈恼戮徒榻B到這了,更多相關(guān)python視頻傳輸內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Python使用UDP實現(xiàn)720p視頻傳輸?shù)牟僮?/li>
- 基于python實現(xiàn)高速視頻傳輸程序
- Python樹莓派學(xué)習(xí)筆記之UDP傳輸視頻幀操作詳解
- python 使用socket傳輸圖片視頻等文件的實現(xiàn)方式
- 使用Python的Flask框架實現(xiàn)視頻的流媒體傳輸
- videocapture庫制作python視頻高速傳輸程序