- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello there, when i run this code on my Jupyter Notebook I'm getting this error
%%writefile person_detect.py
import numpy as np
import time
from openvino.inference_engine import IENetwork, IECore
import os
import cv2
import argparse
import sys
class Queue:
'''
Class for dealing with queues
'''
def __init__(self):
self.queues=[]
def add_queue(self, points):
self.queues.append(points)
def get_queues(self, image):
for q in self.queues:
x_min, y_min, x_max, y_max=q
frame=image[y_min:y_max, x_min:x_max]
yield frame
def check_coords(self, coords):
d={k+1:0 for k in range(len(self.queues))}
for coord in coords:
for i, q in enumerate(self.queues):
if coord[0]>q[0] and coord[2]<q[2]:
d[i+1]+=1
return d
class PersonDetect:
'''
Class for the Person Detection Model.
'''
def __init__(self, model_name, device, threshold=0.60):
self.model_weights=model_name+'.bin'
self.model_structure=model_name+'.xml'
self.device=device
self.threshold=threshold
try:
self.core = IECore()
self.model = self.core.read_network(self.model_structure, self.model_weights)
except Exception as e:
raise ValueError("Could not Initialise the network. Have you enterred the correct model path?")
self.input_name=next(iter(self.model.inputs))
self.input_shape=self.model.inputs[self.input_name].shape
self.output_name=next(iter(self.model.outputs))
self.output_shape=self.model.outputs[self.output_name].shape
def load_model(self):
'''
TODO: This method needs to be completed by you
'''
# load model
self.core = IECore()
self.net = self.core.load_network(network=self.model, device_name=self.device, num_requests=1)
print("Network already loaded")
def predict(self, image):
'''
TODO: This method needs to be completed by you
'''
input_image = image
# name of the input node
input_name = self.input_name
# Preprocess the image
proc_image = self.preprocess_input(image)
input_dict = {input_name:proc_image}
#start async inference
print('Starting async inference')
infer_request = self.net.start_async(request_id=0, inputs=input_dict)
infer_status = infer_request.wait()
if infer_status == 0:
print('waiting for inference')
output = infer_request.outputs[self.output_name]
print('Obtain coords for thresholds')
box = self.preprocess_outputs(output)
print('Lets gooooooooo!')
return self.draw_outputs(box, input_image)
print('Its done')
def draw_outputs(self, outputs, image):
'''
TODO: This method needs to be completed by you
'''
width = image.shape[1]
height = image.shape[0]
coords = []
for box in outputs[0][0]:
conf = box[2]
if conf >= self.threshold:
xmin = int(box[3] * width)
ymin = int(box[4] * height)
xmax = int(box[5] * width)
ymax = int(box[6] * height)
cv2.rectangle(image, (xmin,ymin), (xmax,ymax), (0,255,0), 1)
coords.append((xmin, ymin, xmax, ymax))
return coords, image
print('Hello Keith')
def preprocess_outputs(self, outputs):
'''
TODO: This method needs to be completed by you
'''
boxes = []
for person in outputs[0][0]:
if person[2] > self.threshold:
boxes.append([person[3:]])
return boxes
def preprocess_input(self, image):
'''
TODO: This method needs to be completed by you
'''
image_p = cv2.resize(image, (self.input_shape[3], self.input_shape[2]))
image_p = image_p.transpose((2,0,1))
image_p = image_p.reshape(1, *image_p.shape)
return image_p
def main(args):
model=args.model
device=args.device
video_file=args.video
max_people=args.max_people
threshold=args.threshold
output_path=args.output_path
start_model_load_time=time.time()
pd= PersonDetect(model, device, threshold)
pd.load_model()
total_model_load_time = time.time() - start_model_load_time
queue=Queue()
try:
queue_param=np.load(args.queue_param)
for q in queue_param:
queue.add_queue(q)
except:
print("error loading queue param file")
try:
cap=cv2.VideoCapture(video_file)
except FileNotFoundError:
print("Cannot locate video file: "+ video_file)
except Exception as e:
print("Something else went wrong with the video file: ", e)
initial_w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
initial_h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
video_len = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
out_video = cv2.VideoWriter(os.path.join(output_path, 'output_video.mp4'), cv2.VideoWriter_fourcc(*'avc1'), fps, (initial_w, initial_h), True)
counter=0
start_inference_time=time.time()
try:
while cap.isOpened():
ret, frame=cap.read()
if not ret:
break
counter+=1
coords, image= pd.predict(frame)
num_people= queue.check_coords(coords)
print(f"Total People in frame = {len(coords)}")
print(f"Number of people in queue = {num_people}")
out_text=""
y_pixel=25
for k, v in num_people.items():
out_text += f"No. of People in Queue {k} is {v} "
if v >= int(max_people):
out_text += f" Queue full; Please move to next Queue "
cv2.putText(image, out_text, (15, y_pixel), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
out_text=""
y_pixel+=40
out_video.write(image)
total_time=time.time()-start_inference_time
total_inference_time=round(total_time, 1)
fps=counter/total_inference_time
with open(os.path.join(output_path, 'stats.txt'), 'w') as f:
f.write(str(total_inference_time)+'\n')
f.write(str(fps)+'\n')
f.write(str(total_model_load_time)+'\n')
cap.release()
cv2.destroyAllWindows()
except Exception as e:
print("Could not run Inference: ", e)
if __name__=='__main__':
parser=argparse.ArgumentParser()
parser.add_argument('--model', required=True)
parser.add_argument('--device', default='CPU')
parser.add_argument('--video', default=None)
parser.add_argument('--queue_param', default=None)
parser.add_argument('--output_path', default='/results')
parser.add_argument('--max_people', default=2)
parser.add_argument('--threshold', default=0.60)
args=parser.parse_args()
main(args)
I'm getting this error
Network already loaded Starting async inference waiting for inference Obtain coords for thresholds Lets gooooooooo! Could not run Inference: invalid index to scalar variable. results/ results/manufacturing/ results/manufacturing/cpu/ results/manufacturing/cpu/output_video.mp4 stderr.log
What could be the problem with my code? Thank You
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Keith,
Thanks for reaching out. From the output snippet you have attached, it's not clear what is the problem. Can you please provide more information about the error. You can check the stderr file for the job to find the error log.
Regards.
Alaa
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello Keith,
def __init__(self, model_name, device, threshold=0.60):
self.model_weights=model_name+'.bin'
self.model_structure=model_name+'.xml'
self.device=device
self.threshold=threshold
try:
self.core = IECore()
self.model = self.core.read_network(self.model_structure, self.model_weights)
except Exception as e:
raise ValueError("Could not Initialise the network. Have you enterred the correct model path?")
In this part of your code, you have already initialized instance of the Inference Engine core while using read_network so you don't need to write again
self.core = IECore()
this line of code while loading model using Load_model function.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page