-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
84 lines (63 loc) · 2.28 KB
/
main.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
import sys
import multiprocessing
from calibrate import mainLoop as calibrateLoop
from nestris_ocr.capturing import uncached_capture
from nestris_ocr.scan_strat import Strategy
from nestris_ocr.network.network_client import NetClient
from nestris_ocr.network.cached_sender import CachedSender
from nestris_ocr.config import config
from nestris_ocr.utils.program_args import args
import nestris_ocr.utils.time as time
RATE = 1.0 / config["performance.scan_rate"]
def main(on_cap, check_network_close):
strategy = Strategy()
# The loop makes sure that the program retries constantly even when
# capturing device is having trouble
while True:
try:
read_ts = time.time()
ts, image = uncached_capture().get_image(rgb=True)
if not ts and not image:
break
except KeyboardInterrupt:
break
except Exception:
time.sleep(RATE)
continue
frame_end_ts = (ts or read_ts) + RATE
pre_strategy_ts = time.time()
strategy.update(ts, image)
result = strategy.to_dict()
if config["debug.print_benchmark"]:
elapsed_time = time.time() - (ts or read_ts)
print(f"Elapsed time since capture: {elapsed_time}")
strategy_time = time.time() - pre_strategy_ts
print(f"Strategy processing time: {strategy_time}")
on_cap(result, ts)
# error = check_network_close()
# if error is not None:
# return error
time.sleep(max(frame_end_ts - time.time(), 0))
if __name__ == "__main__":
multiprocessing.freeze_support()
if args.calibrate:
calibrateLoop()
sys.exit()
print("Creating net client...")
client = NetClient.CreateClient(config["network.host"], int(config["network.port"]))
print("Net client created.")
cachedSender = CachedSender(
client, config["debug.print_packet"], config["network.protocol"]
)
result = None
try:
print("Starting main loop")
result = main(cachedSender.sendResult, client.checkNetworkClose)
except KeyboardInterrupt:
pass
uncached_capture().stop()
if result:
print(result)
# todo: close and kill opencv thread if necessary...
client.stop()
client.join()