Skip to content

Commit 0cea8cd

Browse files
Merge pull request avinashkranjan#2274 from avinashkranjan/deepsource-transform-e8cf565e
format code with autopep8
2 parents c7225cb + 798edf7 commit 0cea8cd

File tree

4 files changed

+134
-76
lines changed

4 files changed

+134
-76
lines changed

IOT/Jupyter/inference.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ def load_model(self, model, device, input_size, output_size, num_requests, cpu_e
8989

9090
if num_requests == 0:
9191
# Loads network read from IR to the plugin
92-
self.net_plugin = self.plugin.load_network(network=self.net, device_name=device)
92+
self.net_plugin = self.plugin.load_network(
93+
network=self.net, device_name=device)
9394
else:
94-
self.net_plugin = self.plugin.load_network(network=self.net, num_requests=num_requests, device_name=device)
95+
self.net_plugin = self.plugin.load_network(
96+
network=self.net, num_requests=num_requests, device_name=device)
9597
# log.error("num_requests != 0")
9698

9799
self.input_blob = next(iter(self.net.inputs))
@@ -129,7 +131,7 @@ def exec_net(self, request_id, frame):
129131
"""
130132

131133
self.infer_request_handle = self.net_plugin.start_async(
132-
request_id=request_id, inputs={self.input_blob: frame})
134+
request_id=request_id, inputs={self.input_blob: frame})
133135
return self.net_plugin
134136

135137
def wait(self, request_id):

IOT/Jupyter/intruder_detector_jupyter.py

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
is_async_mode = True
6666

6767
# Event class to store the intruder details
68+
69+
6870
class Event:
6971
def __init__(self, event_time=None, intruder=None, count=None, frame=None):
7072
self.time = event_time
@@ -176,6 +178,7 @@ def parse_args():
176178
else:
177179
is_async_mode = True
178180

181+
179182
def check_args():
180183
"""
181184
Validate the command line arguments
@@ -249,17 +252,20 @@ def get_input():
249252
labels = []
250253
streams = []
251254

252-
assert os.path.isfile(CONFIG_FILE), "{} file doesn't exist".format(CONFIG_FILE)
255+
assert os.path.isfile(
256+
CONFIG_FILE), "{} file doesn't exist".format(CONFIG_FILE)
253257
config = json.loads(open(CONFIG_FILE).read())
254258
for id, item in enumerate(config['inputs']):
255259
for idx, video in enumerate(item['video']):
256260
cams = idx + 1
257261
cam_name = "Cam {}".format(idx)
258262
if video.isdigit():
259-
video_cap = VideoCap(cv2.VideoCapture(int(video)), cam_name, cams, is_cam=True)
263+
video_cap = VideoCap(cv2.VideoCapture(
264+
int(video)), cam_name, cams, is_cam=True)
260265
else:
261266
if os.path.isfile(video):
262-
video_cap = VideoCap(cv2.VideoCapture(video), cam_name, cams, is_cam=False)
267+
video_cap = VideoCap(cv2.VideoCapture(
268+
video), cam_name, cams, is_cam=False)
263269
else:
264270
return [-8, [video]]
265271
video_caps.append(video_cap)
@@ -302,16 +308,22 @@ def save_json():
302308
for i in range(events_size):
303309
event_json.write("\t\t\"%d\":{\n" % (i))
304310
event_json.write("\t\t\t\"time\":\"%s\",\n" % events[i].time)
305-
event_json.write("\t\t\t\"content\":\"%s\",\n" % events[i].intruder)
306-
event_json.write("\t\t\t\"videoTime\":\"%d\"\n" % float(events[i].frame / fps))
311+
event_json.write("\t\t\t\"content\":\"%s\",\n" %
312+
events[i].intruder)
313+
event_json.write("\t\t\t\"videoTime\":\"%d\"\n" %
314+
float(events[i].frame / fps))
307315
event_json.write("\t\t},\n")
308-
data_json.write("\t\t\"%d\": \"%d\",\n" % (float(events[i].frame / fps), events[i].count))
316+
data_json.write("\t\t\"%d\": \"%d\",\n" %
317+
(float(events[i].frame / fps), events[i].count))
309318
event_json.write("\t\t\"%d\":{\n" % events_size)
310319
event_json.write("\t\t\t\"time\":\"%s\",\n" % events[events_size].time)
311-
event_json.write("\t\t\t\"content\":\"%s\",\n" % events[events_size].intruder)
312-
event_json.write("\t\t\t\"videoTime\":\"%d\"\n" % float(events[events_size].frame / fps))
320+
event_json.write("\t\t\t\"content\":\"%s\",\n" %
321+
events[events_size].intruder)
322+
event_json.write("\t\t\t\"videoTime\":\"%d\"\n" %
323+
float(events[events_size].frame / fps))
313324
event_json.write("\t\t}\n")
314-
data_json.write("\t\t\"%d\": \"%d\"\n" % (float(events[events_size].frame / fps), events[events_size].count))
325+
data_json.write("\t\t\"%d\": \"%d\"\n" % (
326+
float(events[events_size].frame / fps), events[events_size].count))
315327
total = events[events_size].count
316328
event_json.write("\t}\n")
317329
event_json.write("}")
@@ -350,13 +362,17 @@ def arrange_windows():
350362
rows += 1
351363
cols = 1
352364
cv2.namedWindow(video_caps[idx].cam_name, cv2.WINDOW_NORMAL)
353-
cv2.resizeWindow(video_caps[idx].cam_name, window_width, window_height)
354-
cv2.moveWindow(video_caps[idx].cam_name, spacer * cols, row_spacer * rows)
365+
cv2.resizeWindow(video_caps[idx].cam_name,
366+
window_width, window_height)
367+
cv2.moveWindow(video_caps[idx].cam_name,
368+
spacer * cols, row_spacer * rows)
355369
else:
356370
cols += 1
357371
cv2.namedWindow(video_caps[idx].cam_name, cv2.WINDOW_NORMAL)
358-
cv2.resizeWindow(video_caps[idx].cam_name, window_width, window_height)
359-
cv2.moveWindow(video_caps[idx].cam_name, spacer * cols, row_spacer * rows)
372+
cv2.resizeWindow(video_caps[idx].cam_name,
373+
window_width, window_height)
374+
cv2.moveWindow(video_caps[idx].cam_name,
375+
spacer * cols, row_spacer * rows)
360376

361377

362378
# Signal handler
@@ -446,14 +462,16 @@ def intruder_detector():
446462
# Initializing VideoWriter for each source
447463
if UI and not LOOP_VIDEO:
448464
for video_cap in video_caps:
449-
ret, ret_value = video_cap.init_vw(int(video_cap.input_height), int(video_cap.input_width))
465+
ret, ret_value = video_cap.init_vw(
466+
int(video_cap.input_height), int(video_cap.input_width))
450467
if ret != 0:
451468
return ret, ret_value
452469

453470
# Initialise the class
454471
infer_network = Network()
455472
# Load the network to IE plugin to get shape of input layer
456-
n, c, h, w = infer_network.load_model(model_xml, TARGET_DEVICE, 1, 1, 2, CPU_EXTENSION)[1]
473+
n, c, h, w = infer_network.load_model(
474+
model_xml, TARGET_DEVICE, 1, 1, 2, CPU_EXTENSION)[1]
457475
# Arrange windows so that they are not overlapping
458476
arrange_windows()
459477

@@ -488,7 +506,8 @@ def intruder_detector():
488506
if no_more_data[idx]:
489507
stream_end_frame = numpy.zeros((int(video_cap.input_height), int(video_cap.input_width), 1),
490508
dtype='uint8')
491-
stream_end_message = "Stream from {} has ended.".format(video_cap.cam_name)
509+
stream_end_message = "Stream from {} has ended.".format(
510+
video_cap.cam_name)
492511
cv2.putText(stream_end_frame, stream_end_message, (int(video_cap.input_width / 2) - 30,
493512
int(video_cap.input_height / 2) - 30),
494513
cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 255), 1)
@@ -514,7 +533,8 @@ def intruder_detector():
514533
# Async enabled and more than one video capture
515534
else:
516535
# Get previous index
517-
videoCapResult = video_caps[idx - 1 if idx - 1 >= 0 else len(video_caps) - 1]
536+
videoCapResult = video_caps[idx -
537+
1 if idx - 1 >= 0 else len(video_caps) - 1]
518538

