-
Notifications
You must be signed in to change notification settings - Fork 4
/
live_ncs.py
executable file
·96 lines (83 loc) · 2.86 KB
/
live_ncs.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
from time import time
import cv2
## FOR PICAMERA HARDWARE
# from picamera.array import PiRGBArray
# from picamera import PiCamera
from yolow_ncs import YolowNCS
from images import display_mess
from argparse import ArgumentParser
from utils import parse_args_from_txt
def arg_builder():
arg_parser = ArgumentParser()
arg_parser.add_argument('-c', '--config',
help='path to the model config file\
(default to data/config/ncs/yolov3_full_416.cfg)',
default='data/config/ncs_yolov3_full_416.cfg',
type=str)
arg_parser.add_argument('-r', '--requests',
help='the number of requests for \
multi-request pool feature (default to 4)',
default=2,
type=int)
return arg_parser.parse_args()
def main(args):
model_args = parse_args_from_txt(args.config)
input_size = (model_args['width'], model_args['height'])
yl_ncs = YolowNCS(model_args['model_name_path'],
input_size,
model_args['labels'],
args.requests)
fps_display_interval = 3
frame_rate = 0
frame_count = 0
async_mode = True
## FOR PICAMERA HARDWARE
# cam = PiCamera()
# cam.resolution = (720, 480)
# cam.framerate = 30
# rawCapture = PiRGBArray(cam)
##
## FOR OTHER CASES
cam = cv2.VideoCapture(-1)
##
start_time = time()
## FOR PICAMERA HARDWARE
# for frame in cam.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# frame = frame.array
##
## FOR OTHER CASES
while cam.isOpened():
_, frame = cam.read()
##
# temporary fix for error VIDIOC_DQBUF: Resource temporarily unavailable.
if frame == None:
continue
yl_ncs.set_input(frame)
# start = time()
# in async mode, some beginnig frames causes nan values which breaks the programme
# skip it for now
try:
frame = yl_ncs.predict(async_mode=async_mode)[0]
except:
pass
duration = time() - start_time
if duration > fps_display_interval:
frame_rate = round(frame_count/duration)
start_time = time()
frame_count = 0
mode = "async" if async_mode else "sync"
status = '{} fps - mode: {}'.format(frame_rate, mode)
frame = display_mess(frame, status)
cv2.imshow('NCS YOLOv3 Live', frame)
frame_count += 1
key = cv2.waitKey(1)
if key & 0xFF==ord('q'):
break
if key & 0xFF==ord('m'):
async_mode = not async_mode
## FOR OTHER CASES
cam.release()
##
cv2.destroyAllWindows()
if __name__ == "__main__":
main(arg_builder())