-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPsychoClient.py
131 lines (112 loc) · 4.06 KB
/
PsychoClient.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
#!/usr/bin/env python3
from __future__ import division
import requests
import time
import numpy as np
from PIL import Image
class PsychoClient():
def __init__(self, ip, port, verbose=True):
self.address = f'http://{ip}:{port}'
self.session = requests.Session()
self.verbose = verbose
self.session.verify = True
def send_command(self, command='noop', timeout=0.2):
url = self.address + '/'
data = {'control': command}
try:
req = self.session.post(url, json=data, timeout=timeout)
if req.ok and req.headers['Content-Type'] == 'application/json':
state = req.json()
else:
state = {
'code': req.status_code,
'content': req.content,
}
except Exception as e:
state = str(e)
return state
def get_state(self, timeout=0.2):
url = self.address + '/status'
try:
req = self.session.get(url, timeout=timeout)
if req.ok and req.headers['Content-Type'] == 'application/json':
state = req.json()
else:
state = {
'code': req.status_code,
'content': req.content,
}
except Exception as e:
state = str(e)
return state
def wait_state(self, state, interval=0.2, timeout=0.2):
url = self.address + '/status'
while True:
try:
req = self.session.get(url, timeout=timeout)
if self.verbose:
print(req.content)
if req.ok and req.headers['Content-Type'] == 'application/json':
ret = req.json()
if 'state' in ret:
if isinstance(state, str) and ret['state'] == state:
return ret
if isinstance(state, list) and ret['state'] in state:
return ret
# except requests.ConnectionError as e:
except Exception as e:
if self.verbose:
print(e)
time.sleep(interval)
def get_frame(self, timeout=0.2):
# url = self.address + '/frame'
url = self.address + '/image'
try:
req = self.session.get(url, stream=True, timeout=timeout)
print(req.headers)
if req.ok and req.headers['Content-Type'] == 'image/png':
req.raw.decode_content = True
img = Image.open(req.raw, formats=['png'])
frame = np.array(img)
else:
if self.verbose:
print(req.content)
frame = None
except Exception as e:
if self.verbose:
print(e)
frame = None
return frame
def loop_frame(self, callback: callable, interval=0.1, timeout=0.2):
# url = self.address + '/frame'
url = self.address + '/image'
while True:
try:
req = self.session.get(url, stream=True, timeout=timeout)
if req.ok and req.headers['Content-Type'] == 'image/png':
req.raw.decode_content = True
img = Image.open(req.raw, formats=['png'])
frame = np.array(img)
callback(frame)
else:
if self.verbose:
print(req.content)
time.sleep(interval)
except Exception as e:
if self.verbose:
print(e)
time.sleep(interval)
def main():
import logging
logging.basicConfig(level=logging.DEBUG)
client = PsychoClient('127.0.0.1', 8080)
client.wait_state(['init', 'waiting'])
while True:
state = client.get_state()
print(state)
if isinstance(state, str):
# should be some exception, e.g. server is down, so
break
time.sleep(2)
if __name__ == '__main__':
main()