519539
else:
520540
in_frame = cv2.resize(video_cap.frame, (w, h))
@@ -541,7 +561,8 @@ def intruder_detector():
541561
xmax = int(obj[5] * videoCapResult.input_width)
542562
ymax = int(obj[6] * videoCapResult.input_height)
543563
# Draw bounding box around the intruder detected
544-
cv2.rectangle(videoCapResult.frame, (xmin, ymin), (xmax, ymax), (0, 255, 0), 4, 16)
564+
cv2.rectangle(videoCapResult.frame, (xmin, ymin),
565+
(xmax, ymax), (0, 255, 0), 4, 16)
545566

546567
for i in range(videoCapResult.no_of_labels):
547568
if videoCapResult.candidate_count[i] == videoCapResult.current_count[i]:
@@ -557,8 +578,10 @@ def intruder_detector():
557578
continue
558579

559580
if videoCapResult.current_count[i] > videoCapResult.last_correct_count[i]:
560-
videoCapResult.total_count[i] += videoCapResult.current_count[i] - videoCapResult.last_correct_count[i]
561-
det_objs = videoCapResult.current_count[i] - videoCapResult.last_correct_count[i]
581+
videoCapResult.total_count[i] += videoCapResult.current_count[i] - \
582+
videoCapResult.last_correct_count[i]
583+
det_objs = videoCapResult.current_count[i] - \
584+
videoCapResult.last_correct_count[i]
562585
total_count = sum(videoCapResult.total_count)
563586
for det_obj in range(det_objs):
564587
current_time = time.strftime("%H:%M:%S")
@@ -570,14 +593,17 @@ def intruder_detector():
570593
frame=videoCapResult.frame_count)
571594
videoCapResult.events.append(event)
572595

