-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.py
More file actions
153 lines (126 loc) · 4.89 KB
/
Copy pathclient.py
File metadata and controls
153 lines (126 loc) · 4.89 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
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
import socket
from sys import argv
import cv2
import mediapipe as mp
import itertools
import numpy as np
import time
import sys
from multiprocessing import Queue, Process
from queue import Empty
import atexit
from math import ceil
from collections import deque
sys.path.insert(1, './tools')
import holistic, common, encrypt
PRINT_FREQ = 30
SERVER_ADDR = "35.243.169.18"
# SERVER_ADDR = "127.0.0.1"
# Server IP address and Port number
serverAddressPort = (SERVER_ADDR, 9999)
APP_NAME = "SignSense"
# send landmarks and receive predictions from server continuously
def server(landmark_queue, prediction_queue):
common.print_debug_banner("STARTED SERVER")
UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
UDPClientSocket.setblocking(0)
while True:
try:
landmark = landmark_queue.get()
encrypted_landmark = encrypt.encrypt_chacha(landmark)
# Send message to server using created UDP socket
UDPClientSocket.sendto(encrypted_landmark, serverAddressPort)
# Receive message from the server
msgFromServer = UDPClientSocket.recvfrom(2048)[0]
raw_data = encrypt.decrypt_chacha(msgFromServer)
prediction_queue.put(raw_data)
except encrypt.DecryptionError:
print(f"tried to decrypt {msgFromServer}")
except socket.error as e:
# print(f"SOCKET EXCEPTION: {e}")
pass
except Exception as e:
print(f"SERVER EXCEPTION: {e}")
pass
def video_loop(landmark_queue, prediction_queue, use_holistic=False):
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
cap.set(cv2.CAP_PROP_FOURCC, fourcc)
if not cap.isOpened():
print("Error opening Camera")
fps = cap.get(cv2.CAP_PROP_FPS)
print("Webcam FPS = {}".format(fps))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
mp_drawing = mp.solutions.drawing_utils
timestamp = None
started = False
predicted = None
initialized = False
delay = 0
pred_history = deque([" "]*5, 5)
pdecay = time.time()
print("starting image cap")
for image, results in holistic.process_capture(cap, use_holistic):
window_state = cv2.getWindowProperty(APP_NAME, 0)
if started and window_state == -1:
print("QUITTING")
break
started = True
newtime = time.time()
if timestamp is not None:
diff = newtime - timestamp
# Uncomment to print time between each frame
# print(diff)
timestamp = newtime
row = holistic.to_landmark_row(results, use_holistic)
landmark_str = ','.join(np.array(row).astype(np.str))
# send comma delimited str of flattened landmarks in bytes to server
try:
landmark_queue.put_nowait(landmark_str)
except Exception as e:
print(e)
try:
out = prediction_queue.get_nowait()
# toggle the server status flag on first message received
if out and not initialized:
initialized = True
common.print_debug_banner("SENDING ACK TO SERVER FOR CONNECTION")
# send a one-time ACK to toggle server connected status
landmark_queue.put_nowait("ACK")
if delay >= PRINT_FREQ:
if out and out != pred_history[-1] and out != "None":
pred_history.append(out)
pdecay = time.time()
delay = 0
except:
pass
delay += 1
if time.time() - pdecay > 7:
pred_history = deque([" "]*5, 5)
holistic.draw_landmarks(image, results, use_holistic, ' '.join(pred_history))
if initialized:
cv2.circle(image,(20,450), 10, (0,255,0), -1)
cv2.putText(image,'online',(40,458), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,255,255),2,cv2.LINE_AA)
cv2.imshow(APP_NAME, image)
else:
cv2.circle(image,(20,450), 10, (0,0,255), -1)
cv2.putText(image,'connecting',(40,458), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,255,255),2,cv2.LINE_AA)
cv2.imshow(APP_NAME, image)
cap.release()
cv2.destroyAllWindows()
# send termination message to server
landmark_queue.put("END")
if __name__ == "__main__":
# queue containing the returned predictions from the server
landmark_queue, prediction_queue = Queue(), Queue()
# start separate process for the webcam video GUI
server_p = Process(target=server, args=(landmark_queue, prediction_queue, ))
server_p.daemon = True
atexit.register(common.exit_handler, server_p)
server_p.start()
video_p = Process(target=video_loop, args=(landmark_queue, prediction_queue, ))
video_p.daemon = True
atexit.register(common.exit_handler, video_p)
video_p.start()
video_p.join()