import numpy as np
import cv2
import socket
import threading
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 3390))
s.listen()
class test(threading.Thread):
def array_to_image(self, bin):
#x为 np.ndarray类型刚好与深度学习算法对应
x = cv2.imdecode(np.asarray(bytearray(bin), dtype='int8'), cv2.IMREAD_COLOR)
return x
def run(self):
count = 0;
imgbytes = bytes()
conn, addr = s.accept()
getPicFlag = 0
while True:
buffer = conn.recv(1024)
count = len(buffer)
if count == -1:
print('正在连接')
break
else:
if getPicFlag == 1:
for i in range(0, count - 1):
if (buffer[i] == 0xff) & (buffer[i + 1] == 0xd9):
imgbytes = imgbytes + buffer[0:i + 2]
img = self.array_to_image(imgbytes)
cv2.imshow('test', img)
cv2.waitKey(1)
getPicFlag = 0
imgbytes = bytes()
if getPicFlag == 1:
imgbytes = imgbytes + (buffer[0:len(buffer)])
if getPicFlag == 0:
for i in range(0, count - 1):
if (buffer[i] == 0xff) & (buffer[i + 1] == 0xd8):
print('接收到消息头')
getPicFlag = 1
imgbytes = imgbytes + buffer[i:len(buffer)]
thread1 = test()
thread1.start()
while True:
cv2.waitKey(10)
PythonTCP图传上位机
最新推荐文章于 2024-06-16 00:44:47 发布