-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
79 lines (58 loc) · 1.71 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
import cv2
import libjevois
import Bunnybot2018
import argparse
import importlib
display_output = True
video_capture = None
retval = None
def parse_args():
parser = argparse.ArgumentParser(description="Emulator for JeVois camera")
parser.add_argument("--module", help="Name of vision module", default="Vision")
args = parser.parse_args()
return args
def load_vision_module(args):
module = importlib.import_module(args.module)
vision_class = getattr(module, args.module)
return vision_class()
def handle_output_toggle(val):
global display_output
display_output = val == 0
def create_window(window_title: str):
cv2.namedWindow(window_title)
cv2.createTrackbar("Output", window_title, 0, 1, handle_output_toggle)
def init_video():
global video_capture
global retval
video_capture = cv2.VideoCapture(0)
if video_capture.isOpened():
retval, _ = video_capture.read()
else:
print("Couldn't read frame from camera.")
retval = False
def release():
global video_capture
video_capture.release()
cv2.destroyAllWindows()
window_title = "JeVois Emulator"
args = parse_args()
vision = load_vision_module(args)
create_window(window_title)
init_video()
try:
while retval:
retval, frame = video_capture.read()
inframe = libjevois.Inframe(frame)
outframe = libjevois.Outframe()
if display_output:
vision.process(inframe, outframe)
cv2.imshow(window_title, outframe.image)
else:
vision.processNoUSB(inframe)
cv2.imshow(window_title, frame)
key = cv2.waitKey(20)
if key == 27:
break
except KeyboardInterrupt:
pass
release()