class OpenVino: def __init__(self, config): #Numbers to keep track of process ID self.curr = 0 self.next = 1 self.width, self.height = config.frame_width, config.frame_height try: self.plugin = IEPlugin(device=config.default_device) self.net = IENetwork(model=config.network_file, weights=config.weights_file) except RuntimeError: print( "We're probably dealing with a mac here, do some cpu target stuff" ) self.plugin = IEPlugin(device=config.fallback_device) self.net = IENetwork(model=config.network_file, weights=config.weights_file) supported_layers = self.plugin.get_supported_layers(self.net) not_supported_layers = [ l for l in self.net.layers.keys() if l not in supported_layers ] if len(not_supported_layers) != 0: raise Exception( "Some layers in the mdoel are not supported by the CPU - figure this out" ) #Get sizes for image pre-processing self.input_blob = next(iter(self.net.inputs)) self.output_blob = next(iter(self.net.outputs)) self.n, self.c, self.h, self.w = self.net.inputs[self.input_blob].shape self.threshold = config.threshold if config.mode == 'classify': self.threshold = [config.threshold, config.non_threshold] self.normalize = config.normalize self.exec_net = self.plugin.load(network=self.net) #Do we need this? del self.net
def movidius(frameCount): global outputFrame, lock, _reset ct = CentroidTracker() args = argsparser() #ใช้ Log แทน Print log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout) #Funtion Select cam_select, cam_rtsp, min_conf_threshold, notify, detect_list, resW, resH = select_option( ) # warmup if cam_select == 0: vs = cv2.VideoCapture(0) else: vs = cv2.VideoCapture(cam_rtsp) imW, imH = int(resW), int(resH) vs.set(3, imW) vs.set(4, imH) time.sleep(0.2) ################## #โหลดโมดูล model_xml = args.model model_bin = os.path.splitext(model_xml)[0] + ".bin" log.info("Initializing plugin for {} device...".format(args.device)) plugin = IEPlugin(device=args.device, plugin_dirs=args.plugin_dir) if args.cpu_extension and 'CPU' in args.device: plugin.add_cpu_extension('args.cpu_extension') # Read IR log.info("Reading IR...") net = IENetwork(model=model_xml, weights=model_bin) if plugin.device == "CPU": supported_layers = plugin.get_supported_layers(net) not_supported_layers = [ l for l in net.layers.keys() if l not in supported_layers ] if len(not_supported_layers) != 0: log.error( "Following layers are not supported by the plugin for specified device {}:\n {}" .format(plugin.device, ', '.join(not_supported_layers))) log.error( "Please try to specify cpu extensions library path in demo's command line parameters using -l " "or --cpu_extension command line argument") sys.exit(1) assert len( net.inputs.keys()) == 1, "Demo supports only single input topologies" assert len(net.outputs) == 1, "Demo supports only single output topologies" input_blob = next(iter(net.inputs)) out_blob = next(iter(net.outputs)) log.info("Loading IR to the plugin...") exec_net = plugin.load(network=net, num_requests=2) # Read and pre-process input image n, c, h, w = net.inputs[input_blob].shape del net if args.labels: with open(args.labels, 'r') as f: labels_map = [x.strip() for x in f] else: labels_map = None cur_request_id = 0 next_request_id = 1 log.info("Starting inference in async mode...") log.info("To switch between sync and async modes press Tab button") log.info("To stop the demo execution press Esc button") is_async_mode = False ################## # Initialize frame rate calculation frame_rate_calc = 1 freq = cv2.getTickFrequency() font = cv2.FONT_HERSHEY_SIMPLEX ########################################## objects_first = 0 t1_image = time.time() t2_image = time.time() ########################################## log.info("Detect On") while True: t1 = cv2.getTickCount() #ชื่อคลาสและไอดีของคลาส ckname = '' object_name = None ret, frame_q = vs.read() if is_async_mode: in_frame = cv2.resize(frame_q, (w, h)) in_frame = in_frame.transpose( (2, 0, 1)) # Change data layout from HWC to CHW in_frame = in_frame.reshape((n, c, h, w)) exec_net.start_async(request_id=next_request_id, inputs={input_blob: in_frame}) else: in_frame = cv2.resize(frame_q, (w, h)) in_frame = in_frame.transpose( (2, 0, 1)) # Change data layout from HWC to CHW in_frame = in_frame.reshape((n, c, h, w)) exec_net.start_async(request_id=cur_request_id, inputs={input_blob: in_frame}) rects = [] if exec_net.requests[cur_request_id].wait(-1) == 0: # Parse detection results of the current request res = exec_net.requests[cur_request_id].outputs[out_blob] for obj in res[0][0]: # Draw only objects when probability more than specified threshold if obj[2] > min_conf_threshold: # Draw label class_id = int(obj[1]) object_name = labels_map[class_id] if labels_map else str( class_id) if object_name in detect_list: ckname = ckname + object_name + ',' xmin = int(obj[3] * imW) ymin = int(obj[4] * imH) xmax = int(obj[5] * imW) ymax = int(obj[6] * imH) cv2.rectangle(frame_q, (xmin, ymin), (xmax, ymax), (10, 255, 0), 2) label = '%s: %s%%' % (object_name, int(round(obj[2] * 100, 1))) #print(label) labelSize, baseLine = cv2.getTextSize( label, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 2) label_ymin = max(ymin, labelSize[1] + 10) color = color_name(object_name) cv2.rectangle( frame_q, (xmin, label_ymin - labelSize[1] - 10), (xmin + labelSize[0], label_ymin + baseLine - 10), color, cv2.FILLED) cv2.putText(frame_q, label, (xmin, label_ymin - 7), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2) x = np.array([xmin, ymin, xmax, ymax]) # print(x) rects.append(x.astype("int")) cv2.putText(frame_q, "FPS: {0:.2f}".format(frame_rate_calc), (30, 50), font, 1, (255, 255, 0), 2, cv2.LINE_AA) #Funtion display_show t1_image = time.time() frame_q, objects_first, t2_image = display_show( rects, t1_image, t2_image, ckname, frame_q, objects_first, notify, ct) t2 = cv2.getTickCount() time1 = (t2 - t1) / freq frame_rate_calc = 1 / time1 if is_async_mode: cur_request_id, next_request_id = next_request_id, cur_request_id with lock: outputFrame = frame_q.copy() if _reset == 1: sys.exit() vs.stop()