Exemple #1
0
def main():
    log.basicConfig(format='[ %(levelname)s ] %(message)s',
                    level=log.INFO,
                    stream=sys.stdout)
    args = parse_args()

    # ---------------------------Step 1. Initialize inference engine core--------------------------------------------------
    log.info('Creating Inference Engine')
    ie = IECore()

    # ---------------------------Step 2. Read a model in OpenVINO Intermediate Representation------------------------------
    log.info(
        f'Loading the network using ngraph function with weights from {args.model}'
    )
    ngraph_function = create_ngraph_function(args)
    net = IENetwork(ngraph.impl.Function.to_capsule(ngraph_function))

    # ---------------------------Step 3. Configure input & output----------------------------------------------------------
    log.info('Configuring input and output blobs')
    # Get names of input and output blobs
    input_blob = next(iter(net.input_info))
    out_blob = next(iter(net.outputs))

    # Set input and output precision manually
    net.input_info[input_blob].precision = 'U8'
    net.outputs[out_blob].precision = 'FP32'

    # Set a batch size to a equal number of input images
    net.batch_size = len(args.input)

    # ---------------------------Step 4. Loading model to the device-------------------------------------------------------
    log.info('Loading the model to the plugin')
    exec_net = ie.load_network(network=net, device_name=args.device)

    # ---------------------------Step 5. Create infer request--------------------------------------------------------------
    # load_network() method of the IECore class with a specified number of requests (default 1) returns an ExecutableNetwork
    # instance which stores infer requests. So you already created Infer requests in the previous step.

    # ---------------------------Step 6. Prepare input---------------------------------------------------------------------
    n, c, h, w = net.input_info[input_blob].input_data.shape
    input_data = np.ndarray(shape=(n, c, h, w))

    for i in range(n):
        image = read_image(args.input[i])

        light_pixel_count = np.count_nonzero(image > 127)
        dark_pixel_count = np.count_nonzero(image < 127)
        is_light_image = (light_pixel_count - dark_pixel_count) > 0

        if is_light_image:
            log.warning(
                f'Image {args.input[i]} is inverted to white over black')
            image = cv2.bitwise_not(image)

        if image.shape != (h, w):
            log.warning(
                f'Image {args.input[i]} is resized from {image.shape} to {(h, w)}'
            )
            image = cv2.resize(image, (w, h))

        input_data[i] = image

    # ---------------------------Step 7. Do inference----------------------------------------------------------------------
    log.info('Starting inference in synchronous mode')
    res = exec_net.infer(inputs={input_blob: input_data})

    # ---------------------------Step 8. Process output--------------------------------------------------------------------
    # Generate a label list
    if args.labels:
        with open(args.labels, 'r') as f:
            labels = [line.split(',')[0].strip() for line in f]

    res = res[out_blob]

    for i in range(n):
        probs = res[i]
        # Get an array of args.number_top class IDs in descending order of probability
        top_n_idexes = np.argsort(probs)[-args.number_top:][::-1]

        header = 'classid probability'
        header = header + ' label' if args.labels else header

        log.info(f'Image path: {args.input[i]}')
        log.info(f'Top {args.number_top} results: ')
        log.info(header)
        log.info('-' * len(header))

        for class_id in top_n_idexes:
            probability_indent = ' ' * (len('classid') - len(str(class_id)) +
                                        1)
            label_indent = ' ' * (len('probability') -
                                  8) if args.labels else ''
            label = labels[class_id] if args.labels else ''
            log.info(
                f'{class_id}{probability_indent}{probs[class_id]:.7f}{label_indent}{label}'
            )
        log.info('')

    # ----------------------------------------------------------------------------------------------------------------------
    log.info(
        'This sample is an API example, for any performance measurements please use the dedicated benchmark_app tool\n'
    )
    return 0
if args.device == "CPU": data_type="FP32"

#STEP-2
model_xml=data_type+'/yolov2.xml'
model_bin=data_type+'/yolov2.bin'
ret, _bin, _xml, classes_mode = check_classes(args.classes)
if ret: model_xml, model_bin = _xml, _bin
if classes_mode == 80:class_names = coco_class_names
if classes_mode == 20:class_names = voc_class_names
if classes_mode ==  2:class_names = c2_class_names
if classes_mode ==  1:class_names = c1_class_names
if classes_mode == 80:anchors     = coco_anchors
if classes_mode == 20:anchors     = voc_anchors
if classes_mode ==  2:anchors     = c2_anchors
if classes_mode ==  1:anchors     = c1_anchors
net = IENetwork(model=model_xml, weights=model_bin)

classes=len(class_names)

print("num/coods/classes/downsampling",num,coords,classes,downsampling_rate)

thresh_conf=0.69 # if 0.69 then can detect motorbike but 0.60 then detect person instead of motorbike
thresh_conf=0.60 # But in YOLO-OpenVINO/YOLOv2/main.cpp thresh_conf is 0.5
thresh_conf=0.50
thresh_iou =0.45

#STEP-3
print(model_bin, "on", args.device)
plugin = IEPlugin(device=args.device, plugin_dirs=None)
if args.device == "CPU":
    HOME = os.environ['HOME']
Exemple #3
0
def main():
    total_timer = Timer(name='total')
    total_timer.tic()

    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    args = build_argparser().parse_args()
    # --------------------------- 1. Read IR Generated by ModelOptimizer (.xml and .bin files) ------------
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"
    log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    net = IENetwork(model=model_xml, weights=model_bin)
    # -----------------------------------------------------------------------------------------------------

    # ------------- 2. Load Plugin for inference engine and extensions library if specified --------------
    log.info("Loading Inference Engine")
    ie = IECore()
    log.info("Device info:")
    versions = ie.get_versions(args.device)
    print("{}{}".format(" " * 8, args.device))
    print("{}MKLDNNPlugin version ......... {}.{}".format(
        " " * 8, versions[args.device].major, versions[args.device].minor))
    print("{}Build ........... {}".format(" " * 8,
                                          versions[args.device].build_number))

    if args.cpu_extension and "CPU" in args.device:
        ie.add_extension(args.cpu_extension, "CPU")
        log.info("CPU extension loaded: {}".format(args.cpu_extension))

    if "CPU" in args.device:
        supported_layers = ie.query_network(net, "CPU")
        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(args.device, ', '.join(not_supported_layers)))
            log.error(
                "Please try to specify cpu extensions library path in sample's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)
    # -----------------------------------------------------------------------------------------------------

    # --------------------------- 4. Configure input & output ---------------------------------------------
    # --------------------------- Prepare input blobs -----------------------------------------------------
    log.info("Preparing input blobs")
    assert (len(net.inputs.keys()) == 1
            ), "Sample supports topologies only with 1 input"

    input_name = next(iter(net.inputs.keys()))
    input_info = net.inputs[input_name]
    input_info.precision = 'FP32'
    log.info('input shape: {}'.format(input_info.shape))

    # --------------------------- Prepare output blobs ----------------------------------------------------
    log.info('Preparing output blobs')
    assert (len(net.outputs.keys()) == 2
            ), "Sample supports topologies only with 2 output"

    loc_out_name = "797"
    class_out_name = "741"
    assert (loc_out_name in net.outputs.keys()) and (class_out_name
                                                     in net.outputs.keys())

    loc_out_info = net.outputs[loc_out_name]
    class_out_info = net.outputs[class_out_name]

    loc_out_info.precision = "FP32"
    class_out_info.precision = "FP32"
    # -----------------------------------------------------------------------------------------------------

    # -----------------------------------------------------------------------------------------------------
    log.info("Loading model to the device")
    # cpu_throughput = {'CPU_THROUGHPUT_STREAMS': 'CPU_THROUGHPUT_AUTO'}
    ie.set_config({'CPU_THROUGHPUT_STREAMS': 'CPU_THROUGHPUT_AUTO'},
                  args.device)
    ie.set_config({'CPU_BIND_THREAD': 'YES'}, args.device)
    exec_net = ie.load_network(network=net,
                               device_name=args.device,
                               num_requests=0)

    infer_requests = exec_net.requests
    request_queue = InferRequestsQueue(infer_requests)
    log.info('nreqs: {}, nstream:{}'.format(
        len(infer_requests),
        ie.get_config(args.device, 'CPU_THROUGHPUT_STREAMS')))

    # --------------------------- 3. Read and preprocess input --------------------------------------------
    # -----------------------------------------------------------------------------------------------------
    if not os.path.exists(args.result_dir):
        os.makedirs(args.result_dir)

    if args.voc_res_file and os.path.exists(args.voc_res_file):
        os.remove(args.voc_res_file)

    load_data_timer = Timer(name='load_data')
    post_process_timer = Timer(name='post_process')

    adapter = RetinaNetAdapter(input_shape=args.patch_size)

    # --------------------------- Performing inference ----------------------------------------------------
    result_all_images = defaultdict(list)
    data_loader = DataLoader(args.image_dir, args.strides, args.patch_size)
    while True:
        load_data_timer.tic()
        input_data = data_loader.next()
        load_data_timer.toc()

        if input_data == None:
            break

        infer_request = request_queue.get_idle_request()
        if not infer_request:
            raise Exception('No idle Infer Requests!')

        if infer_request.cur_meta == None:
            infer_request.start_async(input_name, input_data)
            continue

        # get result
        post_process_timer.tic()
        image_name = infer_request.cur_meta['image_name']
        x = infer_request.cur_meta['x']
        y = infer_request.cur_meta['y']

        loc_out = infer_request.request.outputs[loc_out_name][0]
        class_out = infer_request.request.outputs[class_out_name][0]

        ## start infer
        infer_request.start_async(input_name, input_data)

        ## post-process
        result = adapter.process(loc_out, class_out)
        result, _ = nms(result, thresh=0.5, keep_top_k=100)

        result[:, 0] += x
        result[:, 1] += y
        result[:, 2] += x
        result[:, 3] += y
        result_all_images[image_name].append(result)
        post_process_timer.toc()

    # wait the latest inference executions
    request_queue.wait_all()
    post_process_timer.tic()
    for infer_request in request_queue.requests:
        # get result
        image_name = infer_request.cur_meta['image_name']
        x = infer_request.cur_meta['x']
        y = infer_request.cur_meta['y']

        loc_out = infer_request.request.outputs[loc_out_name][0]
        class_out = infer_request.request.outputs[class_out_name][0]

        ## post-process
        result = adapter.process(loc_out, class_out)
        result, _ = nms(result, thresh=0.5, keep_top_k=100)

        result[:, 0] += x
        result[:, 1] += y
        result[:, 2] += x
        result[:, 3] += y
        result_all_images[image_name].append(result)
    post_process_timer.toc()

    post_process_timer.tic()
    ## process total image result
    for image_name, result_per_image in result_all_images.items():
        result_per_image = np.concatenate(result_per_image, axis=0)
        nms_result, _ = nms(result_per_image, thresh=0.5)

        voc_format = '{} {:.4f} {} {} {} {}'
        pos_all = []
        voc_all = []
        for i in range(nms_result.shape[0]):
            x = int(nms_result[i, 0])
            y = int(nms_result[i, 1])
            w = max(int(nms_result[i, 2] - nms_result[i, 0]), 1)
            h = max(int(nms_result[i, 3] - nms_result[i, 1]), 1)
            p = float(nms_result[i, 4])
            pos = {'x': x, 'y': y, 'w': w, 'h': h, 'p': p}
            pos_all.append(pos)

            if args.voc_res_file:
                xmin = x
                ymin = y
                xmax = int(nms_result[i, 2])
                ymax = int(nms_result[i, 3])
                voc_str = voc_format.format(
                    os.path.splitext(image_name)[0], p, xmin, ymin, xmax, ymax)
                voc_all.append(voc_str)

        file_name = os.path.splitext(image_name)[0] + '.json'
        with open(os.path.join(args.result_dir, file_name), 'w') as f:
            json.dump(pos_all, f)

        if args.voc_res_file:
            with open(args.voc_res_file, 'a') as f:
                for voc_str in voc_all:
                    f.write(voc_str + '\n')

    post_process_timer.toc()
    total_timer.toc()
    # -----------------------------------------------------------------------------------------------------
    all_timers = []
    # all_timers.extend([create_anchor_timer,
    #                    read_img_timer,
    #                    preprocess_timer,
    #                    infer_timer,
    #                    adapter_timer,
    #                    patch_img_nms_timer,
    #                    whole_img_nms_timer,
    #                    add_offset_timer,
    #                    write_result_timer,
    #                    total_timer])
    all_timers.extend([load_data_timer, post_process_timer, total_timer])
    for timer in all_timers:
        log.info('{}: avg: {:.2f} ms, total: {:.2f}s'.format(
            timer.name, timer.avg * 1000, timer.total))

    log.info('infer: {:2f}s'.format(request_queue.get_duration_in_seconds()))
    log.info("Execution successful\n")
def road_segementation_demo():
    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    # Plugin initialization for specified device and load extensions library if specified
    log.info("Initializing plugin for {} device...".format("CPU"))
    plugin = IEPlugin(device="CPU", plugin_dirs=plugin_dir)
    plugin.add_cpu_extension(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

    cap = cv2.VideoCapture(
        "H:/OpenVINO_Learning/007_road-segmentation/road_seg.mp4")

    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 = True
    render_time = 0
    ret, frame = cap.read()

    print(
        "To close the application, press 'CTRL+C' or any key with focus on the output window"
    )
    while cap.isOpened():
        if is_async_mode:
            ret, next_frame = cap.read()
        else:
            ret, frame = cap.read()
        if not ret:
            break
        initial_w = cap.get(3)
        initial_h = cap.get(4)

        # 开启同步或者异步执行模式
        inf_start = time.time()
        if is_async_mode:
            in_frame = cv2.resize(next_frame, (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, (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})
        if exec_net.requests[cur_request_id].wait(-1) == 0:
            # 获取网络输出
            res = exec_net.requests[cur_request_id].outputs[out_blob]

            # 解析道路分割结果
            res = np.squeeze(res, 0)
            res = res.transpose(1, 2, 0)  # HWC
            res = np.argmax(res, 2)

            hh, ww = res.shape
            mask = np.zeros((hh, ww, 3), dtype=np.uint8)
            mask[np.where(res > 0)] = (0, 255, 255)  # yellow
            mask[np.where(res > 1)] = (255, 0, 255)  #

            # 显示mask
            cv2.imshow("mask", mask)

            # 叠加输出结果
            mask = cv2.resize(mask, dsize=(frame.shape[1], frame.shape[0]))
            frame = cv2.addWeighted(mask, 0.2, frame, 0.8, 0)

            inf_end = time.time()
            det_time = inf_end - inf_start
            # Draw performance stats
            inf_time_message = "Inference time: {:.3f} ms, FPS:{:.3f}".format(
                det_time * 1000, 1000 / (det_time * 1000 + 1))
            render_time_message = "OpenCV rendering time: {:.3f} ms".format(
                render_time * 1000)
            async_mode_message = "Async mode is on. Processing request {}".format(cur_request_id) if is_async_mode else \
                "Async mode is off. Processing request {}".format(cur_request_id)

            cv2.putText(frame, inf_time_message, (15, 15),
                        cv2.FONT_HERSHEY_COMPLEX, 0.5, (200, 10, 10), 1)
            cv2.putText(frame, render_time_message, (15, 30),
                        cv2.FONT_HERSHEY_COMPLEX, 0.5, (10, 10, 200), 1)
            cv2.putText(frame, async_mode_message, (10, int(initial_h - 20)),
                        cv2.FONT_HERSHEY_COMPLEX, 0.5, (10, 10, 200), 1)

        #
        render_start = time.time()
        cv2.imshow("road segmentation demo", frame)
        render_end = time.time()
        render_time = render_end - render_start

        if is_async_mode:
            cur_request_id, next_request_id = next_request_id, cur_request_id
            frame = next_frame

        key = cv2.waitKey(1)
        if key == 27:
            break

    cv2.destroyAllWindows()

    del exec_net
    del plugin
def main():
    log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout)
    args = build_argparser().parse_args()
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    preprocess_times = collections.deque()
    infer_times = collections.deque()
    postprocess_times = collections.deque()
    
    ROIfile=open("ROIs.txt","w"); # output stored here, view with ROIviewer
    
    # Plugin initialization for specified device and load extensions library if specified
    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)

    #Set Batch Size
    net.batch_size = args.b
    batchSize = net.batch_size
    frameLimit = args.fr
    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)
    infer_file = os.path.join(args.output_dir, 'i_progress.txt')
       
    # Read and pre-process input image
    n, c, h, w = net.inputs[input_blob].shape
    output_dims=net.outputs[out_blob].shape
    infer_width=w;
    infer_height=h;
    num_channels=c;
    channel_size=infer_width*infer_height
    full_image_size=channel_size*num_channels
    
    print("inputdims=",w,h,c,n)
    print("outputdims=",output_dims[3],output_dims[2],output_dims[1],output_dims[0])
    if int(output_dims[3])>1 :
        print("SSD Mode")
        output_mode=output_mode_type.SSD_MODE
    else:
        print("Single Classification Mode")
        output_mode=CLASSIFICATION_MODE
        output_data_size=int(output_dims[2])*int(output_dims[1])*int(output_dims[0])
    del net
    if args.input == 'cam':
        input_stream = 0
    else:
        input_stream = args.input
        assert os.path.isfile(args.input), "Specified input file doesn't exist"
    if args.labels:
        with open(args.labels, 'r') as f:
            labels_map = [x.strip() for x in f]
    else:
        labels_map = None

    cap = cv2.VideoCapture(input_stream)
    cur_request_id = 0
    next_request_id = 1

    is_async_mode =True
    if (is_async_mode == True):
        log.info("Starting inference in async mode...")   
    else :
            log.info("Starting inference in sync mode...")

    render_time = 0

    framenum = 0
    process_more_frames=True
    frames_in_output=batchSize
    
    while process_more_frames:
        time1 = time.time()
        for mb in range(0 , batchSize):
            ret, frame = cap.read()
            if not ret or (framenum >= frameLimit):
                process_more_frames=False
                frames_in_output=mb
                
            if (not process_more_frames):
                break

            # convert image to blob
            # Fill input tensor with planes. First b channel, then g and r channels
            in_frame = cv2.resize(frame, (w, h))
            in_frame = in_frame.transpose((2, 0, 1))  # Change data layout from HWC to CHW
            
			
        time2 = time.time()
        diffPreProcess = time2 - time1
        if process_more_frames:
            preprocess_times.append(diffPreProcess*1000)
            
        # Main sync point:
        # in the truly Async mode we start the NEXT infer request, while waiting for the CURRENT to complete
        # in the regular mode we start the CURRENT request and immediately wait for it's completion
            inf_start = time.time()
            if is_async_mode:
                exec_net.start_async(request_id=next_request_id, inputs={input_blob: in_frame})
            else:
                exec_net.start_async(request_id=cur_request_id, inputs={input_blob: in_frame})
            if exec_net.requests[cur_request_id].wait(-1) == 0:
                inf_end = time.time()
                
                det_time = inf_end - inf_start
                infer_times.append(det_time*1000)
                progressUpdate(infer_file, (sum(infer_times)/1000), framenum+1, 256)
                time1 = time.time()
                
                for mb in range(0 , batchSize):
                    if (framenum >= frameLimit):
                        process_more_frames=False;
                        break;

            # Parse detection results of the current request
                    res = exec_net.requests[cur_request_id].outputs[out_blob]
                    for obj in res[0][0]:
                # Write into ROIs.txt only objects when probability more than specified threshold
            	        if obj[2] > args.prob_threshold:
                            confidence=obj[2]
                            locallabel = obj[1] - 1
                            print(str(0),str(framenum),str(locallabel),str(confidence),str(obj[3]),str(obj[4]),str(obj[5]),str(obj[6]), file=ROIfile)

        
                    sys.stdout.write("\rframenum:"+str(framenum + 1))
                    sys.stdout.flush()
                    render_start = time.time()
                    framenum = framenum+1  
                time2 = time.time()
                diffPostProcess = time2 - time1
                postprocess_times.append(diffPostProcess*1000)

            if is_async_mode:
                cur_request_id, next_request_id = next_request_id, cur_request_id

            
    print("\n")
    preprocesstime=0
    inferencetime=0
    postprocesstime=0
    
    for obj in preprocess_times:
         preprocesstime+=obj
    for obj in infer_times:
        inferencetime+=obj
    for obj in postprocess_times:
        postprocesstime+=obj

        
    print("Preprocess: {:.2f} ms/frame".format(preprocesstime/(len(preprocess_times)*batchSize)))
    print("Inference: {:.2f} ms/frame ".format(inferencetime/(len(infer_times)*batchSize)))
    print("Postprocess: {:.2f} ms/frame".format(postprocesstime/(len(postprocess_times)*batchSize)))
    
    
    with open(os.path.join(args.output_dir, 'stats.txt'), 'w') as f:
        f.write('{:.3g} \n'.format(inferencetime/(batchSize*1000)))
        f.write('{} \n'.format(framenum))

    del exec_net
    del plugin
