最近需要将本地的实时视频图像传输到远端,故采用Soket TCP传输实现。其中也遇到了很多问题,最终解决。
服务端:
import cv2
import time
import numpy as np
import socket
def recv_size(sock, count):
buf = b''#buf是一个byte类型
while count:
newbuf = sock.recv(count)
if not newbuf:
return None
buf += newbuf
count -= len(newbuf)
return buf
def recv_img(sock, count):
buf = b''#buf是一个byte类型
while count:
#接受TCP套接字的数据。数据以字符串形式返回,count指定要接收的最大数据量.
newbuf = sock.recv(count)
if not newbuf:
return None
buf += newbuf
count -= len(newbuf)
return buf
# socket.AF_INET 用于服务器与服务器之间的网络通信
# socket.SOCK_STREAM 代表基于TCP的流式socket通信
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# sock.bind(('127.0.0.1', 00000))
sock.bind(('0.0.0.0', 00000)) #需改为自己的ip和端口
sock.listen(3)
conn, addr = sock.accept()
sentace = "get_size"
def get_img():
start = time.time()
length = recv_size(conn, 16)
conn.send(sentace.encode('utf-8'))
result = int(str(length, encoding='utf-8'))
stringData = recv_img(conn, result)
data = np.fromstring(stringData, np.uint8)
decimg = cv2.imdecode(data, cv2.IMREAD_COLOR) # 解码处理,返回mat图片
end = time.time()
seconds = end - start
print("Time is: " + str(seconds))
return decimg
def send_result(img):
ret, img_encode = cv2.imencode('.jpg', img)
data_encode = np.array(img_encode)
stringData = data_encode.tostring()
# 首先发送图片编码后的长度
conn.send(str.encode(str(len(stringData)).ljust(16)))
answer = conn.recv(1024)
answer = str(answer, encoding = "utf-8")
if answer == "Get":
conn.send(stringData)
客户端:
import cv2
import socket
import sys
import numpy as np
import time
cap = cv2.VideoCapture(0)
def load_img():
_, frame = cap.read()
image = frame[..., ::-1]
# image = frame
return image
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 连接服务端
sock.connect(('10.22.18.23', 52846))
# socket.setdefaulttimeout(1000)
def recv_img(sock, count):
buf = b''#buf是一个byte类型
while count:
#接受TCP套接字的数据。数据以字符串形式返回,count指定要接收的最大数据量.
newbuf = sock.recv(count)
if not newbuf:
return None
buf += newbuf
count -= len(newbuf)
return buf
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 100]
sentace = "Get"
while True:
img = load_img()
# 首先对图片进行编码,因为socket不支持直接发送图片
# '.jpg'表示把当前图片frame按照jpg格式编码
ret, img_encode = cv2.imencode('.jpg', img, encode_param)
data_encode = np.array(img_encode)
stringData = data_encode.tostring()
# 首先发送图片编码后的长度
sock.send(str.encode(str(len(stringData)).ljust(16)))
answer = sock.recv(1024)
answer = str(answer, encoding = "utf-8")
if answer == "get_size":
sock.send(stringData)
# 接收返回的数据尺寸
receive = sock.recv(1024)
# 返回信息已收到通知
sock.send(sentace.encode('utf-8'))
receive = int(str(receive, encoding='utf-8'))
if receive:
result_mat = recv_img(sock, receive)
result_mat = np.fromstring(result_mat, np.uint8)
result_mat = cv2.imdecode(result_mat, cv2.IMREAD_COLOR) # 解码处理,返回mat图片
cv2.imshow("Backgrpound Matting", result_mat)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
sock.close()
cv2.destroyAllWindows()