573-
snapshot_name = "output/intruder_{}.png".format(total_count)
596+
snapshot_name = "output/intruder_{}.png".format(
597+
total_count)
574598
cv2.imwrite(snapshot_name, videoCapResult.frame)
575599
videoCapResult.last_correct_count[i] = videoCapResult.current_count[i]
576600

577601
# Create intruder log window, add logs to the frame and display it
578-
log_window = numpy.zeros((LOG_WIN_HEIGHT, LOG_WIN_WIDTH, 1), dtype='uint8')
602+
log_window = numpy.zeros(
603+
(LOG_WIN_HEIGHT, LOG_WIN_WIDTH, 1), dtype='uint8')
579604
for i, log in enumerate(log_list):
580-
cv2.putText(log_window, log, (10, 20 * i + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
605+
cv2.putText(log_window, log, (10, 20 * i + 15),
606+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
581607
cv2.imshow("Intruder Log", log_window)
582608
videoCapResult.frame_count += 1
583609

@@ -620,7 +646,8 @@ def intruder_detector():
620646

621647
if cv2.waitKey(1) == 9:
622648
is_async_mode = not is_async_mode
623-
print("Switched to {} mode".format("async" if is_async_mode else "sync"))
649+
print("Switched to {} mode".format(
650+
"async" if is_async_mode else "sync"))
624651

625652
if False not in no_more_data:
626653
break
@@ -670,7 +697,8 @@ def intruder_detector():
670697
elif status == -14:
671698
print("No input source found in configuration file!")
672699
elif status == -15:
673-
print("Error: No labels currently in use. Please edit " + CONFIG_FILE+" file!")
700+
print("Error: No labels currently in use. Please edit " +
701+
CONFIG_FILE+" file!")
674702
elif status == -16:
675703
print("Error in opening intruder log file!")
676704
elif status == -17:

IOT/application/inference.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ def load_model(self, model, device, input_size, output_size, num_requests, cpu_e
8989

9090
if num_requests == 0:
9191
# Loads network read from IR to the plugin
92-
self.net_plugin = self.plugin.load_network(network=self.net, device_name=device)
92+
self.net_plugin = self.plugin.load_network(
93+
network=self.net, device_name=device)
9394
else:
94-
self.net_plugin = self.plugin.load_network(network=self.net, num_requests=num_requests, device_name=device)
95+
self.net_plugin = self.plugin.load_network(
96+
network=self.net, num_requests=num_requests, device_name=device)
9597

9698
self.input_blob = next(iter(self.net.inputs))
9799
self.out_blob = next(iter(self.net.outputs))

0 commit comments

Comments
 (0)