def main():
    args = build_argparser().parse_args()

    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    # ------------- 1. Plugin initialization for specified device and load extensions library if specified -------------
    log.info("Creating Inference Engine...")
    ie = IECore()
    if args.cpu_extension and 'CPU' in args.device:
        ie.add_extension(args.cpu_extension, "CPU")

    # -------------------- 2. Reading the IR generated by the Model Optimizer (.xml and .bin files) --------------------
    log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    net = IENetwork(model=model_xml, weights=model_bin)

    # ---------------------------------- 3. Load CPU extension for support specific layer ------------------------------
    if "CPU" in args.device:
        supported_layers = ie.query_network(net, "CPU")
        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(args.device, ', '.join(not_supported_layers)))
            log.error(
                "Please try to specify cpu extensions library path in sample's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)

    assert len(net.inputs.keys(
    )) == 1, "Sample supports only YOLO V3 based single input topologies"

    # ---------------------------------------------- 4. Preparing inputs -----------------------------------------------
    log.info("Preparing inputs")
    input_blob = next(iter(net.inputs))

    #  Defaulf batch_size is 1
    net.batch_size = 1

    # Read and pre-process input images
    n, c, h, w = net.inputs[input_blob].shape

    if args.labels:
        with open(args.labels, 'r') as f:
            labels_map = [x.strip() for x in f]
    else:
        labels_map = None

    input_stream = 0 if args.input == "cam" else args.input

    is_async_mode = True
    cap = cv2.VideoCapture(input_stream)
    number_input_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    number_input_frames = 1 if number_input_frames != -1 and number_input_frames < 0 else number_input_frames

    wait_key_code = 1

    # Number of frames in picture is 1 and this will be read in cycle. Sync mode is default value for this case
    if number_input_frames != 1:
        ret, frame = cap.read()
    else:
        is_async_mode = False
        wait_key_code = 0

    # ----------------------------------------- 5. Loading model to the plugin -----------------------------------------
    log.info("Loading model to the plugin")
    exec_net = ie.load_network(network=net,
                               num_requests=2,
                               device_name=args.device)

    cur_request_id = 0
    next_request_id = 1
    render_time = 0
    parsing_time = 0

    # ----------------------------------------------- 6. Doing inference -----------------------------------------------
    log.info("Starting inference...")
    print(
        "To close the application, press 'CTRL+C' here or switch to the output window and press ESC key"
    )
    print(
        "To switch between sync/async modes, press TAB key in the output window"
    )
    while cap.isOpened():
        # Here is the first asynchronous point: in the Async mode, we capture frame to populate the NEXT infer request
        # in the regular mode, we capture frame to the CURRENT infer request
        if is_async_mode:
            ret, next_frame = cap.read()
        else:
            ret, frame = cap.read()

        if not ret:
            break

        if is_async_mode:
            request_id = next_request_id
            in_frame = cv2.resize(next_frame, (w, h))
        else:
            request_id = cur_request_id
            in_frame = cv2.resize(frame, (w, h))

        # resize input_frame to network size
        in_frame = in_frame.transpose(
            (2, 0, 1))  # Change data layout from HWC to CHW
        in_frame = in_frame.reshape((n, c, h, w))

        # Start inference
        start_time = time()
        exec_net.start_async(request_id=request_id,
                             inputs={input_blob: in_frame})
        det_time = time() - start_time

        # Collecting object detection results
        objects = list()
        if exec_net.requests[cur_request_id].wait(-1) == 0:
            output = exec_net.requests[cur_request_id].outputs

            start_time = time()
            for layer_name, out_blob in output.items():
                out_blob = out_blob.reshape(
                    net.layers[net.layers[layer_name].parents[0]].shape)
                layer_params = YoloParams(net.layers[layer_name].params,
                                          out_blob.shape[2])
                log.info("Layer {} parameters: ".format(layer_name))
                layer_params.log_params()
                objects += parse_yolo_region(out_blob, in_frame.shape[2:],
                                             frame.shape[:-1], layer_params,
                                             args.prob_threshold)
            parsing_time = time() - start_time

        # Filtering overlapping boxes with respect to the --iou_threshold CLI parameter
        objects = sorted(objects,
                         key=lambda obj: obj['confidence'],
                         reverse=True)
        for i in range(len(objects)):
            if objects[i]['confidence'] == 0:
                continue
            for j in range(i + 1, len(objects)):
                if intersection_over_union(objects[i],
                                           objects[j]) > args.iou_threshold:
                    objects[j]['confidence'] = 0

        # Drawing objects with respect to the --prob_threshold CLI parameter
        objects = [
            obj for obj in objects if obj['confidence'] >= args.prob_threshold
        ]

        if len(objects) and args.raw_output_message:
            log.info("\nDetected boxes for batch {}:".format(1))
            log.info(
                " Class ID | Confidence | XMIN | YMIN | XMAX | YMAX | COLOR ")

        origin_im_size = frame.shape[:-1]
        for obj in objects:
            # Validation bbox of detected object
            if obj['xmax'] > origin_im_size[1] or obj['ymax'] > origin_im_size[
                    0] or obj['xmin'] < 0 or obj['ymin'] < 0:
                continue
            color = (int(min(obj['class_id'] * 12.5,
                             255)), min(obj['class_id'] * 7,
                                        255), min(obj['class_id'] * 5, 255))
            det_label = labels_map[obj['class_id']] if labels_map and len(labels_map) >= obj['class_id'] else \
                str(obj['class_id'])

            if args.raw_output_message:
                log.info(
                    "{:^9} | {:10f} | {:4} | {:4} | {:4} | {:4} | {} ".format(
                        det_label, obj['confidence'], obj['xmin'], obj['ymin'],
                        obj['xmax'], obj['ymax'], color))

            cv2.rectangle(frame, (obj['xmin'], obj['ymin']),
                          (obj['xmax'], obj['ymax']), color, 2)
            cv2.putText(
                frame, "#" + det_label + ' ' +
                str(round(obj['confidence'] * 100, 1)) + ' %',
                (obj['xmin'], obj['ymin'] - 7), cv2.FONT_HERSHEY_COMPLEX, 0.6,
                color, 1)

        # Draw performance stats over frame
        inf_time_message = "Inference time: N\A for async mode" if is_async_mode else \
            "Inference time: {:.3f} ms".format(det_time * 1e3)
        render_time_message = "OpenCV rendering time: {:.3f} ms".format(
            render_time * 1e3)
        async_mode_message = "Async mode is on. Processing request {}".format(cur_request_id) if is_async_mode else \
            "Async mode is off. Processing request {}".format(cur_request_id)
        parsing_message = "YOLO parsing time is {:.3f}".format(parsing_time *
                                                               1e3)

        cv2.putText(frame, inf_time_message, (15, 15),
                    cv2.FONT_HERSHEY_COMPLEX, 0.5, (200, 10, 10), 1)
        cv2.putText(frame, render_time_message, (15, 45),
                    cv2.FONT_HERSHEY_COMPLEX, 0.5, (10, 10, 200), 1)
        cv2.putText(frame, async_mode_message,
                    (10, int(origin_im_size[0] - 20)),
                    cv2.FONT_HERSHEY_COMPLEX, 0.5, (10, 10, 200), 1)
        cv2.putText(frame, parsing_message, (15, 30), cv2.FONT_HERSHEY_COMPLEX,
                    0.5, (10, 10, 200), 1)

        start_time = time()
        cv2.imshow("DetectionResults", frame)
        render_time = time() - start_time

        if is_async_mode:
            cur_request_id, next_request_id = next_request_id, cur_request_id
            frame = next_frame

        key = cv2.waitKey(wait_key_code)

        # ESC key
        if key == 27:
            break
        # Tab key
        if key == 9:
            exec_net.requests[cur_request_id].wait()
            is_async_mode = not is_async_mode
            log.info("Switched to {} mode".format(
                "async" if is_async_mode else "sync"))

    cv2.destroyAllWindows()
Exemple #7
0
    def load_model(self,
                   model,
                   device,
                   input_size,
                   output_size,
                   num_requests,
                   cpu_extension=None,
                   plugin=None):
        """
         Loads a network and an image to the Inference Engine plugin.
        :param model: .xml file of pre trained model
        :param cpu_extension: extension for the CPU device
        :param device: Target device
        :param input_size: Number of input layers
        :param output_size: Number of output layers
        :param num_requests: Index of Infer request value. Limited to device capabilities.
        :param plugin: Plugin for specified device
        :return:  Shape of input layer
        """

        model_xml = model
        model_bin = os.path.splitext(model_xml)[0] + ".bin"
        # Plugin initialization for specified device
        # and load extensions library if specified
        if not plugin:
            log.info("Initializing plugin for {} device...".format(device))
            self.plugin = IECore()
        else:
            self.plugin = plugin

        if cpu_extension and 'CPU' in device:
            self.plugin.add_extension(cpu_extension, "CPU")

        # Read IR
        log.info("Reading IR...")
        self.net = IENetwork(model=model_xml, weights=model_bin)
        # print("Network Layers : ")
        # print(self.net.layers)
        log.info("Loading IR to the plugin...")

        if device == "CPU":
            supported_layers = self.plugin.query_network(self.net, "CPU")
            not_supported_layers = \
                [l for l in self.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(
                              device, ', '.join(not_supported_layers)))
                log.error("Please try to specify cpu extensions library path"
                          " in command line parameters using -l "
                          "or --cpu_extension command line argument")
                sys.exit(1)

        if num_requests == 0:
            # Loads network read from IR to theplugin
            self.net_plugin = self.plugin.load_network(network=self.net,
                                                       device_name=device)
        else:
            self.net_plugin = self.plugin.load_network(
                network=self.net,
                num_requests=num_requests,
                device_name=device)
            # log.error("num_requests != 0")

        self.input_blob = next(iter(self.net.inputs))
        self.out_blob = next(iter(self.net.outputs))
        # assert len(self.net.inputs.keys()) == input_size, \
        #     "Supports only {} input topologies".format(len(self.net.inputs))
        # assert len(self.net.outputs) == output_size, \
        #     "Supports only {} output topologies".format(len(self.net.outputs))

        return self.plugin, self.get_input_shape()
Exemple #8
0
def main():

    job_id = os.environ['PBS_JOBID']
    job_id = job_id.rstrip().split('.')[0]
    codec = data_utils.TextFeatureIO(char_dict_path='Config/char_dict.json',
                                     ord_map_dict_path=r'Config/ord_map.json')
    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    args = build_argparser().parse_args()
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    # Plugin initialization for specified device and load extensions library if specified
    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("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    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 sample's command line parameters using -l "
    # "or --cpu_extension command line argument")
    # sys.exit(1)

    assert len(
        net.inputs.keys()) == 1, "Sample supports only single input topologies"
    assert len(
        net.outputs) == 1, "Sample supports only single output topologies"

    log.info("Preparing input blobs")
    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
    net.batch_size = len(args.input)

    # Read and pre-process input images
    n, c, h, w = net.inputs[input_blob].shape
    images = np.ndarray(shape=(n, c, h, w))
    for i in range(n):
        image = cv2.imread(args.input[i])
        if image.shape[:-1] != (h, w):
            log.warning("Image {} is resized from {} to {}".format(
                args.input[i], image.shape[:-1], (h, w)))
            image = cv2.resize(image, (w, h))
        image = image.transpose(
            (2, 0, 1))  # Change data layout from HWC to CHW
        images[i] = image
    log.info("Batch size is {}".format(n))

    # Loading model to the plugin
    log.info("Loading model to the plugin")
    exec_net = plugin.load(network=net)
    del net

    # Start sync inference
    log.info("Starting inference ({} iterations)".format(args.number_iter))
    infer_time = []

    result_dir = os.path.join(args.output_dir, job_id)

    if not os.path.isdir(result_dir):
        print(result_dir)
        os.makedirs(result_dir, exist_ok=True)
    infer_file = os.path.join(result_dir, 'i_progress.txt')
    t0 = time.time()
    for i in range(args.number_iter):
        res = exec_net.infer(inputs={input_blob: images})
        if i % 10 == 0 or i == args.number_iter - 1:
            progressUpdate(infer_file,
                           time.time() - t0, i + 1, args.number_iter)
    t1 = (time.time() - t0)
    log.info("Average running time of one iteration: {} ms".format(
        np.average(np.asarray(infer_time))))
    if args.perf_counts:
        perf_counts = exec_net.requests[0].get_perf_counts()
        log.info("Performance counters:")
        print("{:<70} {:<15} {:<15} {:<15} {:<10}".format(
            'name', 'layer_type', 'exet_type', 'status', 'real_time, us'))
        for layer, stats in perf_counts.items():
            print("{:<70} {:<15} {:<15} {:<15} {:<10}".format(
                layer, stats['layer_type'], stats['exec_type'],
                stats['status'], stats['real_time']))

    # Processing output blob
    log.info("Processing output blob")
    res = res[out_blob]

    preds = res.argmax(2)
    preds = preds.transpose(1, 0)
    preds = np.ascontiguousarray(preds, dtype=np.int8).view(dtype=np.int8)
    values = codec.writer.ordtochar(preds[0].tolist())
    values = [v for i, v in enumerate(values) if i == 0 or v != values[i - 1]]
    values = [x for x in values if x != ' ']
    res = ''.join(values)
    print("The result is : " + res)

    avg_time = round((t1 * 1000 / args.number_iter), 3)
    with open(os.path.join(args.output_dir, job_id, 'result.txt'), 'w') as f:
        f.write(res + "\n Inference performed in " + str(avg_time) + "ms")

    stats = {}
    stats['time'] = str(round(t1, 2))
    stats['frames'] = str(args.number_iter * n)
    stats['fps'] = str(round(args.number_iter * n / t1, 2))
    stats_file = result_dir + "/stats.json"
    with open(stats_file, 'w') as f:
        json.dump(stats, f)

    del exec_net
    del plugin
def main():
    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    args = build_argparser().parse_args()
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"
    # Plugin initialization for specified device and load extensions library if specified
    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
    print(n, c, h, w)
    del net
    if args.input == 'cam':
        input_stream = 0
    else:
        input_stream = args.input
        assert os.path.isfile(args.input), "Specified input file doesn't exist"
    if args.labels:
        with open(args.labels, 'r') as f:
            labels_map = [x.strip() for x in f]
    else:
        labels_map = None

    cap = cv2.VideoCapture(input_stream)
    fps = cap.get(cv2.CAP_PROP_FPS)
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    videWriter = cv2.VideoWriter("label.avi",
                                 cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'),
                                 fps, (width, height))

    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 = True
    render_time = 0
    ret, frame = cap.read()
    max_cost_time = 0
    total_cost_time = 0
    cnt = 0
    while cap.isOpened():
        if is_async_mode:
            ret, next_frame = cap.read()
        else:
            ret, frame = cap.read()
        if not ret:
            break
        initial_w = cap.get(3)
        initial_h = cap.get(4)
        # Main sync point:
        # in the truly Async mode we start the NEXT infer request, while waiting for the CURRENT to complete
        # in the regular mode we start the CURRENT request and immediately wait for it's completion
        inf_start = time.time()
        if is_async_mode:
            in_frame = cv2.resize(next_frame, (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, (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})
        if exec_net.requests[cur_request_id].wait(-1) == 0:
            inf_end = time.time()
            det_time = inf_end - inf_start
            max_cost_time = det_time if det_time > max_cost_time else max_cost_time
            total_cost_time += det_time
            cnt += 1

            # 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] > args.prob_threshold:
                    xmin = int(obj[3] * initial_w)
                    ymin = int(obj[4] * initial_h)
                    xmax = int(obj[5] * initial_w)
                    ymax = int(obj[6] * initial_h)
                    class_id = int(obj[1])
                    # Draw box and label\class_id
                    color = (min(class_id * 12.5,
                                 255), min(class_id * 7,
                                           255), min(class_id * 5, 255))
                    cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), color, 2)
                    det_label = labels_map[class_id] if labels_map else str(
                        class_id)
                    cv2.putText(
                        frame,
                        det_label + ' ' + str(round(obj[2] * 100, 1)) + ' %',
                        (xmin, ymin - 7), cv2.FONT_HERSHEY_COMPLEX, 0.6, color,
                        1)

            # Draw performance stats
            inf_time_message = "Inference time: N\A for async mode" if is_async_mode else \
                "Inference time: {:.3f} ms".format(det_time * 1000)
            render_time_message = "OpenCV rendering time: {:.3f} ms".format(
                render_time * 1000)
            async_mode_message = "Async mode is on. Processing request {}".format(cur_request_id) if is_async_mode else \
                "Async mode is off. Processing request {}".format(cur_request_id)

            cv2.putText(frame, inf_time_message, (15, 15),
                        cv2.FONT_HERSHEY_COMPLEX, 0.5, (200, 10, 10), 1)
            cv2.putText(frame, render_time_message, (15, 30),
                        cv2.FONT_HERSHEY_COMPLEX, 0.5, (10, 10, 200), 1)
            cv2.putText(frame, async_mode_message, (10, int(initial_h - 20)),
                        cv2.FONT_HERSHEY_COMPLEX, 0.5, (10, 10, 200), 1)

        #
        render_start = time.time()
        cv2.imshow("Detection Results", frame)
        videWriter.write(frame)
        render_end = time.time()
        render_time = render_end - render_start

        if is_async_mode:
            cur_request_id, next_request_id = next_request_id, cur_request_id
            frame = next_frame

        key = cv2.waitKey(1)
        if key == 27:
            break
        if (9 == key):
            is_async_mode = not is_async_mode
            log.info("Switched to {} mode".format(
                "async" if is_async_mode else "sync"))

    print("Inference Max Cost {:.3f}ms".format(max_cost_time * 1000))
    print("Total Cost {:.3f}ms, mean cost {:.3f}ms, frame cnt {}".format(
        total_cost_time * 1000, total_cost_time / cnt * 1000, cnt))
    cv2.destroyAllWindows()
    del exec_net
    del plugin
Exemple #10
0
 def check_model(self, model_structure, model_weights):
     try:
         self.network = IENetwork(model_structure, model_weights)
     except Exception as e:
         raise ValueError("Could not Initialize Network")
def main():
    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    args = build_argparser().parse_args()
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"
    # Plugin initialization for specified device and load extensions library if specified
    log.info("Initializing plugin for %s device...", 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 "CPU" in plugin.device:
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [
            l for l in net.layers.keys() if l not in supported_layers
        ]
        if not_supported_layers:
            log.error(
                "Following layers are not supported by the plugin for specified device %s:\n %s",
                plugin.device, ', '.join(not_supported_layers))
            log.error(
                "Please try to specify cpu extensions library path in sample's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)
    assert len(
        net.inputs.keys()) == 1, "Sample supports only single input topologies"
    assert len(
        net.outputs) == 1, "Sample 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
    batch, channels, height, width = net.inputs[input_blob].shape
    del net

    predictions = []
    data = Input(args.input_type, args.input)
    cur_request_id = 0

    fps = 25
    out_width = 640
    out_height = 480
    if args.dump_output_video:
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter(args.path_to_output_video, fourcc, fps,
                              (int(out_width), int(out_height)))

    while not data.is_finished():
        frame, img_id = data.get_next_item()
        initial_h, initial_w, _ = frame.shape
        in_frame = cv2.resize(frame, (width, height))
        in_frame = in_frame.transpose(
            (2, 0, 1))  # Change data layout from HWC to CHW
        in_frame = in_frame.reshape((batch, channels, height, width))

        exec_net.start_async(request_id=cur_request_id,
                             inputs={input_blob: in_frame})
        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]
            coco_detections = []
            for obj in res[0][0]:
                # Draw only objects when probability more than specified threshold
                if obj[2] > args.prob_threshold:
                    top_left_x = float(obj[3] * initial_w)
                    top_left_y = float(obj[4] * initial_h)
                    bottom_right_x = float(obj[5] * initial_w)
                    bottom_right_y = float(obj[6] * initial_h)

                    obj_width = round(bottom_right_x - top_left_x, 1)
                    obj_height = round(bottom_right_y - top_left_y, 1)
                    class_id = int(obj[1])

                    coco_det = {}
                    coco_det['image_id'] = img_id
                    coco_det['category_id'] = class_id
                    coco_det['bbox'] = [
                        round(top_left_x, 1),
                        round(top_left_y, 1), obj_width, obj_height
                    ]
                    coco_det['score'] = round(float(obj[2]), 1)
                    coco_detections.append(coco_det)

                    # Draw box and label\class_id
                    cv2.rectangle(frame, (int(top_left_x), int(top_left_y)),
                                  (int(bottom_right_x), int(bottom_right_y)),
                                  (255, 0, 0), 2)
                    cv2.putText(
                        frame,
                        str(class_id) + ' ' + str(round(obj[2] * 100, 1)) +
                        ' %', (int(top_left_x), int(top_left_y) - 7),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
            predictions.extend(coco_detections)

        if args.dump_output_video:
            img_resized = cv2.resize(frame, (out_width, out_height))
            out.write(img_resized)

        if args.show:
            cv2.imshow("Detection Results", frame)
            key = cv2.waitKey(args.delay)
            if key == 27:
                break

    if args.dump_predictions_to_json:
        with open(args.output_json_path, 'w') as output_file:
            json.dump(predictions, output_file, sort_keys=True, indent=4)

    cv2.destroyAllWindows()
    del exec_net
    del plugin
def run_app():
    human_count = 0
    car_count = 0
    frame_count = 0

    print("App INITIALIZED")
    print("---------")

    # Load Network
    OpenVinoNetwork = IENetwork(model=arguments.model_xml,
                                weights=arguments.model_bin)

    print("XML and BIN files loded")
    # Get Input Layer Information
    InputLayer = next(iter(OpenVinoNetwork.inputs))
    print("Input Layer: ", InputLayer)

    # Get Output Layer Information
    OutputLayer = next(iter(OpenVinoNetwork.outputs))
    print("Output Layer: ", OutputLayer)

    # Get Input Shape of Model
    InputShape = OpenVinoNetwork.inputs[InputLayer].shape
    print("Input Shape: ", InputShape)

    # Get Output Shape of Model
    OutputShape = OpenVinoNetwork.outputs[OutputLayer].shape
    print("Output Shape: ", OutputShape)

    # Load IECore Object
    OpenVinoIE = IECore()
    print("Available Devices: ", OpenVinoIE.available_devices)

    # Create Executable Network
    if arguments. async:
        print("Async Mode Enabled")
        OpenVinoExecutable = OpenVinoIE.load_network(
            network=OpenVinoNetwork,
            device_name=arguments.target_device,
            num_requests=number_of_async_req)
    else:
        OpenVinoExecutable = OpenVinoIE.load_network(
            network=OpenVinoNetwork, device_name=arguments.target_device)

    # Generate a Named Window to Show Output
    cv.namedWindow('Window', cv.WINDOW_NORMAL)
    cv.resizeWindow('Window', 800, 600)

    start_time = time.time()

    if arguments.input_type == 'image':
        frame_count += 1
        # Read Image
        image = cv.imread(arguments.input)

        # Get Shape Values

        N, C, H, W = OpenVinoNetwork.inputs[InputLayer].shape

        # Pre-process Image
        resized = cv.resize(image, (W, H))
        # Change data layout from HWC to CHW
        resized = resized.transpose((2, 0, 1))
        input_image = resized.reshape((N, C, H, W))

        # Start Inference
        start = time.time()
        results = OpenVinoExecutable.infer(inputs={InputLayer: input_image})
        end = time.time()
        inf_time = end - start
        print('Inference Time: {} Seconds Single Image'.format(inf_time))

        fps = 1. / (end - start)
        print('Estimated FPS: {} FPS Single Image'.format(fps))

        fh = image.shape[0]
        fw = image.shape[1]

        # Write Information on Image
        text = 'FPS: {}, INF: {}'.format(round(fps, 2), round(inf_time, 2))
        cv.putText(image, text, (0, 20), cv.FONT_HERSHEY_COMPLEX, 0.6,
                   (0, 125, 255), 1)

        # Print Bounding Boxes on Image
        detections = results[OutputLayer][0][0]

        print(detections)
        # refined_detections = []
        human_count = 0
        car_count = 0
        for detection in detections:

            print("*********************************************")
            print(detection)
            print("*********************************************")

            if (detection[2] > arguments.detection_threshold):
                # refined_detections.append(detection)
                print(detection)
                for detr in detection:
                    print(detr)
                print('Original Frame Shape: ', fw, fh)
                xmin = int(detection[3] * fw)
                ymin = int(detection[4] * fh)
                xmax = int(detection[5] * fw)
                ymax = int(detection[6] * fh)

                if (detection[1] == 1):
                    car_count += 1
                    cv.rectangle(frame, (xmin, ymin), (xmax, ymax),
                                 (0, 255, 0), 3)
                if (detection[1] == 2):
                    human_count += 1
                    cv.rectangle(frame, (xmin, ymin), (xmax, ymax),
                                 (0, 0, 255), 3)
                print("Number Of Humans present : ", human_count)
                print("Number Of Cars present : ", car_count)
            else:
                break
                # if detection[2] > arguments.detection_threshold:
                #     print('Original Frame Shape: ', fw, fh)
                #     xmin = int(detection[3] * fw)
                #     ymin = int(detection[4] * fh)
                #     xmax = int(detection[5] * fw)
                #     ymax = int(detection[6] * fh)
                #     cv.rectangle(image, (xmin, ymin),
                #                  (xmax, ymax), (0, 125, 255), 3)
                #     text = '{}, %: {}'.format(
                #         mobilenet_ssd_labels[int(detection[1])], round(detection[2], 2))
                #     cv.putText(image, text, (xmin, ymin - 7),
                #                cv.FONT_HERSHEY_PLAIN, 0.8, (0, 125, 255), 1)

        cv.imshow('Window', image)
        cv.waitKey(0)

    else:
        print("Running Inference for {} - {}".format(arguments.input_type,
                                                     arguments.input))

        process_id = os.getpid()
        process = psutil.Process(process_id)

        total_inference_time = 0.0
        # Implementation for CAM or Video File
        # Read Image
        capture = cv.VideoCapture(arguments.input)
        has_frame, frame = capture.read()
        frame_count += 1

        if not has_frame:
            print("Can't Open Input Video Source {}".format(arguments.input))
            exit(-1)

        # Get Shape Values
        N, C, H, W = OpenVinoNetwork.inputs[InputLayer].shape
        fh = frame.shape[0]
        fw = frame.shape[1]
        print('Original Frame Shape: ', fw, fh)

        request_order = list()
        process_order = list()
        frame_order = list()
        if arguments. async:
            print("Async Mode Set")
            for i in range(number_of_async_req):
                request_order.append(i)
                print('Request Id {} Created'.format(i))

            print('Request Ids {}'.format(request_order))

        while has_frame:
            if arguments. async:
                if len(request_order) > 0:
                    resized = cv.resize(frame, (W, H))
                    # Change data layout from HWC to CHW
                    resized = resized.transpose((2, 0, 1))
                    input_data = resized.reshape((N, C, H, W))
                    req_id = request_order[0]
                    request_order.pop(0)
                    OpenVinoExecutable.start_async(
                        req_id, inputs={InputLayer: input_data})
                    process_order.append(req_id)
                    frame_order.append(frame)

                if len(process_order) > 0:
                    first = process_order[0]
                    if OpenVinoExecutable.requests[first].wait(0) == 0:
                        results = OpenVinoExecutable.requests[first].outputs[
                            OutputLayer]
                        process_order.pop(0)
                        request_order.append(first)
                        show_frame = frame_order[0]
                        frame_order.pop(0)

                        detections = results[0][0]
                        human_count = 0
                        car_count = 0
                        for detection in detections:

                            if detection[2] > arguments.detection_threshold:

                                xmin = int(detection[3] * fw)
                                ymin = int(detection[4] * fh)
                                xmax = int(detection[5] * fw)
                                ymax = int(detection[6] * fh)
                                if (detection[1] == 1):
                                    car_count += 1
                                    cv.rectangle(frame, (xmin, ymin),
                                                 (xmax, ymax), (0, 255, 0), 3)
                                    text = '{}, %: {}'.format(
                                        "CAR", round(detection[2], 3))
                                    cv.putText(show_frame, text,
                                               (xmin, ymin - 7),
                                               cv.FONT_HERSHEY_PLAIN, 0.8,
                                               (0, 255, 0), 1)
                                if (detection[1] == 2):
                                    human_count += 1
                                    cv.rectangle(frame, (xmin, ymin),
                                                 (xmax, ymax), (0, 0, 255), 3)
                                    text = '{}, %: {}'.format(
                                        "HUMAN", round(detection[2], 3))
                                    cv.putText(show_frame, text,
                                               (xmin, ymin - 7),
                                               cv.FONT_HERSHEY_PLAIN, 0.8,
                                               (0, 0, 255), 1)

                        print("Number Of Humans present : ", human_count)
                        print("Number Of Cars present : ", car_count)
                        fps = frame_count / (time.time() - start_time)
                        # Write Information on Image
                        text = 'FPS: {}, INF: {} ms'.format(round(fps, 3), "-")
                        cv.putText(show_frame, text, (0, 20),
                                   cv.FONT_HERSHEY_COMPLEX, 0.8, (0, 125, 255),
                                   1)

                        text = "SYS CPU% {} SYS MEM% {} \n " \
                               "PROC CPU Affinity {} \n " \
                               "NUM Threads {} \n " \
                               "PROC CPU% {} \n " \
                               "PROC MEM% {}".format(psutil.cpu_percent(),
                                                     psutil.virtual_memory()[
                                   2],
                                   process.cpu_affinity(),
                                   process.num_threads(),
                                   process.cpu_percent(),
                                   round(process.memory_percent(), 4))

                        cv.putText(show_frame, text, (0, 50),
                                   cv.FONT_HERSHEY_COMPLEX, 1, (250, 0, 255),
                                   1)
                        cv.imshow('Window', show_frame)
                        if cv.waitKey(1) & 0xFF == ord('q'):
                            break

                if len(process_order) > 0:
                    has_frame, frame = capture.read()
                    frame_count += 1
            else:
                frame_count += 1
                resized = cv.resize(frame, (W, H))
                # Change data layout from HWC to CHW
                resized = resized.transpose((2, 0, 1))
                input_data = resized.reshape((N, C, H, W))
                # Start Inference
                results = OpenVinoExecutable.infer(
                    inputs={InputLayer: input_data})

                fps = frame_count / (time.time() - start_time)
                inf_time = (time.time() - start_time) / frame_count
                # Write Information on Image
                text = 'FPS: {}, INF: {} ms'.format(round(fps, 3),
                                                    round(inf_time, 3))
                cv.putText(frame, text, (0, 20), cv.FONT_HERSHEY_COMPLEX, 0.8,
                           (0, 125, 255), 1)

                # Print Bounding Boxes on Image
                detections = results[OutputLayer][0][0]
                human_count = 0
                car_count = 0
                for detection in detections:

                    if detection[2] > arguments.detection_threshold:

                        xmin = int(detection[3] * fw)
                        ymin = int(detection[4] * fh)
                        xmax = int(detection[5] * fw)
                        ymax = int(detection[6] * fh)

                        if (detection[1] == 1):
                            car_count += 1
                            cv.rectangle(frame, (xmin, ymin), (xmax, ymax),
                                         (0, 255, 0), 3)
                            text = '{}, %: {}'.format("CAR",
                                                      round(detection[2], 3))
                            cv.putText(frame, text, (xmin, ymin - 7),
                                       cv.FONT_HERSHEY_PLAIN, 0.8, (0, 255, 0),
                                       1)
                        if (detection[1] == 2):
                            human_count += 1
                            cv.rectangle(frame, (xmin, ymin), (xmax, ymax),
                                         (0, 0, 255), 3)
                            text = '{}, %: {}'.format("HUMAN",
                                                      round(detection[2], 3))
                            cv.putText(frame, text, (xmin, ymin - 7),
                                       cv.FONT_HERSHEY_PLAIN, 0.8, (0, 0, 255),
                                       1)
                        detection_percentage = round(detection[2], 4)
                print("Number Of Humans present : ", human_count)
                print("Number Of Cars present : ", car_count)

                text = "SYS CPU% {} SYS MEM% {} \n " \
                       "PROC CPU Affinity {} \n " \
                       "NUM Threads {} \n " \
                       "PROC CPU% {} \n " \
                       "PROC MEM% {}".format(psutil.cpu_percent(),
                                             psutil.virtual_memory()[2],
                                             process.cpu_affinity(),
                                             process.num_threads(),
                                             process.cpu_percent(),
                                             round(process.memory_percent(), 4))

                cv.putText(frame, text, (0, 50), cv.FONT_HERSHEY_COMPLEX, 0.8,
                           (250, 0, 250), 1)
                cv.imshow('Window', frame)
                if cv.waitKey(1) & 0xFF == ord('q'):
                    break
                has_frame, frame = capture.read()

    print("---------")
    print("App ENDS")
Exemple #13
0
def main() :
    words = ['a', 'b', 'c', 'd', 'e', 'f', 
             'g', 'h', 'i', 'j', 'k', 'l', 
             'm', 'n', 'o', 'p', 'q', 'r', 
             's', 't', 'u', 'v', 'w', 'x', 
             'y', 'z', '0', '1', '2', '3', 
             '4', '5', '6', '7', '8', '9', 
             '-']    

    args = parsing().parse_args()

    model_graph = args.model
    model_weight = args.model[:-3] + 'bin'

    net = IENetwork(model = model_graph, 
                    weights = model_weight)

    # net.batch_size = 2

    iter_inputs = iter(net.inputs)
    iter_outputs = iter(net.outputs)
   
    # inputs_num = len(net.inputs)
    # print (inputs_num)

    '''
    input_blob = []
    for _inputs in iter_inputs:
        input_blob.append(_inputs)

    output_blob = []
    for _outputs in iter_outputs:
        output_blob.append(_outputs)
    '''

    '''
    input_l = []
    for i in input_blob:
        print (net.inputs[i].shape)
        input_l.append(np.ones(shape=net.inputs[i].shape, dtype=np.float32))

    inputs = dict()
    for i in range (inputs_num):
        inputs[input_blob[i]] = input_l[i]
    '''

    input_blob = next(iter_inputs)
    output_blob = next(iter_outputs)

    if args.input == '':
        input = np.ones(shape=net.inputs[input_blob].shape, dtype = np.float32)
    else :
        b, c, h, w = net.inputs[input_blob].shape
        print (b, c, h, w)
        import cv2
        input = cv2.imread(args.input)
        print (input.shape)
        input = cv2.resize(input, (w, h))
        input = input.transpose((2, 0, 1)).reshape(1, c, h, w) 

    plugin = IEPlugin(device = 'CPU')
    exec_net = plugin.load(network = net)
    # if args.cpu_extension :
    #    plugin.add_cpu_extension(args.cpu_extension)
    # res = plugin.impl.CTCGreedyDecoder();

    a =  np.concatenate((input, input), 0)
    print (a.shape)    

    inputs = {input_blob: input}
    out = exec_net.infer(inputs)

    # print (out[output_blob])
    
    print (decoder.CTCGreedyDecoder(out[output_blob], words, words[-1]))
    
    print (decoder.CTCBeamSearchDecoder(out[output_blob], words, words[-1], 50))


    ''' 
Exemple #14
0
def main():
    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    args = build_argparser().parse_args()
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    # Plugin initialization for specified device and load extensions library if specified
    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("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    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 sample's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)

    assert len(
        net.inputs.keys()) == 1, "Sample supports only single input topologies"
    assert len(
        net.outputs) == 1, "Sample supports only single output topologies"

    log.info("Preparing input blobs")
    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
    net.batch_size = len(args.input)

    # Read and pre-process input images
    n, c, h, w = net.inputs[input_blob].shape
    images = np.ndarray(shape=(n, c, h, w))
    for i in range(n):
        image = cv2.imread(args.input[i])
        original_h = image.shape[0]
        original_w = image.shape[1]
        if image.shape[:-1] != (h, w):
            log.warning("Image {} is resized from {} to {}".format(
                args.input[i], image.shape[:-1], (h, w)))
            image = cv2.resize(image, (w, h))
        image = image.transpose(
            (2, 0, 1))  # Change data layout from HWC to CHW
        image = image / 255.
        images[i] = image
    log.info("Batch size is {}".format(n))

    print("@@@@@@")
    print(images[0].shape)  # (1, 1, 224, 224)
    print(images[0])
    print("@@@@@@")

    # Loading model to the plugin
    log.info("Loading model to the plugin")
    exec_net = plugin.load(network=net)
    del net

    # Start sync inference
    log.info("Starting inference ({} iterations)".format(args.number_iter))
    infer_time = []
    for i in range(args.number_iter):
        t0 = time()
        res = exec_net.infer(inputs={input_blob: images})
        infer_time.append((time() - t0) * 1000)
    log.info("Average running time of one iteration: {} ms".format(
        np.average(np.asarray(infer_time))))
    if args.perf_counts:
        perf_counts = exec_net.requests[0].get_perf_counts()
        log.info("Performance counters:")
        print("{:<70} {:<15} {:<15} {:<15} {:<10}".format(
            'name', 'layer_type', 'exet_type', 'status', 'real_time, us'))
        for layer, stats in perf_counts.items():
            print("{:<70} {:<15} {:<15} {:<15} {:<10}".format(
                layer, stats['layer_type'], stats['exec_type'],
                stats['status'], stats['real_time']))

    # Processing output blob
    log.info("Processing output blob")
    res = res[out_blob]

    #np.set_printoptions(threshold=np.inf)
    print("@@@@@@")
    print(res.shape)  # (1, 1, 224, 224)
    print(res)
    print("@@@@@@")

    res = np.squeeze(res)  # (1, 224, 224)
    res = np.squeeze(res)  # (224, 224)

    result = sigmoid(res)
    print("@@@@@@")
    print(result.shape)
    print(result)
    print("@@@@@@")

    result = cv2.resize(result, (original_w, original_h))  ## to 960x540
    cv2.imwrite('out_openvino.png', (result * 255).astype(np.uint8))

    del exec_net
    del plugin
Exemple #15
0
def func(impath, x, device="CPU"):
    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    model_xml = "model.h5.xml"
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    # Plugin initialization for specified device and load extensions library if specified
    # log.info("Creating Inference Engine")
    ie = IECore()
    '''
    if args.cpu_extension and 'CPU' in args.device:
        ie.add_extension(args.cpu_extension, "CPU")'''
    # Read IR
    # log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    net = IENetwork(model=model_xml, weights=model_bin)

    assert len(
        net.inputs.keys()) == 1, "Sample supports only single input topologies"
    assert len(
        net.outputs) == 1, "Sample supports only single output topologies"

    # log.info("Preparing input blobs")
    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
    net.batch_size = 1

    # Read and pre-process input images
    n, c, h, w = net.inputs[input_blob].shape
    images = np.ndarray(shape=(n, c, h, w))
    for i in range(n):
        image = cv2.imread(impath)
        if image.shape[:-1] != (h, w):
            log.warning("Image {} is resized from {} to {}".format(
                impath, image.shape[:-1], (h, w)))
            image = cv2.resize(image, (w, h))
        image = image.transpose(
            (2, 0, 1))  # Change data layout from HWC to CHW
        images[i] = image
    # log.info("Batch size is {}".format(n))

    # Loading model to the plugin
    # log.info("Loading model to the plugin")
    # exec_net = ie.load_network(network=net, device_name='CPU')
    exec_net = ie.load_network(network=net, device_name=device)

    # Start sync inference
    # log.info("Starting inference in synchronous mode")
    res = exec_net.infer(inputs={input_blob: images})

    # Processing output blob
    # log.info("Processing output blob")
    res = res[out_blob]

    idx = np.argsort(res[0])[-1]
    h = np.argmax(res)
    k = category_list[h]
    print('The image belongs to class: {}'.format(k))
    if (k == x):
        ind = 1
        print("True\n")
    else:
        ind = 0
        print("False\n")
    return ind
Exemple #16
0
def main():
    total_timer = Timer(name='total')
    total_timer.tic()

    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    args = build_argparser().parse_args()
    # --------------------------- 1. Read IR Generated by ModelOptimizer (.xml and .bin files) ------------
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"
    log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    net = IENetwork(model=model_xml, weights=model_bin)
    # -----------------------------------------------------------------------------------------------------

    # ------------- 2. Load Plugin for inference engine and extensions library if specified --------------
    log.info("Loading Inference Engine")
    ie = IECore()
    log.info("Device info:")
    versions = ie.get_versions(args.device)
    print("{}{}".format(" " * 8, args.device))
    print("{}MKLDNNPlugin version ......... {}.{}".format(
        " " * 8, versions[args.device].major, versions[args.device].minor))
    print("{}Build ........... {}".format(" " * 8,
                                          versions[args.device].build_number))

    if args.cpu_extension and "CPU" in args.device:
        ie.add_extension(args.cpu_extension, "CPU")
        log.info("CPU extension loaded: {}".format(args.cpu_extension))

    if "CPU" in args.device:
        supported_layers = ie.query_network(net, "CPU")
        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(args.device, ', '.join(not_supported_layers)))
            log.error(
                "Please try to specify cpu extensions library path in sample's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)
    # -----------------------------------------------------------------------------------------------------

    # --------------------------- 4. Configure input & output ---------------------------------------------
    # --------------------------- Prepare input blobs -----------------------------------------------------
    log.info("Preparing input blobs")
    assert (len(net.inputs.keys()) == 1
            ), "Sample supports topologies only with 1 input"

    input_name = next(iter(net.inputs.keys()))
    input_info = net.inputs[input_name]
    # input_info.precision = 'FP32'
    input_info.precision = 'U8'

    # --------------------------- Prepare output blobs ----------------------------------------------------
    log.info('Preparing output blobs')
    assert (len(net.outputs.keys()) == 2
            ), "Sample supports topologies only with 2 output"

    loc_out_name = args.loc_out_name
    class_out_name = args.class_out_name
    assert (loc_out_name in net.outputs.keys()) and (class_out_name
                                                     in net.outputs.keys())

    loc_out_info = net.outputs[loc_out_name]
    class_out_info = net.outputs[class_out_name]

    loc_out_info.precision = "FP32"
    class_out_info.precision = "FP32"
    # -----------------------------------------------------------------------------------------------------

    # -----------------------------------------------------------------------------------------------------
    log.info("Loading model to the device")
    # ie.set_config({'CPU_THREADS_NUM': str(12)}, 'CPU')
    exec_net = ie.load_network(network=net, device_name=args.device)

    # # --------------------------- 3. Read and preprocess input --------------------------------------------
    # # -----------------------------------------------------------------------------------------------------
    if not os.path.exists(args.result_dir):
        os.makedirs(args.result_dir)

    if args.voc_res_file and os.path.exists(args.voc_res_file):
        os.remove(args.voc_res_file)

    # create_anchor_timer = Timer(name='create_anchor')
    # read_img_timer = Timer(name='read_img')
    # preprocess_timer = Timer(name='preprocess')
    # infer_timer = Timer(name='infer')
    # adapter_timer = Timer(name='adapter')
    # patch_img_nms_timer = Timer(name='patch_img_nms')
    # whole_img_nms_timer = Timer(name='whole_img_nms')
    # add_offset_timer = Timer(name='add_offset')
    # write_result_timer = Timer(name='write_result')

    queue = Queue()
    producer = Producer('Producer', queue, args)
    consumer = Consumer('Consumer', queue, exec_net, input_name, args)

    producer.start()
    consumer.start()

    producer.join()
    consumer.join()
    log.info('All threads finished!')

    total_timer.toc()
    # # -----------------------------------------------------------------------------------------------------
    all_timers = []
    # all_timers.extend([create_anchor_timer,
    #                    read_img_timer,
    #                    preprocess_timer,
    #                    infer_timer,
    #                    adapter_timer,
    #                    patch_img_nms_timer,
    #                    whole_img_nms_timer,
    #                    add_offset_timer,
    #                    write_result_timer,
    #                    total_timer])
    all_timers.extend([total_timer])
    for timer in all_timers:
        log.info('{}: avg: {:.2f} ms, total: {:.2f}s'.format(
            timer.name, timer.avg * 1000, timer.total))

    log.info("Execution successful\n")
Exemple #17
0
def main():
    log.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(message)s',
                    filename=LOG_FILE)
    model_xml = MODEL
    model_bin = os.path.splitext(model_xml)[0] + '.bin'
    # Plugin initialization
    log.info('Initializing plugin for {} device...'.format(DEVICE))
    plugin = IEPlugin(device=DEVICE, plugin_dirs='')
    # Read IR
    log.info('Reading IR...')
    net = IENetwork(model=model_xml, weights=model_bin)

    assert plugin.device != 'CPU'
    assert len(net.inputs.keys()) == 1
    assert len(net.outputs) == 1

    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

    cap = cv2.VideoCapture(VIDEO_IN)
    cur_request_id = 0
    next_request_id = 1

    log.info("Starting inference in async mode...")
    log.info("To stop the demo execution press Esc button")
    initial_w = IMG_W
    initial_h = IMG_H
    ret, frame = cap.read()
    if not ret:
        sys.exit('No input frame!')
    frame = cv2.resize(frame, (IMG_W, IMG_H))
    while cap.isOpened():
        ret, next_frame = cap.read()
        if not ret:
            break
        next_frame = cv2.resize(next_frame, (IMG_W, IMG_H))
        # Main sync point:
        # in the Async mode we start the NEXT infer request, while
        # waiting for the CURRENT to complete
        in_frame = cv2.resize(next_frame, (w, h))
        in_frame = in_frame.transpose((2, 0, 1))  # 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})
        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]
            event_detected = 0
            for obj in res[0][0]:
                if int(obj[1]) in DETECT_CLASS and obj[2] > CONF_THRESHOLD:
                    event_detected = 1
                    xmin = int(obj[3] * initial_w)
                    ymin = int(obj[4] * initial_h)
                    xmax = int(obj[5] * initial_w)
                    ymax = int(obj[6] * initial_h)
                    # Draw bounding box
                    color = (0, 255, 0)
                    cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), color, 2)
            check_notify(event_detected, frame)
        if DO_IMSHOW:
            cv2.imshow("Detection Results", frame)
            if cv2.waitKey(1) == 27:
                break
        cur_request_id, next_request_id = next_request_id, cur_request_id
        frame = next_frame

    cv2.destroyAllWindows()
    del exec_net
    del plugin
def Object_Detection(InputType):
    ####################################SETUP########################################
    #Object Detection
    print("Initializing Parameters")
    startTimerTime = 0.0
    endTimerTime = 0.0
    elapseTimerTime = 0.0
    setTimerTime = 10.0 #seconds (30sec)

    #Detection
    detectIterations = 0
    DOG_Stat = 0
    CAT_Stat = 0
    PERSON_Stat = 0
    confidenceTimeThreshold = .70

    # Command Line Interface #
    # construct the argument parser and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-c", "--conf", required=True,
        help="Path to the input configuration file")
    ap.add_argument("-i", "--input", help="path to the input video file")
    args = vars(ap.parse_args())

    # load the configuration file
    conf = Conf(args["conf"])

    # COCO label and corresponding color preparation #
    # load the COCO class labels that YOLO model was trained on and
    # initialize a list of colors to represent each possible class label
    LABELS = open(conf["labels_path"]).read().strip().split("\n")
    np.random.seed(42)
    COLORS = np.random.uniform(0, 255, size=(len(LABELS), 3))

    # Load tinyYOLOv3 modle onto Movidius NCS2 #
    # initialize the plugin in for specified device
    plugin = IEPlugin(device="MYRIAD")

    # read the IR generated by the Model Optimizer (.xml and .bin files)
    #.xml is YOLO architecture & .bin is the pretrained weights
    print("[INFO] loading models...")
    net = IENetwork(model=conf["xml_path"], weights=conf["bin_path"])

    # prepare inputs
    print("[INFO] preparing inputs...")
    inputBlob = next(iter(net.inputs))

    # set the default batch size as 1 and get the number of input blobs,
    # number of channels, the height, and width of the input blob
    net.batch_size = 1
    (n, c, h, w) = net.inputs[inputBlob].shape

    # /Image input reference for tinyYOLOv3 #
    # if a video path was not supplied, grab a reference to the webcam
    if args["input"] is None:
        print("[INFO] starting video stream...")
        # vs = VideoStream(src=0).start() #USB Camera
        vs = VideoStream(usePiCamera=True).start() #Pi Camera
        time.sleep(2.0)
        # otherwise, grab a reference to the video file
    else:
        print("[INFO] opening image/video file...")
        vs = cv2.VideoCapture(os.path.abspath(args["input"]))
        
    # loading model to the plugin and start the frames per second
    # throughput estimator
    print("[INFO] loading model to the plugin...")
    execNet = plugin.load(network=net, num_requests=1)
    fps = FPS().start()

    ######################################Processing#####################################

    print("[INFO] Runnig Detection...")     
    startTimerTime = time.perf_counter()
    while(elapseTimerTime < setTimerTime):
        
        # grab the next frame and handle VideoCapture or VideoStream
        orig = vs.read()
        orig = orig[1] if args["input"] is not None else orig

        # if we are viewing a video and we did not grab a frame then we
        # have reached the end of the video
        if args["input"] is not None and orig is None:
            break

        # resize original frame to have a maximum width of 500 pixel and
        # input_frame to network size
        orig = imutils.resize(orig, width=500)
        frame = cv2.resize(orig, (w, h))

        # change data layout from HxWxC to CxHxW
        frame = frame.transpose((2, 0, 1))
        frame = frame.reshape((n, c, h, w))

        # start inference and initialize list to collect object detection
        # results
        output = execNet.infer({inputBlob: frame})
        objects = []

        # loop over the output items
        for (layerName, outBlob) in output.items():
            # create an object with required tinyYOLOv3 parameters
            layerParams = TinyYOLOV3Params(net.layers[layerName].params,
                outBlob.shape[2])

            # parse the output region
            objects += TinyYOLOv3.parse_yolo_region(outBlob,
                frame.shape[2:], orig.shape[:-1], layerParams,
                conf["prob_threshold"])

        # loop over each of the objects
        for i in range(len(objects)):
            # check if the confidence of the detected object is zero, if
            # it is, then skip this iteration, indicating that the object
            # should be ignored
            if objects[i]["confidence"] == 0:
                continue

            # loop over remaining objects[Intersection over Union (IoU)]
            for j in range(i + 1, len(objects)):
                # check if the IoU of both the objects exceeds a
                # threshold, if it does, then set the confidence of
                # that object to zero
                if TinyYOLOv3.intersection_over_union(objects[i],
                    objects[j]) > conf["iou_threshold"]:
                    objects[j]["confidence"] = 0

        # filter objects by using the probability threshold -- if a an
        # object is below the threshold, ignore it
        objects = [obj for obj in objects if obj['confidence'] >= \
            conf["prob_threshold"]]

    ############################Object Detection Frame & Stats###########################

        # store the height and width of the original frame
        (endY, endX) = orig.shape[:-1]

        # loop through all the remaining objects
        for obj in objects:
            # validate the bounding box of the detected object, ensuring
            # we don't have any invalid bounding boxes
            if obj["xmax"] > endX or obj["ymax"] > endY or obj["xmin"] \
                < 0 or obj["ymin"] < 0:
                continue

            # build a label consisting of the predicted class and
            # associated probability
            label = "{}: {:.2f}%".format(LABELS[obj["class_id"]],
                obj["confidence"] * 100)
            print(label)
            
            tag = LABELS[obj["class_id"]]
            if (tag == "person"):
                PERSON_Stat = PERSON_Stat+ 1
            elif (tag == "dog"):
                DOG_Stat = DOG_Stat+ 1
            elif(tag == "cat"):
                CAT_Stat = CAT_Stat+ 1
                
            # calculate the y-coordinate used to write the label on the
            # frame depending on the bounding box coordinate
            y = obj["ymin"] - 15 if obj["ymin"] - 15 > 15 else \
                obj["ymin"] + 15

            # draw a bounding box rectangle and label on the frame
            cv2.rectangle(orig, (obj["xmin"], obj["ymin"]), (obj["xmax"],
                obj["ymax"]), COLORS[obj["class_id"]], 2)
            cv2.putText(orig, label, (obj["xmin"], y),
                cv2.FONT_HERSHEY_SIMPLEX, 1, COLORS[obj["class_id"]], 3)

        # display the current frame to the screen
        cv2.imshow("TinyYOLOv3", orig)

        # update the FPS counter
        fps.update()
        
        detectIterations = detectIterations + 1
        endTimerTime = time.perf_counter()
        elapseTimerTime = endTimerTime - startTimerTime #seconds
        print(elapseTimerTime)

    # stop the timer and display FPS information
    fps.stop()
    print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
    print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
    print("[INFO] Time Frame Calculation for Person: ", PERSON_Stat)
    print("[INFO] Time Frame Calculation for Dog: ", DOG_Stat)
    print("[INFO] Time Frame Calculation for Cat: ", CAT_Stat)

    #calculate confidence level
    print("Calculating OD Stats")
    print("Iterations :", detectIterations)
    print("TimeFrame :", elapseTimerTime)
    if(InputType == 'v'):
        IterationPercentage = elapseTimerTime / detectIterations
    if(InputType == 'i'):
        IterationPercentage = detectIterations
    print(IterationPercentage)

    DOG_Final= DOG_Stat * IterationPercentage
    print(DOG_Final)

    CAT_Final= CAT_Stat * IterationPercentage
    print(CAT_Final)

    PERSON_Final= PERSON_Stat * IterationPercentage
    print(PERSON_Final)

    if(((DOG_Final or CAT_Final) > confidenceTimeThreshold) and (PERSON_Final < confidenceTimeThreshold )):
        print("[INFO] Detected ...")
        print("Reading TEMP")
        Current_Temp = readTemp()
        print("Current Temp: ", Current_Temp, "*C")

        return Current_Temp
    else:
        print("[INFO] No Detections ...")
        return 0
Exemple #19
0
def main():
    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    args = build_argparser().parse_args()
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    # Plugin initialization for specified device and load extensions library if specified
    log.info("Initializing plugin for {} device...".format(args.device))
    ie = IECore()
    #plugin = IEPlugin(device=args.device, plugin_dirs=args.plugin_dir)
    if args.cpu_extension and 'CPU' in args.device:
        log.info("Loading plugins for {} device...".format(args.device))
        #ie.add_extension(args.cpu_extension, args.device) // openvino2019-R1
        ie.add_extension(args.cpu_extension, "CPU")

    # Read IR
    log.info("Reading IR...")
    net = IENetwork(model=model_xml, weights=model_bin)

    if "CPU" in args.device:
        supported_layers = ie.query_network(net, "CPU")
        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(args.device, ', '.join(not_supported_layers)))
            log.error(
                "Please try to specify cpu extensions library path in sample's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)
    assert len(
        net.inputs.keys()) == 1, "Sample supports only single input topologies"
    assert len(
        net.outputs) == 1, "Sample supports only single output topologies"

    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))

    if args.input == 'cam':
        input_stream = 0
        out_file_name = 'cam'
    else:
        input_stream = args.input
        assert os.path.isfile(args.input), "Specified input file doesn't exist"
        out_file_name = os.path.splitext(os.path.basename(args.input))[0]

    log.info("Loading IR to the plugin...")
    exec_net = ie.load_network(network=net,
                               num_requests=args.number_infer_requests,
                               device_name=args.device)

    log.info(
        "Starting inference in async mode, {} requests in parallel...".format(
            args.number_infer_requests))
    job_id = str(os.environ['PBS_JOBID'])
    result_file = open(
        os.path.join(args.output_dir, 'output_' + job_id + '.txt'), "w")
    pre_infer_file = os.path.join(args.output_dir,
                                  'pre_progress_' + job_id + '.txt')
    infer_file = os.path.join(args.output_dir, 'i_progress_' + job_id + '.txt')
    processed_vid = '/tmp/processed_vid.bin'

    # Read and pre-process input image
    if isinstance(net.inputs[input_blob], list):
        n, c, h, w = net.inputs[input_blob]
    else:
        n, c, h, w = net.inputs[input_blob].shape
    del net

    cap = cv2.VideoCapture(input_stream)
    video_len = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    if video_len < args.number_infer_requests:
        args.number_infer_requests = video_len
    #Pre inference processing, read mp4 frame by frame, process using openCV and write to binary file
    width = int(cap.get(3))
    height = int(cap.get(4))
    CHUNKSIZE = n * c * w * h
    id_ = 0
    with open(processed_vid, 'w+b') as f:
        time_start = time.time()
        while cap.isOpened():
            ret, next_frame = cap.read()
            if not ret:
                break
            in_frame = cv2.resize(next_frame, (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))
            bin_frame = bytearray(in_frame)
            f.write(bin_frame)
            id_ += 1
            if id_ % 10 == 0:
                progressUpdate(pre_infer_file,
                               time.time() - time_start, id_, video_len)
    cap.release()

    if args.labels:
        with open(args.labels, 'r') as f:
            labels_map = [x.strip() for x in f]
    else:
        labels_map = None

    current_inference = 0
    previous_inference = 1 - args.number_infer_requests
    infer_requests = exec_net.requests
    frame_count = 0

    try:
        infer_time_start = time.time()
        with open(processed_vid, "rb") as data:
            while frame_count < video_len:
                # Read next frame from input stream if available and submit it for inference
                byte = data.read(CHUNKSIZE)
                if not byte == b"":
                    deserialized_bytes = np.frombuffer(byte, dtype=np.uint8)
                    in_frame = np.reshape(deserialized_bytes,
                                          newshape=(n, c, h, w))
                    exec_net.start_async(request_id=current_inference,
                                         inputs={input_blob: in_frame})

                # Retrieve the output of an earlier inference request
                if previous_inference >= 0:
                    status = infer_requests[previous_inference].wait()
                    if status is not 0:
                        raise Exception(
                            "Infer request not completed successfully")
                    # Parse inference results
                    res = infer_requests[previous_inference].outputs[out_blob]
                    processBoxes(frame_count, res, labels_map,
                                 args.prob_threshold, width, height,
                                 result_file)
                    frame_count += 1

                # Write data to progress tracker
                if frame_count % 10 == 0:
                    progressUpdate(infer_file,
                                   time.time() - infer_time_start,
                                   frame_count + 1, video_len + 1)

                # Increment counter for the inference queue and roll them over if necessary
                current_inference += 1
                if current_inference >= args.number_infer_requests:
                    current_inference = 0

                previous_inference += 1
                if previous_inference >= args.number_infer_requests:
                    previous_inference = 0

        # End while loop
        total_time = time.time() - infer_time_start
        with open(os.path.join(args.output_dir, 'stats_{}.txt'.format(job_id)),
                  'w') as f:
            f.write('{:.3g} \n'.format(total_time))
            f.write('{} \n'.format(frame_count))

        result_file.close()

    finally:
        log.info("Processing done...")
        del exec_net
Exemple #20
0
def main():
    # Argument parsing and parameter setting
    ARGS = parse_args().parse_args()
    input_stream = ARGS.input
    labels = ARGS.labels
    if ARGS.input.lower() == "cam" or ARGS.input.lower() == "camera":
        input_stream = 0
    ir = ARGS.ir
    detection_threshold = ARGS.threshold

    # Prepare Categories
    with open(labels) as labels_file:
        label_list = labels_file.read().splitlines()

    print(YELLOW + 'Running OpenVINO NCS Tensorflow TinyYolo v3 example...' +
          NOCOLOR)
    print('\n Displaying image with objects detected in GUI...')
    print(' Click in the GUI window and hit any key to exit.')

    ####################### 1. Create ie core and network #######################
    # Select the myriad plugin and IRs to be used
    ie = IECore()
    net = IENetwork(model=ir, weights=ir[:-3] + 'bin')

    # Set up the input blobs
    input_blob = next(iter(net.inputs))
    input_shape = net.inputs[input_blob].shape

    # Display model information
    display_info(input_shape, net.outputs, input_stream, ir, labels)

    # Load the network and get the network input shape information
    exec_net = ie.load_network(network=net, device_name=DEVICE)
    n, c, network_input_h, network_input_w = input_shape

    # Prepare the input stream
    cap = cv2.VideoCapture(input_stream)
    cap.set(cv2.CAP_PROP_FPS, 30)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, WINDOW_SIZE_W)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, WINDOW_SIZE_H)

    # Read a frame
    ret, frame = cap.read()
    # Width and height calculations. These will be used to scale the bounding boxes
    source_image_width = frame.shape[1]
    source_image_height = frame.shape[0]
    scaled_w = int(source_image_width *
                   min(network_input_w / source_image_width,
                       network_input_w / source_image_height))
    scaled_h = int(source_image_height *
                   min(network_input_h / source_image_width,
                       network_input_h / source_image_height))

    while cap.isOpened():
        # Make a copy of the original frame. Get the frame's width and height.
        if frame is None:
            print(RED + "\nUnable to read the input." + NOCOLOR)
            quit()
####################### 2. Preprocessing #######################
# Image preprocessing
        frame = cv2.flip(frame, 1)

        display_image = frame

        # Image preprocessing (resize, transpose, reshape)
        input_image = cv2.resize(frame, (network_input_w, network_input_h),
                                 cv2.INTER_LINEAR)
        input_image = input_image.astype(np.float32)
        input_image = np.transpose(input_image, (2, 0, 1))
        reshaped_image = input_image.reshape(
            (n, c, network_input_h, network_input_w))
        ####################### 3. Perform Inference #######################
        # Perform the inference asynchronously
        req_handle = exec_net.start_async(request_id=0,
                                          inputs={input_blob: reshaped_image})
        status = req_handle.wait()
        ####################### 4. Get results #######################
        all_output_results = req_handle.outputs

        ####################### 5. Post processing for results #######################
        # Post-processing for tiny yolo v3
        # The post process consists of the following steps:
        # 1. Parse the output and filter out low scores
        # 2. Filter out duplicates using intersection over union
        # 3. Draw boxes and text

        ## 1. Tiny yolo v3 has two outputs and we check/parse both outputs
        filtered_objects = []
        for output_node_results in all_output_results.values():
            parseTinyYoloV3Output(output_node_results, filtered_objects,
                                  source_image_width, source_image_height,
                                  scaled_w, scaled_h, detection_threshold,
                                  len(label_list))

        ## 2. Filter out duplicate objects from all detected objects
        filtered_mask = get_duplicate_box_mask(filtered_objects)
        ## 3. Draw rectangles and set up display texts
        for object_index in range(len(filtered_objects)):
            if filtered_mask[object_index] == True:
                # get all values from the filtered object list
                xmin = filtered_objects[object_index][0]
                ymin = filtered_objects[object_index][1]
                xmax = filtered_objects[object_index][2]
                ymax = filtered_objects[object_index][3]
                confidence = filtered_objects[object_index][4]
                class_id = filtered_objects[object_index][5]
                # Set up the text for display
                cv2.rectangle(display_image, (xmin, ymin), (xmax, ymin + 20),
                              LABEL_BG_COLOR, -1)
                cv2.putText(display_image,
                            label_list[class_id] + ': %.2f' % confidence,
                            (xmin + 5, ymin + 15), TEXT_FONT, 0.5, TEXT_COLOR,
                            1)
                # Set up the bounding box
                cv2.rectangle(display_image, (xmin, ymin), (xmax, ymax),
                              BOX_COLOR, 1)

        # Now we can display the results!
        cv2.imshow("OpenVINO Tiny yolo v3 - Press any key to quit",
                   display_image)

        # Handle key presses
        # Get another frame from camera if using camera input
        if input_stream == 0:
            key = cv2.waitKey(1)
            if key != -1:  # if used pressed a key, release the capture stream and break
                cap.release()
                break
            ret, frame = cap.read()  # read another frame
        else:  #  or wait for key press if image input
            print("\n Press any key to quit.")
            while True:
                key = cv2.waitKey(1)
                if key != -1:
                    cap.release()
                    break

    # Clean up
    cv2.destroyAllWindows()
    del net
    del exec_net
    print('\n Finished.')
Exemple #21
0
def make_network(model, weights):
    return IENetwork(model = model, weights = weights)
Exemple #22
0
def main():
    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    args = build_argparser().parse_args()
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    # Plugin initialization for specified device and load extensions library if specified
    log.info("Creating Inference Engine")
    ie = IECore()
    if args.cpu_extension and 'CPU' in args.device:
        ie.add_extension(args.cpu_extension, "CPU")
    # Read IR
    log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    net = IENetwork(model=model_xml, weights=model_bin)

    if "CPU" in args.device:
        supported_layers = ie.query_network(net, "CPU")
        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(args.device, ', '.join(not_supported_layers)))
            log.error(
                "Please try to specify cpu extensions library path in sample's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)

    assert len(
        net.inputs.keys()) == 1, "Sample supports only single input topologies"
    assert len(
        net.outputs) == 1, "Sample supports only single output topologies"

    log.info("Preparing input blobs")
    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
    net.batch_size = len(args.input)

    # Read and pre-process input images
    n, c, h, w = net.inputs[input_blob].shape
    images = np.ndarray(shape=(n, c, h, w))
    for i in range(n):
        image = cv2.imread(args.input[i])
        if image.shape[:-1] != (h, w):
            log.warning("Image {} is resized from {} to {}".format(
                args.input[i], image.shape[:-1], (h, w)))
            image = cv2.resize(image, (w, h))
        image = image.transpose(
            (2, 0, 1))  # Change data layout from HWC to CHW
        images[i] = image
    log.info("Batch size is {}".format(n))

    # Loading model to the plugin
    log.info("Loading model to the plugin")
    exec_net = ie.load_network(network=net, device_name=args.device)

    # Start sync inference
    log.info("Starting inference")
    res = exec_net.infer(inputs={input_blob: images})

    # Processing output blob
    log.info("Processing output blob")
    res = res[out_blob]
    # Post process output
    for batch, data in enumerate(res):
        # Clip values to [0, 255] range
        data = np.swapaxes(data, 0, 2)
        data = np.swapaxes(data, 0, 1)
        data = cv2.cvtColor(data, cv2.COLOR_BGR2RGB)
        data[data < 0] = 0
        data[data > 255] = 255
        data = data[::] - (args.mean_val_r, args.mean_val_g, args.mean_val_b)
        out_img = os.path.join(os.path.dirname(__file__),
                               "out_{}.bmp".format(batch))
        cv2.imwrite(out_img, data)
        log.info("Result image was saved to {}".format(out_img))
    log.info(
        "This sample is an API example, for any performance measurements please use the dedicated benchmark_app tool\n"
    )
Exemple #23
0
    if (width_of_overlap_area < 0.0 or height_of_overlap_area < 0.0):
        area_of_overlap = 0.0
    else:
        area_of_overlap = width_of_overlap_area * height_of_overlap_area
    box_1_area = (box_1.ymax - box_1.ymin) * (box_1.xmax - box_1.xmin)
    box_2_area = (box_2.ymax - box_2.ymin) * (box_2.xmax - box_2.xmin)
    area_of_union = box_1_area + box_2_area - area_of_overlap
    retval = 0.0
    if area_of_union <= 0.0:
        retval = 0.0
    else:
        retval = (area_of_overlap / area_of_union)
    return retval


net = IENetwork(model=xml_path, weights=bin_path)

plugin = IEPlugin(device='MYRIAD')
exec_net = plugin.load(net)

cam = WebcamVideoStream(src=0).start()

while (True):
    t1 = time.time()

    frame = cam.read()

    frame = cv2.resize(frame, (cam_w, cam_h), interpolation=cv2.INTER_AREA)

    prepimg = preprocess(frame, image_size, new_w, new_h)
Exemple #24
0
def main():
    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    args = build_argparser().parse_args()
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    # Plugin initialization for specified device and load extensions library if specified
    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("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    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 sample's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)

    assert len(
        net.inputs.keys()) == 1, "Sample supports only single input topologies"
    assert len(
        net.outputs) == 1, "Sample supports only single output topologies"

    log.info("Preparing input blobs")
    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
    net.batch_size = len(args.input)

    # Read and pre-process input images
    n, c, h, w = net.inputs[input_blob].shape
    images = np.ndarray(shape=(n, c, h, w))
    for i in range(n):
        image = cv2.imread(args.input[i])
        #if image.shape[:-1] != (h, w):
        #    log.warning("Image {} is resized from {} to {}".format(args.input[i], image.shape[:-1], (h, w)))
        #    image = cv2.resize(image, (w, h))

        #image = image.transpose((2, 0, 1))  # Change data layout from HWC to CHW
        image = preprocess(image, h, w)
        images[i] = image
    log.info("Batch size is {}".format(n))

    # Loading model to the plugin
    log.info("Loading model to the plugin")
    exec_net = plugin.load(network=net)
    del net

    # Start sync inference
    log.info("Starting inference ({} iterations)".format(args.number_iter))
    infer_time = []
    for i in range(args.number_iter):
        t0 = time()
        res = exec_net.infer(inputs={input_blob: images})
        infer_time.append((time() - t0) * 1000)
    log.info("Average running time of one iteration: {} ms".format(
        np.average(np.asarray(infer_time))))
    if args.perf_counts:
        perf_counts = exec_net.requests[0].get_perf_counts()
        log.info("Performance counters:")
        print("{:<70} {:<15} {:<15} {:<15} {:<10}".format(
            'name', 'layer_type', 'exet_type', 'status', 'real_time, us'))
        for layer, stats in perf_counts.items():
            print("{:<70} {:<15} {:<15} {:<15} {:<10}".format(
                layer, stats['layer_type'], stats['exec_type'],
                stats['status'], stats['real_time']))

    # Processing output blob
    log.info("Processing output blob")
    res = res[out_blob]
    log.info("Top {} results: ".format(args.number_top))
    if args.labels:
        with open(args.labels, 'r') as f:
            labels_map = [x.split(sep=' ', maxsplit=1)[-1].strip() for x in f]
    else:
        labels_map = None
    for i, probs in enumerate(res):
        probs = np.squeeze(probs)
        top_ind = np.argsort(probs)[-args.number_top:][::-1]
        print("Image {}\n".format(args.input[i]))
        for id in top_ind:
            det_label = labels_map[id] if labels_map else "#{}".format(id)
            print("{:.7f} label {}".format(probs[id], det_label))
        print("\n")

    del exec_net
    del plugin
Exemple #25
0
def main(camera_FPS, camera_width, camera_height, inference_scale, threshold, device):

    path = "pictures/"
    if not os.path.exists(path):
        os.mkdir(path)

    model_path = "OneClassAnomalyDetection-RaspberryPi3/DOC/model/" 
    if os.path.exists(model_path):
        # LOF
        print("LOF model building...")
        x_train = np.loadtxt(model_path + "train.csv",delimiter=",")

        ms = MinMaxScaler()
        x_train = ms.fit_transform(x_train)

        # fit the LOF model
        clf = LocalOutlierFactor(n_neighbors=5)
        clf.fit(x_train)

        # DOC
        print("DOC Model loading...")
        if device == "MYRIAD":
            model_xml="irmodels/tensorflow/FP16/weights.xml"
            model_bin="irmodels/tensorflow/FP16/weights.bin"
        else:
            model_xml="irmodels/tensorflow/FP32/weights.xml"
            model_bin="irmodels/tensorflow/FP32/weights.bin"
        net = IENetwork(model=model_xml, weights=model_bin)
        plugin = IEPlugin(device=device)
        if device == "CPU":
            if platform.processor() == "x86_64":
                plugin.add_cpu_extension("lib/x86_64/libcpu_extension.so")
        exec_net = plugin.load(network=net)
        input_blob = next(iter(net.inputs))
        print("loading finish")
    else:
        print("Nothing model folder")
        sys.exit(0)

    base_range = min(camera_width, camera_height)
    stretch_ratio = inference_scale / base_range
    resize_image_width = int(camera_width * stretch_ratio)
    resize_image_height = int(camera_height * stretch_ratio)

    if base_range == camera_height:
        crop_start_x = (resize_image_width - inference_scale) // 2
        crop_start_y = 0
    else:
        crop_start_x = 0
        crop_start_y = (resize_image_height - inference_scale) // 2
    crop_end_x = crop_start_x + inference_scale
    crop_end_y = crop_start_y + inference_scale

    fps = ""
    message = "Push [p] to take a picture"
    result = "Push [s] to start anomaly detection"
    flag_score = False
    picture_num = 1
    elapsedTime = 0
    score = 0
    score_mean = np.zeros(10)
    mean_NO = 0

    cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FPS, camera_FPS)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, camera_width)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, camera_height)

    time.sleep(1)

    while cap.isOpened():
        t1 = time.time()

        ret, image = cap.read()

        if not ret:
            break

        image_copy = image.copy()

        # prediction
        if flag_score == True:
            prepimg = cv2.resize(image, (resize_image_width, resize_image_height))
            prepimg = prepimg[crop_start_y:crop_end_y, crop_start_x:crop_end_x]
            prepimg = np.array(prepimg).reshape((1, inference_scale, inference_scale, 3))
            prepimg = prepimg / 255
            prepimg = prepimg.transpose((0, 3, 1, 2))

            exec_net.start_async(request_id = 0, inputs={input_blob: prepimg})
            exec_net.requests[0].wait(-1)
            outputs = exec_net.requests[0].outputs["Reshape_"]
            outputs = outputs.reshape((len(outputs), -1))
            outputs = ms.transform(outputs)
            score = -clf._decision_function(outputs)

        # output score
        if flag_score == False:
            cv2.putText(image, result, (camera_width - 350, 100), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA)
        else:
            score_mean[mean_NO] = score[0]
            mean_NO += 1
            if mean_NO == len(score_mean):
                mean_NO = 0
                
            if np.mean(score_mean) > threshold: #red if score is big
                cv2.putText(image, "{:.1f} Score".format(np.mean(score_mean)),(camera_width - 230, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1, cv2.LINE_AA)
            else: # blue if score is small
                cv2.putText(image, "{:.1f} Score".format(np.mean(score_mean)),(camera_width - 230, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 1, cv2.LINE_AA)
              
        # message
        cv2.putText(image, message, (camera_width - 285, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
        cv2.putText(image, fps, (camera_width - 164, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0 ,0), 1, cv2.LINE_AA)

        cv2.imshow("Result", image)
            
        # FPS
        elapsedTime = time.time() - t1
        fps = "{:.0f} FPS".format(1/elapsedTime)

        # quit or calculate score or take a picture
        key = cv2.waitKey(1)&0xFF
        if key == ord("q"):
            break
        if key == ord("p"):
            cv2.imwrite(path + str(picture_num) + ".jpg", image_copy)
            picture_num += 1
        if key == ord("s"):
            flag_score = True

    cv2.destroyAllWindows()
def main():

    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    args = build_argparser().parse_args()

    # Plugin initialization for specified device and
    #     load extensions library if specified
    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
    # If using MYRIAD then we need to load FP16 model version
    model_xml, model_bin = load_model(args.device == "MYRIAD")

    log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    net = IENetwork(model=model_xml, weights=model_bin)
    # net = IENetwork.from_ir(model=model_xml, weights=model_bin) # Old API
    """
    This code checks to see if all of the graphs in the IR are
    compatible with OpenVINO. If not, then you'll need to probably
    try to load in an extension library from ${INTEL_CVSDK_DIR}/inference_engine/lib
    """
    if "CPU" in plugin.device:
        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 sample's command line parameters using -l "
                      "or --cpu_extension command line argument")
            log.error(
                "On CPU this is usually -l ${INTEL_CVSDK_DIR}/inference_engine/lib/centos_7.4/intel64/libcpu_extension_avx2.so"
            )
            log.error(
                "You may need to build the OpenVINO samples directory for this library to be created on your system."
            )
            log.error(
                "e.g. bash ${INTEL_CVSDK_DIR}/inference_engine/samples/build_samples.sh will trigger the library to be built."
            )
            log.error(
                "Replace 'centos_7.4' with the pathname on your computer e.g. ('ubuntu_16.04')"
            )
            sys.exit(1)

    assert len(
        net.inputs.keys()) == 1, "Sample supports only single input topologies"
    assert len(
        net.outputs) == 1, "Sample supports only single output topologies"
    """
    Ask OpenVINO for input and output tensor names and sizes
    """
    input_blob = next(iter(net.inputs))  # Name of the input layer
    out_blob = next(iter(net.outputs))  # Name of the output layer

    batch_size, n_channels, height, width, depth = net.inputs[input_blob].shape
    batch_size, n_out_channels, height_out, width_out, depth_out = net.outputs[
        out_blob].shape
    net.batch_size = batch_size

    log.info("Batch size = {}".format(batch_size))

    # Load data
    input_data, label_data, img_indicies = load_data(args)

    # Loading model to the plugin
    exec_net = plugin.load(network=net)
    del net

    if args.stats:
        # Print the latency and throughput for inference
        print_stats(exec_net, input_data, n_channels, batch_size, input_blob,
                    out_blob, args)
    """
    OpenVINO inference code
    input_blob is the name (string) of the input tensor in the graph
    out_blob is the name (string) of the output tensor in the graph
    Essentially, this looks exactly like a feed_dict for TensorFlow inference
    """
    # Go through the sample validation dataset to plot predictions
    predictions = np.zeros((input_data.shape[0], n_out_channels, height_out,
                            width_out, depth_out))

    for idx in range(0, input_data.shape[0], batch_size):
        log.info("{} started".format(idx))
        res = exec_net.infer(
            inputs={
                input_blob: input_data[idx:(idx + batch_size), :n_channels]
            })
        log.info("{} finished".format(idx))
        # Save the predictions to array
        predictions[idx:(idx + batch_size), ] = res[out_blob]

    if idx != (len(img_indicies) - 1):  # Partial batch left in data
        log.info("Partial batch left over in dataset.")
    """
    Evaluate model with Dice metric
    """
    for idx in range(input_data.shape[0]):
        dice = dice_score(predictions[idx, 0, :, :, :], label_data[idx,
                                                                   0, :, :, :])
        log.info("Image #{}: Dice score = {:.4f}".format(
            img_indicies[idx], dice))

    del exec_net
    del plugin
Exemple #27
0
def main():

    job_id = os.environ['PBS_JOBID']
    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    args = build_argparser().parse_args()
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"
    log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    device = args.device

    # Plugin initialization for specified device and load extensions library if specified
    plugin = IEPlugin(device=device)

    # Read IR
    net = IENetwork(model=model_xml, weights=model_bin)

    assert len(
        net.inputs.keys()) == 1, "Sample supports only single input topologies"

    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))

    net.batch_size = 1

    exec_net = plugin.load(network=net)

    n, c, h, w = net.inputs[input_blob].shape
    files = glob.glob(os.getcwd() + args.input[0])
    if not os.path.isdir(args.output_dir):
        os.makedirs(args.output_dir, exist_ok=True)
    f = open(os.path.join(args.output_dir, 'result' + job_id + '.txt'), 'w')
    f1 = open(os.path.join(args.output_dir, 'stats' + job_id + '.txt'), 'w')
    time_images = []
    for file in files:
        [image1, image] = read_image(file)
        t0 = time()
        for i in range(args.number_iter):
            res = exec_net.infer(inputs={input_blob: image1})
            #infer_time.append((time()-t0)*1000)
        infer_time = (time() - t0) * 1000
        log.info("Average running time of one iteration: {} ms".format(
            np.average(np.asarray(infer_time))))
        if args.perf_counts:
            perf_counts = exec_net.requests[0].get_perf_counts()
            log.info("Performance counters:")
            print("{:<70} {:<15} {:<15} {:<15} {:<10}".format(
                'name', 'layer_type', 'exet_type', 'status', 'real_time, us'))
            for layer, stats in perf_counts.items():
                print("{:<70} {:<15} {:<15} {:<15} {:<10}".format(
                    layer, stats['layer_type'], stats['exec_type'],
                    stats['status'], stats['real_time']))
        res_pb = res[out_blob]
        probs = res_pb[0][0]
        print("Probability of having disease= " + str(probs) +
              ", performed in " + str(np.average(np.asarray(infer_time))) +
              " ms")

        avg_time = round((infer_time / args.number_iter), 1)

        #f.write(res + "\n Inference performed in " + str(np.average(np.asarray(infer_time))) + "ms")
        f.write("Pneumonia probability: " + str(probs) +
                "\n Inference performed in " + str(avg_time) + "ms \n")
        time_images.append(avg_time)
    f1.write(str(np.average(np.asarray(time_images))) + '\n')
    f1.write(str(1))
Exemple #28
0
# load the configuration file
conf = Conf(args["conf"])

# load the COCO class labels our YOLO model was trained on and
# initialize a list of colors to represent each possible class
# label
LABELS = open(conf["labels_path"]).read().strip().split("\n")
np.random.seed(42)
COLORS = np.random.uniform(0, 255, size=(len(LABELS), 3))
ANSWER = ["yes", "no"]
# initialize the plugin in for specified device
plugin = IEPlugin(device="MYRIAD")

# read the IR generated by the Model Optimizer (.xml and .bin files)
print("[INFO] loading models...")
net = IENetwork(model=conf["xml_path"], weights=conf["bin_path"])

# prepare inputs
print("[INFO] preparing inputs...")
inputBlob = next(iter(net.inputs))

# set the default batch size as 1 and get the number of input blobs,
# number of channels, the height, and width of the input blob
net.batch_size = 1
(n, c, h, w) = net.inputs[inputBlob].shape

# if a video path was not supplied, grab a reference to the webcam
#load videostream to use stt.py
if args["input"] is None:
    print("[INFO] starting video stream...")
    vs = VideoStream(src=0).start()
Exemple #29
0
def main():
    
    pplCount = 0
    currentCount = 0
    lastCount = 0
    totalCount = 0
    duration = 0
    elapsedtime = getTime()
    people = 0
    totalDuration = 0
	
    log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout)
    args = build_argparser().parse_args()
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"
    # Plugin initialization for specified device and load extensions library if specified
    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.input == 'cam':
        input_stream = 0
    else:
        input_stream = args.input
        assert os.path.isfile(args.input), "Specified input file doesn't exist"
    if args.labels:
        with open(args.labels, 'r') as f:
            labels_map = [x.strip() for x in f]
    else:
        labels_map = None

    cap = cv2.VideoCapture(input_stream)

    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 = True
    render_time = 0
    ret, frame = cap.read()
	
    frameThresh = 15
    internalFrameCounter = 0
	
    while cap.isOpened():
        if is_async_mode:
            ret, next_frame = cap.read()
        else:
            ret, frame = cap.read()
        if not ret:
            break
        initial_w = cap.get(3)
        initial_h = cap.get(4)
        # Main sync point:
        # in the truly Async mode we start the NEXT infer request, while waiting for the CURRENT to complete
        # in the regular mode we start the CURRENT request and immediately wait for it's completion
        inf_start = time.time()
        if is_async_mode:
            in_frame = cv2.resize(next_frame, (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, (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})
        if exec_net.requests[cur_request_id].wait(-1) == 0:
            inf_end = time.time()
            det_time = inf_end - inf_start

            # Parse detection results of the current request
            res = exec_net.requests[cur_request_id].outputs[out_blob]
			

            totalPeopleCount = 0
            currentCount = 0
            for obj in res[0][0]:
                image_id = obj[0]				
                label = obj[1]				
                confidence = obj[2]
                xmin = int(obj[3] * initial_w)
                ymin = int(obj[4] * initial_h)
                xmax = int(obj[5] * initial_w)
                ymax = int(obj[6] * initial_h)
                if image_id < 0 or confidence == 0:
                    continue

                if confidence > args.prob_threshold:					
                    if label == 1.0:
                        # Draw box and label\class_id
                        currentCount += 1
                        color = (min(label * 12.5, 255), min(label * 7, 255), min(label * 5, 255))
                        cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), color, 2)
                        det_label = labels_map[label] if labels_map else str(label)
                        cv2.putText(frame, det_label + ' ' + str(round(obj[2] * 100, 1)) + ' %', (xmin, ymin - 7),
                                cv2.FONT_HERSHEY_COMPLEX, 0.6, color, 1)
								
            if currentCount == 0 and lastCount == 0 and internalFrameCounter != 1:
                internalFrameCounter = 0
					
            if currentCount == 1 and lastCount == 1 and internalFrameCounter != 1:
                internalFrameCounter = 1
	
            if currentCount < lastCount and internalFrameCounter != 0:
                if internalFrameCounter < frameThresh:
                    internalFrameCounter += 1
                    currentCount = lastCount
                else:
                    internalFrameCounter = 0
						
            if currentCount > lastCount and internalFrameCounter == 0:
                elapsedtime = getTime()				
                totalCount += currentCount - lastCount
                internalFrameCounter += 1
                people = int(totalCount)

            if currentCount < lastCount and internalFrameCounter == 0:
                durationTime = int((getTime() - elapsedtime) * 1000)
                duration = durationTime
                totalDuration = duration					
	
            totalPeopleCount = currentCount
            lastCount = currentCount
				

            # Draw performance stats
            inf_time_message = "Inference time: N\A for async mode" if is_async_mode else \
                "Inference time: {:.3f} ms".format(det_time * 1000)
            render_time_message = "OpenCV rendering time: {:.3f} ms".format(render_time * 1000)
            async_mode_message = "Async mode is on. Processing request {}".format(cur_request_id) if is_async_mode else \
                "Async mode is off. Processing request {}".format(cur_request_id)
				
            results = "Total: " + str(people) + " Duration: " + str(totalDuration) + " ms " + " Count: " + str(totalPeopleCount)
            cv2.putText(frame, results, (5, 15), cv2.FONT_HERSHEY_COMPLEX, 0.7, (200, 10, 10), 1)
			
        render_start = time.time()
        cv2.imshow("Detection Results", frame)
        render_end = time.time()
        render_time = render_end - render_start

        if is_async_mode:
            cur_request_id, next_request_id = next_request_id, cur_request_id
            frame = next_frame

        key = cv2.waitKey(1)
        if key == 27:
            break
        if (9 == key):
            is_async_mode = not is_async_mode
            log.info("Switched to {} mode".format("async" if is_async_mode else "sync"))

    cv2.destroyAllWindows()
    del exec_net
    del plugin
Exemple #30
0
def infer_on_video(args):
    args.ct = float(args.ct)

    ### TODO: Initialize the Inference Engine
    # plugin = Network()
    # ### TODO: Load the network model into the IE
    # plugin.load_model(args.m, args.d, CPU_EXTENSION)
    # net_input_shape = plugin.get_input_shape()

    model_xml = args.m + '.xml'
    model_bin = args.m + ".bin"

    plugin = IEPlugin(device=args.d)
    if "CPU" in args.d:
        plugin.add_cpu_extension("lib/libcpu_extension.dylib")

    net = IENetwork(model=model_xml, weights=model_bin)
    input_blob = next(iter(net.inputs))
    exec_net = plugin.load(network=net)

    print("IR Sucessfully loaded into the Inference Engine")

    camera_width = 640
    camera_height = 480
    fps = ""
    framepos = 0
    frame_count = 0
    vidfps = 0
    skip_frame = 0
    elapsedTime = 0
    new_w = int(camera_width * m_input_size/camera_width)
    new_h = int(camera_height * m_input_size/camera_height)

    cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FPS, 30)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, camera_width)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, camera_height)

    time.sleep(1)

    while cap.isOpened():
        t1 = time.time()

        ## Uncomment only when playing video files
        #cap.set(cv2.CAP_PROP_POS_FRAMES, framepos)

        ret, image = cap.read()
        if not ret:
            break

        resized_image = cv2.resize(image, (new_w, new_h), interpolation = cv2.INTER_CUBIC)
        canvas = np.full((m_input_size, m_input_size, 3), 128)
        canvas[(m_input_size-new_h)//2:(m_input_size-new_h)//2 + new_h,(m_input_size-new_w)//2:(m_input_size-new_w)//2 + new_w,  :] = resized_image
        prepimg = canvas
        prepimg = prepimg[np.newaxis, :, :, :]     # Batch size axis add
        prepimg = prepimg.transpose((0, 3, 1, 2))  # NHWC to NCHW
        outputs = exec_net.infer(inputs={input_blob: prepimg})


        objects = []

        for output in outputs.values():
            objects = ParseYOLOV3Output(output, new_h, new_w, camera_height, camera_width, 0.4, objects)

        # Filtering overlapping boxes
        objlen = len(objects)
        for i in range(objlen):
            if (objects[i].confidence == 0.0):
                continue
            for j in range(i + 1, objlen):
                if (IntersectionOverUnion(objects[i], objects[j]) >= 0.4):
                    if objects[i].confidence < objects[j].confidence:
                        objects[i], objects[j] = objects[j], objects[i]
                    objects[j].confidence = 0.0

        # Drawing boxes
        for obj in objects:
            if obj.confidence < args.ct:
                continue
            label = obj.class_id
            confidence = obj.confidence
            #if confidence >= 0.2:
            label_text = LABELS[label] + " (" + "{:.1f}".format(confidence * 100) + "%)"
            if LABELS[label] in danger:
                mixer.music.play()
            cv2.rectangle(image, (obj.xmin, obj.ymin), (obj.xmax, obj.ymax), box_color, box_thickness)
            cv2.putText(image, label_text, (obj.xmin, obj.ymin - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, label_text_color, 1)

        cv2.putText(image, fps, (camera_width - 170, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (38, 0, 255), 1, cv2.LINE_AA)
        cv2.imshow("Result", image)

        if cv2.waitKey(1)&0xFF == ord('q'):
            break
        elapsedTime = time.time() - t1
        fps = "(Playback) {:.1f} FPS".format(1/elapsedTime)

        ## frame skip, video file only
        #skip_frame = int((vidfps - int(1/elapsedTime)) / int(1/elapsedTime))
        #framepos += skip_frame

    cv2.destroyAllWindows()
    del net
    del exec_net
    del plugin