-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
126 lines (113 loc) · 3.63 KB
/
client.py
File metadata and controls
126 lines (113 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018-05-30 20:00 下午
# @Author : HuangLi
# @Contact : lhuang9703@gmail.com
# @Site :
# @File : client.py
# @Software: PyCharm Community Edition
import sys
import time
from socket import *
import threading
import struct
import pickle
import zlib
import numpy as np
import cv2
HOST = "127.0.0.1"
PORT1 = 6666
PORT2 = 9999
class VideoClient():
def __init__(self):
super(VideoClient, self).__init__()
self.sock = socket(AF_INET, SOCK_STREAM)
while True:
try:
self.sock.connect((HOST, PORT1))
break
except:
time.sleep(2)
continue
print("connection with server success")
client = threading.Thread(target=self.senddata)
server = threading.Thread(target=self.showdata)
client.start()
server.start()
def senddata(self):
print("in senddata")
data = "".encode("utf-8")
size = struct.calcsize("L")
# self.cap = cv2.VideoCapture(0)
self.cap = cv2.VideoCapture(r"D:\\360data\重要数据\桌面\杂\《小王》.mp4")
while self.cap.isOpened():
ret, frame = self.cap.read()
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# cv2.imshow('frame', gray)
frame = cv2.resize(frame, (0,0), fx=1, fy=1)
data = pickle.dumps(frame)
zdata = zlib.compress(data, zlib.Z_BEST_COMPRESSION)
try:
# msg = input()
self.sock.sendall(struct.pack("L", len(zdata)) + zdata)
# print("send ", len(zdata))
except:
print('exception')
break
for i in range(0):
self.cap.read()
def showdata(self):
data = "".encode("utf-8")
size = struct.calcsize("L")
cv2.namedWindow('remote', cv2.WINDOW_GUI_NORMAL)
cv2.resizeWindow('remote', 320, 240)
while True:
while len(data) < size:
data = data + self.sock.recv(81920)
packed_size = data[:size]
data = data[size:]
msg_size = struct.unpack("L", packed_size)[0]
while len(data) < msg_size:
data = data + self.sock.recv(81920)
zframe_data = data[:msg_size]
data = data[msg_size:]
frame_data = zlib.decompress(zframe_data)
frame = pickle.loads(frame_data)
try:
cv2.imshow('remote', frame)
if cv2.waitKey(100) & 0xFF == ord('q'):
break
except:
pass
# print('server said:' + '\n' + data)
self.cap.release()
cv2.destroyAllWindows()
class TextClient():
def __init__(self):
super(TextClient, self).__init__()
self.sock = socket(AF_INET, SOCK_STREAM)
while True:
try:
self.sock.connect((HOST, PORT1))
break
except:
time.sleep(2)
continue
print("connection with server success")
client = threading.Thread(target=self.senddata)
server = threading.Thread(target=self.showdata)
client.start()
server.start()
def senddata(self):
print("in sendtext")
while True:
msg = input()
msg = '000' + msg
self.sock.send(msg.encode())
def showdata(self):
while True:
data = self.sock.recv(1024).decode()
print('server said:' + '\n' + data)
if __name__ == '__main__':
VideoClient()
TextClient()