-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.py
86 lines (66 loc) · 2.51 KB
/
client.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 20 14:14:07 2023
@author: shree
"""
from socket import socket, AF_INET, SOCK_STREAM, gaierror
import cv2
import pickle
import struct
import sys
import converter
import hasher
from time import sleep
# waits until data is entered
def wait_change(glob_vars):
while not glob_vars["pressed"]:
sleep(1)
glob_vars["pressed"] = False
def retrieve():
global data, HEADER_SIZE
# first we receive the header (size of the pickle file)
while len(data) < HEADER_SIZE:
# the smaller the size of datapacket, the longer it takes to receive
packet = client.recv(2**10)
if not packet:
break
data += packet
msg_size = struct.unpack("Q", data[:HEADER_SIZE])[0]
data = data[HEADER_SIZE:]
while len(data) < msg_size:
data += client.recv(2**10)
pickle_data = data[:msg_size]
data = data[msg_size:] # the remainder of data received is stored for the next loop
return pickle.loads(pickle_data)
def feed_generator(glob_vars):
global data, HEADER_SIZE, client
yield b'--frame\r\n'
hashbin, im_list, bgr_avg = hasher.load_data()
while True:
try:
############# SERVER STUFF ##################
host_ip, port = glob_vars["host_ip"], glob_vars["port"]
print(glob_vars, file=sys.stderr)
client = socket(AF_INET, SOCK_STREAM)
client.connect((host_ip, port))
data = b""
HEADER_SIZE = struct.calcsize("Q")
glob_vars["message"] = "Connected!"
############# VIDEO STUFF #####################
image = retrieve()
converter.init(image.shape[0], image.shape[1], im_list, bgr_avg, hashbin, w_size=1)
while True:
image = retrieve()
converter.convert(image)
frame = cv2.imencode('.png', converter.result_image)[1].tobytes()
yield b'Content-Type: image/png\r\n\r\n' + frame + b'\r\n--frame\r\n'
except gaierror:
glob_vars["message"] = "Incorrect input"
wait_change(glob_vars)
except ConnectionRefusedError:
glob_vars["message"] = "Host busy"
wait_change(glob_vars)
except (struct.error, ConnectionResetError):
glob_vars["message"] = "Host disconnected :("
wait_change(glob_vars)