数据采集端
import socket
import cv2
import numpy
import time
import sys
def SendPicture():
# 建立sock连接
# address要连接的服务器IP地址和端口号
address = ('10.52.130.116', 3000)
try:
# 建立socket对象,参数意义见https://blog.csdn.net/rebelqsp/article/details/22109925
# socket.AF_INET:服务器之间网络通信
# socket.SOCK_STREAM:流式socket , for TCP
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 开启连接
sock.connect(address)
except socket.error as msg:
print(msg)
sys.exit(1)
# 建立图像读取对象
capture = cv2.VideoCapture(0)
# 读取一帧图像,读取成功:ret=1 frame=读取到的一帧图像;读取失败:ret=0
ret, frame = capture.read()
print(frame.shape)
# 压缩参数,后面cv2.imencode将会用到,对于jpeg来说,15代表图像质量,越高代表图像质量越好为 0-100,默认95
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 100]
while ret:
# cv2.imencode将图片格式转换(编码)成流数据,赋值到内存缓存中;主要用于图像数据格式的压缩,方便网络传输
# '.jpg'表示将图片按照jpg格式编码。
result, imgencode = cv2.imencode('.jpg', frame, encode_param)
# 建立矩阵
data = numpy.array(imgencode)
# 将numpy矩阵转换成字符形式,以便在网络中传输
stringData = data.tobytes()
# 先发送要发送的数据的长度
# ljust() 方法返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串
sock.send(str.encode(str(len(stringData)).ljust(16)));
# 发送数据
sock.send(stringData);
# 读取下一帧图片
ret, frame = capture.read()
if cv2.waitKey(10) == 27:
break
sock.close()
if __name__ == '__main__':
SendPicture()
用户端
import socket
import cv2 as cv
import numpy as np
import os
import shutil
import easygui as a
redlower = np.array([0, 43, 46])
redupper = np.array([10, 255, 255])
bluelower = np.array([100, 43, 46])
blueupper = np.array([124, 255, 255])
greenlower = np.array([37, 43, 46])
greenupper = np.array([77, 255, 255])
def ReceivePicture():
address = ('10.52.130.116', 3000)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 将套接字绑定到地址, 在AF_INET下,以元组(host,port)的形式表示地址.
s.bind(address)
s.listen(1)
def recvall(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
# 接受TCP连接并返回(conn,address),其中conn是新的套接字对象,可以用来接收和发送数据。addr是连接客户端的地址。
# 没有连接则等待有连接
conn, addr = s.accept()
print('connect from:' + str(addr))
i = 1
while 1:
length = recvall(conn, 16) # 获得图片文件的长度,16代表获取长度
stringData = recvall(conn, int(length)) # 根据获得的文件长度,获取图片文件
data = np.frombuffer(stringData, np.uint8) # 将获取到的字符流数据转换成1维数组
decimg = cv.imdecode(data, cv.IMREAD_COLOR) # 将数组解码成图像
cv.imwrite(r"C:\Users\Administrator\Desktop\picturedata\test" + str(i) + ".jpg", decimg)
i += 1
if (i == 250):
s.close()
break;
def showpro():
choice = a.ccbox(msg='选择需要显示红色(1)还是绿色(2)。', title='红绿色盲辅助脚本', choices=('1', '2'))
if (choice == 1):
for i in range(1, 250):
img = cv.imread(r"C:\Users\Administrator\Desktop\picturedata\test" + str(i) + ".jpg", 1)
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
maskG = cv.inRange(hsv, lowerb=greenlower, upperb=greenupper)
maskR = cv.inRange(hsv, lowerb=redlower, upperb=redupper)
maskRG = cv.bitwise_or(maskG, maskR)
picpro = cv.bitwise_or(img, img, mask=maskR)
out = cv.GaussianBlur(picpro, (3, 3), 1.3) # σ较大,则生成的模板的各个系数相差就不是很大,比较类似均值模板,对图像的平滑效果比较明显。
cv.imshow('videopro', out)
cv.imshow('videorecv', img)
c = cv.waitKey(35)
if c == 27:
break;
else:
for i in range(1, 250):
img = cv.imread(r"C:\Users\Administrator\Desktop\picturedata\test" + str(i) + ".jpg", 1)
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
maskG = cv.inRange(hsv, lowerb=greenlower, upperb=greenupper)
maskR = cv.inRange(hsv, lowerb=redlower, upperb=redupper)
maskRG = cv.bitwise_or(maskG, maskR)
picpro = cv.bitwise_or(img, img, mask=maskG)
out = cv.GaussianBlur(picpro, (3, 3), 1.3) # σ较大,则生成的模板的各个系数相差就不是很大,比较类似均值模板,对图像的平滑效果比较明显。
cv.imshow('videopro', out)
cv.imshow('videorecv', img)
c = cv.waitKey(35)
if c == 27:
break;
if __name__ == '__main__':
ReceivePicture()
showpro()
shutil.rmtree(r"C:\Users\Administrator\Desktop\picturedata")
os.mkdir(r"C:\Users\Administrator\Desktop\picturedata")