-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
161 lines (140 loc) · 7.07 KB
/
server.py
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# coding:utf-8
import socket
import cv2
import numpy as np
import time
pick_start = False
def coor_tran(pos):
for i in range(int(len(pos) / 2)):
pos[2 * i] = int( pos[2 * i] * 1600 / 640 )
pos[2 * i + 1] = int( pos[2 * i + 1] * 900 / 480 )
return pos
def draw_x(img, x, y, line):
cv2.line(img, (x + int(line/2), y + int(line/2)),(x - int(line/2), y - int(line/2)), (255, 255, 255), 10)
cv2.line(img, (x - int(line / 2), y + int(line / 2)), (x + int(line / 2), y - int(line / 2)), (255, 255, 255), 10)
return img
def draw_arrow(img, x1, y1, x2, y2, line):
cv2.arrowedLine(img, (int(0.8*x1 + 0.2*x2), int(0.8*y1 + 0.2*y2)), (int(0.2*x1 + 0.8*x2), int(0.2*y1+0.8*y2)),(255, 255, 255), line)
return img
img = np.zeros((900, 1600, 3), np.uint8) * 255
cv2.circle(img, (int(1600*428/640),int(207*900/480)), 40, (255, 255, 255), -1)
img = draw_x(img,int(325*1600/640),int(420*900/480), 40)
img = draw_arrow(img, int(1600*325/640), int(420*900/480), int(1600*207/640),int(207*900/480),5)
cv2.namedWindow("projector", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("projector", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow("projector", img)
cv2.waitKey(0)
img = np.zeros((900, 1600, 3), np.uint8) * 255
cv2.circle(img, (int(1600*428/640),int(207*900/480)), 40, (255, 255, 255), -1)
cv2.circle(img, (int(1600*428/640+80),int(207*900/480+80)), 40, (255, 255, 255), -1)
img = draw_x(img,int(207*1600/640),int(207*900/480), 40)
img = draw_arrow(img, int(1600*428/640+40), int(207*900/480+40), int(1600*207/640),int(207*900/480),5)
cv2.namedWindow("projector", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("projector", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow("projector", img)
cv2.waitKey(0)
img = np.zeros((900, 1600, 3), np.uint8) * 255
cv2.circle(img, (int(1600*428/640),int(207*900/480)), 100, (255, 255, 255), 5)
img = draw_x(img,int(207*1600/640),int(207*900/480), 40)
img = draw_arrow(img, int(1600*428/640), int(207*900/480), int(1600*207/640),int(207*900/480),5)
cv2.namedWindow("projector", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("projector", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow("projector", img)
cv2.waitKey(0)
img = np.zeros((900, 1600, 3), np.uint8) * 255
cv2.circle(img, (int(1600*200/640),int(400*900/480)), 70, (255, 255, 255), 5)
img = draw_x(img,int(207*1600/640),int(207*900/480), 40)
img = draw_arrow(img, int(1600*200/640), int(400*900/480), int(1600*207/640),int(207*900/480),5)
cv2.namedWindow("projector", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("projector", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow("projector", img)
cv2.waitKey(0)
img = np.zeros((900, 1600, 3), np.uint8) * 255
cv2.circle(img, (int(1600*500/640),int(400*900/480)), 40, (255, 255, 255), -1)
cv2.circle(img, (int(1600*500/640+80),int(400*900/480)), 40, (255, 255, 255), -1)
img = draw_x(img,int(207*1600/640),int(207*900/480), 40)
img = draw_arrow(img, int(1600*500/640+40), int(400*900/480), int(1600*207/640),int(207*900/480),5)
cv2.namedWindow("projector", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("projector", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow("projector", img)
cv2.waitKey(0)
img = np.zeros((900, 1600, 3), np.uint8) * 255
cv2.namedWindow("projector", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("projector", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow("projector", img)
cv2.waitKey(0)
server = socket.socket() # 默认是AF_INET、SOCK_STREAM
server.bind(('10.194.47.21',6868)) # 将主机号与端口绑定到套接字
server.listen() # 设置并启动TCP监听器
pos = []
while True:
conn,addr = server.accept() # 被动接受TCP连接,一直等待连接到达
data = conn.recv(10) # 接收TCP消息,并制定最大长度
coor = str(data)
print(coor)
coor = coor[2:-1]
coor = int(coor)
if coor == -1 or coor == 1 or coor == 2 or coor == 0 or coor == 8:
pos = coor_tran(pos)
print(pos)
if pos is not None:
if coor == -1:
if not pick_start:
img = np.zeros((900, 1600, 3), np.uint8) * 255
for i in range(int(len(pos) / 2)):
cv2.circle(img, (pos[2*i] + 20, pos[2*i + 1]), 40, (255, 255, 255), -1) # 修改最后一个参数
cv2.namedWindow("projector", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("projector", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow("projector", img)
cv2.waitKey(20)
pos = []
elif coor == 1:
pick_start = True
img = np.zeros((900, 1600, 3), np.uint8) * 255
for i in range(int(len(pos) / 2)):
cv2.rectangle(img, (pos[2*i]-30, pos[2*i + 1]-30), (pos[2*i]+30, pos[2*i+1]+ 30), (255, 255, 255), -1)
#cv2.circle(img, (pos[2*i], pos[2*i + 1]), 35, (0, 0, 255), -1) # 修改最后一个参数
cv2.namedWindow("projector", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("projector", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow("projector", img)
cv2.waitKey(20)
pos = []
elif coor == 2:
if pick_start:
for i in range(int(len(pos) / 2)):
img = draw_x(img,pos[2*i],pos[2*i + 1], 40) # 修改最后一个参数
pick_start = False
cv2.namedWindow("projector", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("projector", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow("projector", img)
cv2.waitKey(20)
time.sleep(5)
pos = []
elif coor == 0:
pick_start = False
img = np.zeros((900, 1600, 3), np.uint8) * 255
cv2.namedWindow("projector", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("projector", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow("projector", img)
cv2.waitKey(20)
pos = []
elif coor == 8:
pick_start = False
img = np.zeros((900, 1600, 3), np.uint8) * 255
i = 0
cv2.circle(img, (pos[2 * i] + 20, pos[2 * i + 1]), 100, (255, 255, 255), 5)
cv2.namedWindow("projector", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("projector", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow("projector", img)
cv2.waitKey(20)
pos = []
else:
if not pick_start:
img = np.zeros((900, 1600, 3), np.uint8) * 255
cv2.namedWindow("projector", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("projector", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow("projector", img)
cv2.waitKey(20)
pos = []
else:
pos.append(coor)