C++端(客户端client)
#include
#include
#pragma comment(lib,"ws2_32.lib")
#pragma warning(disable:4996)
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
#define BuffSize 1024
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
return -1;
}
if (LOBYTE(wsaData.wVersion) != 1 ||
HIBYTE(wsaData.wVersion) != 1) {
WSACleanup();
return -1;
}
Mat imgSrc = imread("F:\PycharmProjects\Facial\testface\5.jpg", IMREAD_UNCHANGED);
if (imgSrc.empty())
{
return -1;
}
Mat img;
img.create(imgSrc.size(), imgSrc.type());
img = imgSrc.clone();
int totalNum = img.rows*img.cols*img.elemSize();
while (true)
{
SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//本机
//addrSrv.sin_addr.S_un.S_addr = inet_addr("192.168.1.107");
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(8888);
int sec = connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
if (SOCKET_ERROR == sec)
{
continue;
}
char* pos = (char*)img.data;
int colByte = img.cols*img.channels() * sizeof(uchar);
for (int i = 0; i < img.rows; i++)
{
char* data = (char*)img.ptr<uchar>(i); //第i行首地址
int sedNum = 0;
char buf[BuffSize] = { 0 };
//按行拆分,发送数据
while (sedNum < colByte)
{
int sed = (BuffSize < colByte - sedNum) ? BuffSize : (colByte - sedNum);
memcpy(buf, &data[sedNum], sed);
int SendSize = send(sockClient, buf, sed, 0);
if (SOCKET_ERROR == SendSize)
return -1;
/*else
cout << SendSize << endl;*/
sedNum += SendSize;
}
}
char recvBuf[50];
int ns = recv(sockClient, recvBuf, 50, 0);
recvBuf[ns] = ';'printf
("severRecv %s\n",) recvBuf;Sleep
(5);char
[ t2];::
std;cin >> t}
WSACleanup
();getchar
();return
0 ;}
import
python端(service)
as numpy import np
import socket
from time
import PIL import Image
# 图像尺寸 cv2
=
wid 512 =
hig 384 =
channels 3 def
recvall (,sock) count:=
buf b'' while
: count=
newbuf . sock(recv)countif
not : newbufreturn None +=
buf -= newbuf
count len ()newbufreturn
if buf
== __name__ '__main__' :=
server . socket(socket.socket,AF_INET. socket)SOCK_STREAM.
server(bind("localhost",8888 )).
server(listen0)=
totleNum * wid * hig # 总共多少数据 channels while
True :,
connection= address . server(accept)print
(,connection) address=
stringData ( recvall,connectionint ()totleNum)# data = np.fromstring(stringData, dtype='uint8')
=
data . np(frombuffer,stringData= dtype'uint8')print
(len()data)# 解析数据
# 方法一
=
Array . data(reshape,hig, wid) channels=
im . Image(fromarray)Array.
im(save"out.jpg").
im(show"test")# 方法二
# decimg = np.zeros((hig, wid, channels), np.uint8)
# for i in range(0, hig):
# for j in range(0, wid):
# for k in range(0, channels):
# decimg[i][j][k] = data[(i * wid + j) * channels + k]
#
# # cv2.imwrite('.\SERVER.jpg', decimg)
# cv2.namedWindow("test", 0)
# cv2.imshow("test", decimg)
# cv2.waitKey(0)
.
connection(sendbytes("clientRecv: ok",= encoding"ascii")).
time(sleep10).
connection(close)input
("enter end")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)