Exemple #1
0
 def __init__(self, devid, device, model_xml, frameBuffer, results,
              camera_width, camera_height, number_of_ncs, vidfps, nPoints,
              w, h, new_w, new_h):
     self.devid = devid
     self.frameBuffer = frameBuffer
     self.model_xml = model_xml
     self.model_bin = os.path.splitext(model_xml)[0] + ".bin"
     self.camera_width = camera_width
     self.camera_height = camera_height
     self.threshold = 0.1
     self.nPoints = nPoints
     self.num_requests = 4
     self.inferred_request = [0] * self.num_requests
     self.heap_request = []
     self.inferred_cnt = 0
     self.plugin = IEPlugin(device=device)
     if "CPU" == device:
         if platform.processor() == "x86_64":
             self.plugin.add_cpu_extension("lib/libcpu_extension.so")
     self.net = IENetwork(model=self.model_xml, weights=self.model_bin)
     self.input_blob = next(iter(self.net.inputs))
     self.exec_net = self.plugin.load(network=self.net,
                                      num_requests=self.num_requests)
     self.results = results
     self.number_of_ncs = number_of_ncs
     self.predict_async_time = 250
     self.skip_frame = 0
     self.roop_frame = 0
     self.vidfps = vidfps
     self.w = w  #432
     self.h = h  #368
     self.new_w = new_w
     self.new_h = new_h
 def __init__(self, devid, frameBuffer, results, camera_width,
              camera_height, number_of_ncs, vidfps):
     self.devid = devid
     self.frameBuffer = frameBuffer
     self.model_xml = model_xml
     self.model_bin = model_bin
     self.camera_width = camera_width
     self.camera_height = camera_height
     self.m_input_size = 256
     self.threshould = 0.7
     self.num_requests = 1
     self.inferred_request = [0] * self.num_requests
     self.heap_request = []
     self.inferred_cnt = 0
     self.plugin = IEPlugin(device="MYRIAD")
     self.net = IENetwork(model=self.model_xml, weights=self.model_bin)
     self.input_blob = next(iter(self.net.inputs))
     self.out_blob = next(iter(self.net.outputs))
     self.exec_net = self.plugin.load(network=self.net,
                                      num_requests=self.num_requests)
     self.results = results
     self.number_of_ncs = number_of_ncs
     self.predict_async_time = 800
     self.skip_frame = 0
     self.roop_frame = 0
     self.vidfps = vidfps
     self.new_w = int(camera_width * self.m_input_size / camera_width)
     self.new_h = int(camera_height * self.m_input_size / camera_height)
    def __init__(self, devid, frameBuffer, results, camera_width, camera_height, number_of_ncs, vidfps,  api_results, model_name="coco_tiny_yolov3_320", input_size=320, plugin=None):
        self.devid = devid
        self.frameBuffer = frameBuffer
        # self.model_xml = "./lrmodels/tiny-YoloV3/FP16/frozen_tiny_yolo_v3.xml"
        # self.model_bin = "./lrmodels/tiny-YoloV3/FP16/frozen_tiny_yolo_v3.bin"
        self.model_xml = "./cs6220-ncs2-yolo-api/models/%s.xml" % model_name
        self.model_bin = "./cs6220-ncs2-yolo-api/models/%s.bin" % model_name
        self.camera_width = camera_width
        self.camera_height = camera_height
        self.m_input_size = input_size
        self.threshould = 0.4
        self.num_requests = 4
        self.inferred_request = [0] * self.num_requests
        self.heap_request = []
        self.inferred_cnt = 0
        self.plugin = plugin
        self.net = IENetwork(model=self.model_xml, weights=self.model_bin)
        # self.net2 = IENetwork(model=self.model_xml, weights=self.model_bin)
        self.input_blob = next(iter(self.net.inputs))
        self.exec_net = self.plugin.load(network=self.net, num_requests=self.num_requests)
        # self.exec_net2 = self.plugin.load(network=self.net2, num_requests=self.num_requests)

        self.results = results
        self.api_results = api_results
        self.number_of_ncs = number_of_ncs
        self.predict_async_time = 800
        self.skip_frame = 0
        self.roop_frame = 0
        self.vidfps = vidfps
        self.new_w = int(camera_width * min(self.m_input_size/camera_width, self.m_input_size/camera_height))
        self.new_h = int(camera_height * min(self.m_input_size/camera_width, self.m_input_size/camera_height))
 def __init__(self, devid, frameBuffer, results, camera_mode, camera_width,
              camera_height, number_of_ncs, vidfps, skpfrm):
     self.devid = devid
     self.frameBuffer = frameBuffer
     self.model_xml = "./lrmodel/MobileNetSSD/MobileNetSSD_deploy.xml"
     self.model_bin = "./lrmodel/MobileNetSSD/MobileNetSSD_deploy.bin"
     self.camera_width = camera_width
     self.camera_height = camera_height
     self.num_requests = 4
     self.inferred_request = [0] * self.num_requests
     self.heap_request = []
     self.inferred_cnt = 0
     self.plugin = IEPlugin(device="MYRIAD")
     self.net = IENetwork(model=self.model_xml, weights=self.model_bin)
     self.input_blob = next(iter(self.net.inputs))
     self.exec_net = self.plugin.load(network=self.net,
                                      num_requests=self.num_requests)
     self.results = results
     self.camera_mode = camera_mode
     self.number_of_ncs = number_of_ncs
     if self.camera_mode == 0:
         self.skip_frame = skpfrm
     else:
         self.skip_frame = 0
     self.roop_frame = 0
     self.vidfps = vidfps
 def __init__(self, devid, frameBuffer, results, camera_width, camera_height, number_of_ncs, vidfps):
     self.devid = devid
     self.frameBuffer = frameBuffer
     self.model_xml = "./lrmodels/tiny-YoloV3/FP16/frozen_tiny_yolo_v3.xml"
     self.model_bin = "./lrmodels/tiny-YoloV3/FP16/frozen_tiny_yolo_v3.bin"
     self.camera_width = camera_width
     self.camera_height = camera_height
     self.m_input_size = 416
     self.threshould = 0.4
     self.num_requests = 4
     self.inferred_request = [0] * self.num_requests
     self.heap_request = []
     self.inferred_cnt = 0
     self.plugin = IEPlugin(device="MYRIAD")
     self.net = IENetwork(model=self.model_xml, weights=self.model_bin)
     self.input_blob = next(iter(self.net.inputs))
     self.exec_net = self.plugin.load(network=self.net, num_requests=self.num_requests)
     self.results = results
     self.number_of_ncs = number_of_ncs
     self.predict_async_time = 800
     self.skip_frame = 0
     self.roop_frame = 0
     self.vidfps = vidfps
     self.new_w = int(camera_width * min(self.m_input_size/camera_width, self.m_input_size/camera_height))
     self.new_h = int(camera_height * min(self.m_input_size/camera_width, self.m_input_size/camera_height))
Exemple #6
0
    def __init__(self, config):

        #Numbers to keep track of process ID
        self.curr = 0
        self.next = 1
        self.width, self.height = config.frame_width, config.frame_height

        try:
            self.plugin = IEPlugin(device=config.default_device)
            self.net = IENetwork(model=config.network_file,
                                 weights=config.weights_file)

        except RuntimeError:
            print(
                "We're probably dealing with a mac here, do some cpu target stuff"
            )
            self.plugin = IEPlugin(device=config.fallback_device)
            self.net = IENetwork(model=config.network_file,
                                 weights=config.weights_file)
            supported_layers = self.plugin.get_supported_layers(self.net)
            not_supported_layers = [
                l for l in self.net.layers.keys() if l not in supported_layers
            ]

            if len(not_supported_layers) != 0:
                raise Exception(
                    "Some layers in the mdoel are not supported by the CPU - figure this out"
                )

        #Get sizes for image pre-processing
        self.input_blob = next(iter(self.net.inputs))
        self.output_blob = next(iter(self.net.outputs))
        self.n, self.c, self.h, self.w = self.net.inputs[self.input_blob].shape
        self.threshold = config.threshold
        if config.mode == 'classify':
            self.threshold = [config.threshold, config.non_threshold]
        self.normalize = config.normalize

        self.exec_net = self.plugin.load(network=self.net)
        #Do we need this?
        del self.net
Exemple #7
0
 def __init__(self, image_queue, results):
     self.image_queue = image_queue
     self.model_xml = "image_analysis/model/{}.xml".format(
         config.MODEL_NAME)
     self.model_bin = "image_analysis/model/{}.bin".format(
         config.MODEL_NAME)
     self.threshold = 0.4
     self.num_requests = 2
     self.inferred_request = [0] * self.num_requests
     self.heap_request = []
     self.inferred_cnt = 0
     self.plugin = IEPlugin(device=config.DEVICE)
     self.net = IENetwork(model=self.model_xml, weights=self.model_bin)
     self.input_blob = next(iter(self.net.inputs))
     self.exec_net = self.plugin.load(network=self.net,
                                      num_requests=self.num_requests)
     self.results = results
Exemple #8
0
    def __init__(self, devid, model_path, number_of_ncs):
        global g_plugin
        global g_inferred_request
        global g_heap_request
        global g_inferred_cnt
        global g_number_of_allocated_ncs

        self.devid = devid
        if number_of_ncs == 0:
            self.num_requests = 4
        elif number_of_ncs == 1:
            self.num_requests = 4
        elif number_of_ncs == 2:
            self.num_requests = 2
        elif number_of_ncs >= 3:
            self.num_requests = 1

        print("g_number_of_allocated_ncs =", g_number_of_allocated_ncs,
              "number_of_ncs =", number_of_ncs)

        if g_number_of_allocated_ncs < 1:
            self.plugin = IEPlugin(device="CPU")
            self.plugin.add_cpu_extension("./lib/libcpu_extension.so")
            self.inferred_request = [0] * self.num_requests
            self.heap_request = []
            self.inferred_cnt = 0
            g_plugin = self.plugin
            g_inferred_request = self.inferred_request
            g_heap_request = self.heap_request
            g_inferred_cnt = self.inferred_cnt
            g_number_of_allocated_ncs += 1
        else:
            self.plugin = g_plugin
            self.inferred_request = g_inferred_request
            self.heap_request = g_heap_request
            self.inferred_cnt = g_inferred_cnt

        self.model_xml = model_path + ".xml"
        self.model_bin = model_path + ".bin"
        self.net = IENetwork(model=self.model_xml, weights=self.model_bin)
        self.input_blob = next(iter(self.net.inputs))
        self.exec_net = self.plugin.load(network=self.net,
                                         num_requests=self.num_requests)
def classify_frame(inputQueue, outputQueue):
    cur_request_id = 0
    next_request_id = 1
    model_xml = "models/MobileNetSSD_deploy.xml"
    model_bin = "models/MobileNetSSD_deploy.bin"
    plugin = IEPlugin(device="MYRIAD")
    net = IENetwork(model=model_xml, weights=model_bin)
    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
    exec_net = plugin.load(network=net, num_requests=2)
    n, c, h, w = net.inputs[input_blob].shape
    del net
    while True:
        if not inputQueue.empty():
            frame = inputQueue.get()
            in_frame = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)),
                                             0.007843, (300, 300), 127.5)
            exec_net.start_async(request_id=next_request_id,
                                 inputs={input_blob: in_frame})
            if exec_net.requests[cur_request_id].wait(-1) == 0:
                detections = exec_net.requests[cur_request_id].outputs[
                    out_blob]
                data_out = []
                for i in np.arange(0, detections.shape[2]):
                    inference = []
                    confidence = detections[0, 0, i, 2]
                    if confidence > 0.2:
                        idx = int(detections[0, 0, i, 1])
                        box = detections[0, 0, i, 3:7] * np.array(
                            [frameWidth, frameHeight, frameWidth, frameHeight])
                        (startX, startY, endX, endY) = box.astype("int")
                        inference.extend(
                            (idx, confidence, startX, startY, endX, endY))
                        data_out.append(inference)
                outputQueue.put(data_out)
            cur_request_id, next_request_id = next_request_id, cur_request_id
Exemple #10
0
        model_xml = "models/train/test/openvino/mobilenet_v2_0.5_224/FP32/frozen-model.xml"

elif "GPU" == args.device or "MYRIAD" == args.device:
    if args.boost == False:
        model_xml = "models/train/test/openvino/mobilenet_v2_1.4_224/FP16/frozen-model.xml"
    else:
        model_xml = "models/train/test/openvino/mobilenet_v2_0.5_224/FP16/frozen-model.xml"

else:
    print(
        "Specify the target device to infer on; CPU, GPU, MYRIAD is acceptable."
    )
    sys.exit(0)

model_bin = os.path.splitext(model_xml)[0] + ".bin"
net = IENetwork(model=model_xml, weights=model_bin)
input_blob = next(iter(net.inputs))
exec_net = plugin.load(network=net)
inputs = net.inputs["image"]

h = inputs.shape[2]  #368
w = inputs.shape[3]  #432

threshold = 0.1
nPoints = 18

try:

    while True:
        t1 = time.perf_counter()
Exemple #11
0
formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s')

stream_hander = logging.StreamHandler()
stream_hander.setFormatter(formatter)
logger.addHandler(stream_hander)

log_max_size = 10*1024*1024
log_file_count = 20

file_handler = handlers.RotatingFileHandler(filename = "log.log", maxBytes = log_max_size, backupCount=log_file_count)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

# network 생성
logger.info("Network generation")
net = IENetwork(model = xml_path,weights = bin_path)
logger.info("Network generation Success")

# device (MYRIAD : NCS2)
logger.info("Device Init")
plugin = IEPlugin(device='MYRIAD')
logger.info("Device Init Success")

logger.info("Network Load...")
exec_net = plugin.load(net)
logger.info("Network Load Success")

def filtering_box(objects):
    # Filtering overlapping boxes
    # box를 걸러낸다
    objlen = len(objects)
def main_IE_infer():
    fps = ""
    framepos = 0
    frame_count = 0
    vidfps = 0
    skip_frame = 0
    elapsedTime = 0

    args = build_argparser().parse_args()
    #model_xml = "lrmodels/YoloV3/FP32/frozen_yolo_v3.xml" #<--- CPU
    model_xml = "/home/saket/openvino_models/tiger/n_yolov3_tiger_13.xml"  #<--- MYRIAD
    model_bin = os.path.splitext(model_xml)[0] + ".bin"
    '''
    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)
    '''
    #cap = cv2.VideoCapture("data/input/testvideo.mp4")
    #camera_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    #camera_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    #frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    #vidfps = int(cap.get(cv2.CAP_PROP_FPS))
    #print("videosFrameCount =", str(frame_count))
    #print("videosFPS =", str(vidfps))

    time.sleep(1)

    plugin = IEPlugin(device=args.device)
    if "CPU" in args.device:
        print('executing in cpu')
        plugin.add_cpu_extension("lib/libcpu_extension.so")
    net = IENetwork(model=model_xml, weights=model_bin)
    input_blob = next(iter(net.inputs))
    exec_net = plugin.load(network=net)
    counter = 0
    import glob
    images = glob.glob(
        '/media/saket/014178da-fdf2-462c-b901-d5f4dbce2e275/nn/PyTorch-YOLOv3/data/tigersamples/*.jpg'
    )[0:20]
    print('Num images', len(images))
    from PIL import Image

    while cv2.waitKey(0):
        if cv2.waitKey(1) & 0xFF == ord('q') or counter > len(images) - 1:
            break
        t1 = time.time()
        ## Uncomment only when playing video files
        #cap.set(cv2.CAP_PROP_POS_FRAMES, framepos)
        image = np.asarray(Image.open(images[counter]).convert('RGB'))

        #new_img = image
        #image=cv2.imread(images[counter])
        #image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        #image = np.asarray(Image.open(images[counter]).convert('RGB'))
        #camera_height, camera_width = image.shape[0:2]
        master = np.zeros((1920, 1920, 3), dtype='uint8')
        master[420:1500, :] = image
        image = master
        camera_height, camera_width = image.shape[0:2]
        #prepimg = image.transpose((2,0,1))
        #prepimg, _ = pad_to_square(prepimg, 0)
        # Resize
        #prepimg = resize(prepimg, m_input_size)[np.newaxis]
        #print (prepimg.shape)
        #print (camera_height,camera_width)
        #new_w = int(camera_width * min(m_input_size/camera_width, m_input_size/camera_height))
        #new_h = int(camera_height * min(m_input_size/camera_width, m_input_size/camera_height))
        #print (new_w,new_h)

        new_w, new_h = 416, 416
        print(new_w, new_h)
        resized_image = cv2.resize(image, (new_w, new_h),
                                   interpolation=cv2.INTER_CUBIC)
        print('resized', resized_image.shape)
        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

        #resized_image = cv2.resize(canvas, (m_input_size, m_input_size), interpolation = cv2.INTER_CUBIC)
        #print (resized_image.dtype)
        #prepimg = resized_image.astype('float32')
        #new_h, new_w = 416, 416
        #master = np.zeros((1920,1920,3))
        #master[420:1500,:,:]=new_img
        #prepimg = cv2.resize(master, (416, 416), interpolation = cv2.INTER_CUBIC)

        prepimg = prepimg[np.newaxis, :, :, :]  # Batch size axis add
        prepimg = prepimg.transpose((0, 3, 1, 2))  # NHWC to NCHW
        print(prepimg.shape)
        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.5, 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.5):
                    objects[j].confidence = 0

        # Drawing boxes
        image = image[420:1500, :]
        print(image.shape)
        for obj in objects:
            if obj.confidence < 0.2:
                continue
            label = obj.class_id
            confidence = obj.confidence
            if confidence > 0.2:
                label_text = LABELS[label] + " (" + "{:.1f}".format(
                    confidence * 100) + "%)"
                cv2.rectangle(image, (obj.xmin, obj.ymin - 420),
                              (obj.xmax, obj.ymax - 420), 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)
        counter += 1
        ## frame skip, video file only
        #skip_frame = int((vidfps - int(1/elapsedTime)) / int(1/elapsedTime))
        #framepos += skip_frame

    cv2.destroyAllWindows()
    '''
    
    img_detections = []  # Stores detections for each image index
    itime=[]
    for batch_i, input_imgs in enumerate(images):
        # Configure input
        input_imgs = np.asarray(Image.open(input_imgs).convert('RGB'))
        input_imgs = input_imgs.transpose((2,0,1))
        prepimg, _ = pad_to_square(input_imgs, 0)
        # Resize
        prepimg = resize(prepimg, m_input_size)[np.newaxis]
        print (prepimg.shape)
        # Get detections
        prev_time = time.time()
        detections = exec_net.infer(inputs={input_blob: prepimg})
        detections = list(detections.values())
        print (len(detections),len(detections[0]))
        detections = non_max_suppression(detections, 0.5,0.5)
        # Log progress
        current_time = time.time()
        inference_time = current_time - prev_time
        itime.append(inference_time)
        prev_time = current_time
        #flops, params = profile(model, inputs=(input_imgs, ))
        print("\t+ Batch %d, Inference Time: %.4f" % (batch_i, inference_time))
        img_detections.extend(detections)
    '''
    del net
    del exec_net
    del plugin
def main_IE_infer():
    camera_width = 1280
    camera_height = 960
    fps = ""
    framepos = 0
    frame_count = 0
    vidfps = 0
    skip_frame = 0
    elapsedTime = 0
    new_w = int(camera_width * min(m_input_size/camera_width, m_input_size/camera_height))
    new_h = int(camera_height * min(m_input_size/camera_width, m_input_size/camera_height))

    args = build_argparser().parse_args()
    #model_xml = "lrmodels/tiny-YoloV3/FP32/frozen_tiny_yolo_v3.xml" #<--- CPU
    #model_xml = "lrmodels/tiny-YoloV3/FP16/frozen_tiny_yolo_v3.xml" #<--- MYRIAD
    model_xml = "frozen_darknet_yolov3_tiny_model_signals_CPU.xml"
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

#    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)

    #cap = cv2.VideoCapture("ableitung.mp4")
    #camera_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    #camera_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    #frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    #vidfps = int(cap.get(cv2.CAP_PROP_FPS))
    #print("videosFrameCount =", str(frame_count))
    #print("videosFPS =", str(vidfps))

    img = cv2.imread("signals-dataset/yolov3/signal-1516.jpg")
    #cam = cv2.VideoCapture(0)
    #s, img = cam.read()

    time.sleep(1)

    plugin = IEPlugin(device=args.device)
    if "CPU" in args.device:
        plugin.add_cpu_extension("libcpu_extension_sse4.so")
    net = IENetwork(model=model_xml, weights=model_bin)
    input_blob = next(iter(net.inputs))
    exec_net = plugin.load(network=net)

    start_time = time.time()

    resized_image = cv2.resize(img, (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})

    print('time taken: {}'.format(time.time() - start_time))

    objects = []

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

    for object in objects:
	    print(LABELS[object.class_id], object.confidence, "%")

    # 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 < 0.02:
            continue
        label = obj.class_id
        confidence = obj.confidence
        #if confidence >= 0.2:
        label_text = LABELS[label] + " (" + "{:.1f}".format(confidence * 100) + "%)"
        cv2.rectangle(img, (obj.xmin, obj.ymin), (obj.xmax, obj.ymax), box_color, box_thickness)
        cv2.putText(img, label_text, (obj.xmin, obj.ymin - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, label_text_color, 1)

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

    cv2.destroyAllWindows()
    del net
    del exec_net
    del plugin
Exemple #14
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()
Exemple #15
0
def camThread():

    plugin = IEPlugin(device="CPU")
    plugin.add_cpu_extension("./lib/libcpu_extension.so")
    print("successful")
    model_xml = "./models/face-detection-retail-0004.xml"
    model_bin = "./models/face-detection-retail-0004.bin"
    net = IENetwork(model=model_xml, weights=model_bin)
    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
    print("test")
    Exec_net = plugin.load(network=net)
    print("successful also")

    cap = cv2.VideoCapture(0)
    cv2.namedWindow('frame')
    i = 0
    while (i == i):
        faces = []
        ret, frame = cap.read()
        if ret:
            #frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            prepimg = cv2.resize(frame, (300, 300))
            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})
            for k, v in outputs.items():
                print(str(k) + ' is key to value ' + str(v))
                print(type(v))
                print(v.flatten())
                print(type(v.flatten()))

                j = 0
                while float(v.flatten()[j + 2]) > .9:
                    image_id = v.flatten()[j]
                    label = v.flatten()[j + 1]
                    conf = v.flatten()[j + 2]
                    boxTopLeftX = v.flatten()[j + 3] * frame.shape[1]
                    boxTopLefty = v.flatten()[j + 4] * frame.shape[0]
                    boxBottomRightX = v.flatten()[j + 5] * frame.shape[1]
                    boxBottomRightY = v.flatten()[j + 6] * frame.shape[0]
                    faces.append([
                        conf, (int(boxTopLeftX), int(boxTopLefty)),
                        (int(boxBottomRightX), int(boxBottomRightY))
                    ])
                    j = j + 7

                for face in faces:
                    frame = cv2.rectangle(np.array(frame), face[1], face[2],
                                          (0, 0, 255), 2)
                    frame = cv2.putText(frame, "confidence: " + str(face[0]),
                                        (face[1][0], face[1][1] - 2),
                                        cv2.FONT_HERSHEY_SIMPLEX, .5,
                                        (0, 0, 255), 2)
                    print(str(face[0]))
            print("show image")
            cv2.imshow('frame', frame)
            #print("show frame")
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        i = i + 1

    cap.release()
    cv2.destroyAllWindows()
Exemple #16
0
def main_IE_infer():
    fps = ""
    framepos = 0
    frame_count = 0
    vidfps = 0
    skip_frame = 0
    elapsedTime = 0
    args = build_argparser().parse_args()
    result_file = args.result
    model_xml = args.model
    test_dir = args.test_dir
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    time.sleep(1)

    plugin = IEPlugin(device=args.device)
    if "CPU" in args.device:
        print('executing in cpu')
        plugin.add_cpu_extension("lib/libcpu_extension.so")
    net = IENetwork(model=model_xml, weights=model_bin)
    input_blob = next(iter(net.inputs))
    exec_net = plugin.load(network=net)
    counter = 0
    images = glob.glob(test_dir + '/*.jpg')
    print('Num images', len(images))
    from PIL import Image
    results = []
    for item in images:
        t1 = time.time()
        image = np.asarray(Image.open(item).convert('RGB'))
        master = np.zeros((1920, 1920, 3), dtype='uint8')
        master[420:1500, :] = image
        image = master
        camera_height, camera_width = image.shape[0:2]
        new_w, new_h = 416, 416
        resized_image = cv2.resize(image, (new_w, new_h),
                                   interpolation=cv2.INTER_CUBIC)
        canvas = np.full((m_input_size, m_input_size, 3), 0)
        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
        #print (prepimg.shape)
        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.5, 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.5):
                    objects[j].confidence = 0

        # Save detection
        for obj in objects:
            if obj.confidence < 0.5:
                continue
            label = obj.class_id
            confidence = obj.confidence
            if confidence > 0.5:
                xmin, ymin, xmax, ymax = obj.xmin, obj.ymin - 420, obj.xmax, obj.ymax - 420
                x, y, w, h = xmin, ymin, xmax - xmin, ymax - ymin

                anns = {
                    'image_id': item.split('/')[-1].replace('.jpg', ''),
                    'category_id': 1,
                    'segmentation': [],
                    'area': float(w * h),
                    'bbox': [x, y, w, h],
                    'score': float(confidence),
                    'iscrowd': 0
                }
                results.append(anns)

        elapsedTime = time.time() - t1
        fps = "(Playback) {:.1f} FPS".format(1 / elapsedTime)
        print(fps)
    json.dump(results, open(result_file, 'w'))
    del net
    del exec_net
    del plugin
def main_IE_infer():
    camera_width = 320
    camera_height = 240
    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)

    args = build_argparser().parse_args()
    model_xml = "lrmodels/YoloV3/FP32/frozen_yolo_v3.xml" #<--- CPU
    #model_xml = "lrmodels/YoloV3/FP16/frozen_yolo_v3.xml" #<--- MYRIAD
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    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)

    #cap = cv2.VideoCapture("data/input/testvideo.mp4")
    #camera_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    #camera_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    #frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    #vidfps = int(cap.get(cv2.CAP_PROP_FPS))
    #print("videosFrameCount =", str(frame_count))
    #print("videosFPS =", str(vidfps))

    time.sleep(1)

    plugin = IEPlugin(device=args.device)
    if "CPU" in args.device:
        plugin.add_cpu_extension("lib/libcpu_extension.so")
    net = IENetwork(model=model_xml, weights=model_bin)
    input_blob = next(iter(net.inputs))
    exec_net = plugin.load(network=net)

    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.7, 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):
                    objects[j].confidence = 0
        
        # Drawing boxes
        for obj in objects:
            if obj.confidence < 0.2:
                continue
            label = obj.class_id
            confidence = obj.confidence
            if confidence > 0.2:
                label_text = LABELS[label] + " (" + "{:.1f}".format(confidence * 100) + "%)"
                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
Exemple #18
0
class_names = [
    'Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral'
]
t_prediction = 0

image = cv2.imread('/home/pi/Desktop/Expression5/1.jpg')
img = cv2.resize(image.astype(np.float32), (44, 44))
img -= imagenet_mean
img = img.reshape((1, 44, 44, 3))
img = img.transpose((0, 3, 1, 2))

model_xml_CPU = '/home/pi/Desktop/Expression5/models/torch_model.xml'
model_bin_CPU = '/home/pi/Desktop/Expression5/models/torch_model.bin'

plugin = IEPlugin(device='MYRIAD')
net = IENetwork(model=model_xml_CPU, weights=model_bin_CPU)
net.batch_size = 1
input_blob = next(iter(net.inputs))
exec_net = plugin.load(network=net)

t = time.time()
outputs = exec_net.infer(inputs={input_blob: img})
t_prediction = time.time() - t

class_name = class_names[np.argmax(outputs['617'])]
probs = outputs['617'][0, np.argmax(outputs['617'])]

print('Prediction time: %.2f' % t_prediction + ', Speed : %.2fFPS' %
      (1 / t_prediction))

print("Class: " + class_name + ", probability: %.4f" % probs)
Exemple #19
0
def main_IE_infer():
    camera_width = 320
    camera_height = 240
    m_input_size = 513
    fps = ""
    framepos = 0
    frame_count = 0
    vidfps = 0
    skip_frame = 0
    elapsedTime = 0

    args = build_argparser().parse_args()
    #model_xml = "lrmodels/PascalVOC/FP32/frozen_inference_graph.xml" #<--- CPU
    model_xml = "lrmodels/PascalVOC/FP16/frozen_inference_graph.xml"  #<--- MYRIAD
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    seg_image = Image.open("data/input/009649.png")
    palette = seg_image.getpalette()  # Get a color palette

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

    #cap = cv2.VideoCapture("data/input/testvideo.mp4")
    #camera_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    #camera_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    #frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    #vidfps = int(cap.get(cv2.CAP_PROP_FPS))
    #print("videosFrameCount =", str(frame_count))
    #print("videosFPS =", str(vidfps))

    time.sleep(1)

    plugin = IEPlugin(device=args.device, plugin_dirs=args.plugin_dir)
    if "CPU" in args.device:
        plugin.add_cpu_extension("lib/libcpu_extension.so")
    if args.performance:
        plugin.set_config({"PERF_COUNT": "YES"})
    # Read IR
    net = IENetwork(model=model_xml, weights=model_bin)
    input_blob = next(iter(net.inputs))
    exec_net = plugin.load(network=net)

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

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

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

        ratio = 1.0 * m_input_size / max(image.shape[0], image.shape[1])
        shrink_size = (int(ratio * image.shape[1]),
                       int(ratio * image.shape[0]))
        image = cv2.resize(image, shrink_size, interpolation=cv2.INTER_CUBIC)

        prepimg = _pre._pre_process(image)
        prepimg = prepimg.transpose((0, 3, 1, 2))  #NHWC to NCHW
        res = exec_net.infer(inputs={input_blob: prepimg})
        result = _post._post_process(res["ArgMax/Squeeze"], image)[0]

        outputimg = Image.fromarray(np.uint8(result), mode="P")
        outputimg.putpalette(palette)
        outputimg = outputimg.convert("RGB")
        outputimg = np.asarray(outputimg)
        outputimg = cv2.cvtColor(outputimg, cv2.COLOR_RGB2BGR)
        outputimg = cv2.addWeighted(image, 1.0, outputimg, 0.9, 0)
        outputimg = cv2.resize(outputimg, (camera_width, camera_height))

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

        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
Exemple #20
0
def movidius(frameCount):
    global outputFrame, lock, _reset
    ct = CentroidTracker()
    args = argsparser()
    #ใช้ Log แทน Print
    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    #Funtion Select
    cam_select, cam_rtsp, min_conf_threshold, notify, detect_list, resW, resH = select_option(
    )
    # warmup
    if cam_select == 0:
        vs = cv2.VideoCapture(0)
    else:
        vs = cv2.VideoCapture(cam_rtsp)
    imW, imH = int(resW), int(resH)
    vs.set(3, imW)
    vs.set(4, imH)
    time.sleep(0.2)
    ##################
    #โหลดโมดูล
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"
    log.info("Initializing plugin for {} device...".format(args.device))
    plugin = IEPlugin(device=args.device, plugin_dirs=args.plugin_dir)
    if args.cpu_extension and 'CPU' in args.device:
        plugin.add_cpu_extension('args.cpu_extension')
    # Read IR
    log.info("Reading IR...")
    net = IENetwork(model=model_xml, weights=model_bin)
    if plugin.device == "CPU":
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [
            l for l in net.layers.keys() if l not in supported_layers
        ]
        if len(not_supported_layers) != 0:
            log.error(
                "Following layers are not supported by the plugin for specified device {}:\n {}"
                .format(plugin.device, ', '.join(not_supported_layers)))
            log.error(
                "Please try to specify cpu extensions library path in demo's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)
    assert len(
        net.inputs.keys()) == 1, "Demo supports only single input topologies"
    assert len(net.outputs) == 1, "Demo supports only single output topologies"
    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
    log.info("Loading IR to the plugin...")
    exec_net = plugin.load(network=net, num_requests=2)
    # Read and pre-process input image
    n, c, h, w = net.inputs[input_blob].shape
    del net
    if args.labels:
        with open(args.labels, 'r') as f:
            labels_map = [x.strip() for x in f]
    else:
        labels_map = None

    cur_request_id = 0
    next_request_id = 1

    log.info("Starting inference in async mode...")
    log.info("To switch between sync and async modes press Tab button")
    log.info("To stop the demo execution press Esc button")
    is_async_mode = False
    ##################
    # Initialize frame rate calculation
    frame_rate_calc = 1
    freq = cv2.getTickFrequency()
    font = cv2.FONT_HERSHEY_SIMPLEX
    ##########################################
    objects_first = 0
    t1_image = time.time()
    t2_image = time.time()
    ##########################################
    log.info("Detect On")
    while True:
        t1 = cv2.getTickCount()
        #ชื่อคลาสและไอดีของคลาส
        ckname = ''
        object_name = None
        ret, frame_q = vs.read()

        if is_async_mode:
            in_frame = cv2.resize(frame_q, (w, h))
            in_frame = in_frame.transpose(
                (2, 0, 1))  # Change data layout from HWC to CHW
            in_frame = in_frame.reshape((n, c, h, w))
            exec_net.start_async(request_id=next_request_id,
                                 inputs={input_blob: in_frame})
        else:
            in_frame = cv2.resize(frame_q, (w, h))
            in_frame = in_frame.transpose(
                (2, 0, 1))  # Change data layout from HWC to CHW
            in_frame = in_frame.reshape((n, c, h, w))
            exec_net.start_async(request_id=cur_request_id,
                                 inputs={input_blob: in_frame})
        rects = []
        if exec_net.requests[cur_request_id].wait(-1) == 0:
            # Parse detection results of the current request
            res = exec_net.requests[cur_request_id].outputs[out_blob]
            for obj in res[0][0]:
                # Draw only objects when probability more than specified threshold
                if obj[2] > min_conf_threshold:
                    # Draw label
                    class_id = int(obj[1])
                    object_name = labels_map[class_id] if labels_map else str(
                        class_id)
                    if object_name in detect_list:
                        ckname = ckname + object_name + ','
                        xmin = int(obj[3] * imW)
                        ymin = int(obj[4] * imH)
                        xmax = int(obj[5] * imW)
                        ymax = int(obj[6] * imH)
                        cv2.rectangle(frame_q, (xmin, ymin), (xmax, ymax),
                                      (10, 255, 0), 2)
                        label = '%s: %s%%' % (object_name,
                                              int(round(obj[2] * 100, 1)))
                        #print(label)
                        labelSize, baseLine = cv2.getTextSize(
                            label, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 2)
                        label_ymin = max(ymin, labelSize[1] + 10)
                        color = color_name(object_name)
                        cv2.rectangle(
                            frame_q, (xmin, label_ymin - labelSize[1] - 10),
                            (xmin + labelSize[0], label_ymin + baseLine - 10),
                            color, cv2.FILLED)
                        cv2.putText(frame_q, label, (xmin, label_ymin - 7),
                                    cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0),
                                    2)
                        x = np.array([xmin, ymin, xmax, ymax])
                        # print(x)
                        rects.append(x.astype("int"))
        cv2.putText(frame_q, "FPS: {0:.2f}".format(frame_rate_calc), (30, 50),
                    font, 1, (255, 255, 0), 2, cv2.LINE_AA)

        #Funtion display_show
        t1_image = time.time()
        frame_q, objects_first, t2_image = display_show(
            rects, t1_image, t2_image, ckname, frame_q, objects_first, notify,
            ct)

        t2 = cv2.getTickCount()
        time1 = (t2 - t1) / freq
        frame_rate_calc = 1 / time1

        if is_async_mode:
            cur_request_id, next_request_id = next_request_id, cur_request_id
        with lock:
            outputFrame = frame_q.copy()
        if _reset == 1:
            sys.exit()
    vs.stop()
def camThread():

    plugin = IEPlugin(device="CPU")
    plugin.add_cpu_extension("./lib/libcpu_extension.so")
    print("successful")
    model_xml = "./models/face-detection-retail-0004.xml"
    model_bin = "./models/face-detection-retail-0004.bin"
    net = IENetwork(model=model_xml, weights=model_bin)
    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
    print("test")
    Exec_net = plugin.load(network=net)
    print("successful also")

    emotion_model_xml = "./models/emotions-recognition-retail-0003.xml"
    emotion_model_bin = "./models/emotions-recognition-retail-0003.bin"
    emotionNet = IENetwork(model=emotion_model_xml, weights=emotion_model_bin)
    emotion_input_blob = next(iter(net.inputs))
    emotion_exec_net = plugin.load(network=emotionNet)
    print("Successfully loaded emotion model")

    cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
    cv2.namedWindow('frame')
    i = 0
    start = datetime.now()
    fps = 0
    while (i <= i):
        faces = []
        ret, frame = cap.read()
        if ret:
            #frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            prepimg = cv2.resize(frame, (300, 300))
            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})
            for k, v in outputs.items():
                #print(str(k) + ' is key to value ' + str(v))
                #print(type(v))
                #print(v.flatten())
                #print(type(v.flatten()))

                j = 0
                while float(v.flatten()[j + 2]) > .9:
                    image_id = v.flatten()[j]
                    label = v.flatten()[j + 1]
                    conf = v.flatten()[j + 2]
                    boxTopLeftX = v.flatten()[j + 3] * frame.shape[1]
                    boxTopLefty = v.flatten()[j + 4] * frame.shape[0]
                    boxBottomRightX = v.flatten()[j + 5] * frame.shape[1]
                    boxBottomRightY = v.flatten()[j + 6] * frame.shape[0]
                    faces.append([
                        conf, (int(boxTopLeftX), int(boxTopLefty)),
                        (int(boxBottomRightX), int(boxBottomRightY))
                    ])
                    j = j + 7

                for face in faces:
                    prepimg = frame[face[1][1]:face[2][1],
                                    face[1][0]:face[2][0]]
                    #prepimg = cv2.cvtColor(prepimg, cv2.COLOR_RGB2BGR)
                    prepimg = cv2.resize(frame, (64, 64))
                    prepimg = prepimg[
                        np.newaxis, :, :, :]  # Batch size axis add
                    prepimg = prepimg.transpose((0, 3, 1, 2))  # NHWC to NCHW
                    emotion_outputs = emotion_exec_net.infer(
                        inputs={emotion_input_blob: prepimg})
                    for keys, values in emotion_outputs.items():
                        print(type(values))
                        print("emotion key is " + str(keys) +
                              " and value is " + str(values.flatten()))
                        print(int(np.argmax(values.flatten())))
                        emotion = LABELS[int(np.argmax(values.flatten()))]
                        prob = float(values.flatten()[int(
                            np.argmax(values.flatten()))]) * 100
                        print(emotion)
                    frame = cv2.rectangle(np.array(frame), face[1], face[2],
                                          (0, 0, 255), 1)
                    if prob > 60:
                        frame = cv2.putText(
                            frame,
                            emotion + "  confidence: " + "%.2f" % prob + "%",
                            (face[1][0], face[1][1] - 2),
                            cv2.FONT_HERSHEY_SIMPLEX, .5, (0, 0, 255), 1,
                            cv2.LINE_AA)
                    #print(str(face[0]))

            frame = cv2.putText(frame, "FPS: " + "%.2f" % fps, (0, 15),
                                cv2.FONT_HERSHEY_SIMPLEX, .5, (0, 0, 255), 1)

            print("show image")
            cv2.imshow('frame', frame)
            #print("show frame")
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        i = i + 1
        if i > 30:
            fps = i / int((datetime.now() - start).total_seconds())
            print(fps)

    cap.release()
    cv2.destroyAllWindows()
def camThread(device, number_of_camera, camera_width, camera_height,
              number_of_ncs, video, precision):
    if device == 'CPU':
        plugin = IEPlugin(device="CPU")
        if platform.system() == "Linux":
            plugin.add_cpu_extension('./lib/libcpu_extension.so')
        elif platform.system() == "Windows":
            print(os.getcwd())
            plugin.add_cpu_extension(os.getcwd() + '\\lib\\cpu_extension.dll')
        print("successfully loaded CPU plugin")
        if precision == "FP32":
            model_xml = "./models/FP32/face-detection-retail-0004.xml"
            model_bin = "./models/FP32/face-detection-retail-0004.bin"
            emotion_model_xml = "./models/FP32/emotions-recognition-retail-0003.xml"
            emotion_model_bin = "./models/FP32/emotions-recognition-retail-0003.bin"
        elif precision == "INT8":
            model_xml = "./models/INT8/face-detection-retail-0004.xml"
            model_bin = "./models/INT8/face-detection-retail-0004.bin"
            emotion_model_xml = "./models/INT8/emotions-recognition-retail-0003.xml"
            emotion_model_bin = "./models/INT8/emotions-recognition-retail-0003.bin"
    if device == "MYRIAD":
        plugin = IEPlugin(device="MYRIAD")
        print("Successfully loaded MYRIAD plugin")
        model_xml = "./models/FP16/face-detection-retail-0004.xml"
        model_bin = "./models/FP16/face-detection-retail-0004.bin"
        emotion_model_xml = "./models/FP16/emotions-recognition-retail-0003.xml"
        emotion_model_bin = "./models/FP16/emotions-recognition-retail-0003.bin"

    net = IENetwork(model=model_xml, weights=model_bin)
    input_blob = next(iter(net.inputs))
    output_blob = next(iter(net.outputs))
    Exec_net = plugin.load(network=net)
    print("successfully loaded face model")
    emotionNet = IENetwork(model=emotion_model_xml, weights=emotion_model_bin)
    emotion_input_blob = next(iter(emotionNet.inputs))
    emotion_output_blob = next(iter(emotionNet.outputs))
    emotion_exec_net = plugin.load(network=emotionNet)
    print("Successfully loaded emotion model")

    if video == "":
        cap = cv2.VideoCapture(number_of_camera)
        cap.set(cv2.CAP_PROP_FRAME_WIDTH, camera_width)
        cap.set(cv2.CAP_PROP_FRAME_HEIGHT, camera_height)
    else:
        cap = cv2.VideoCapture(video)

    cv2.namedWindow('frame')
    i = 0
    start = datetime.now()
    fps = 0
    while (cap.isOpened()):
        faces = []
        ret, frame = cap.read()
        if ret:
            # frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            prepimg = cv2.resize(frame, (300, 300))
            prepimg = prepimg[np.newaxis, :, :, :]  # Batch size axis add
            prepimg = prepimg.transpose((0, 3, 1, 2))  # NHWC to NCHW
            infer_request_handle = Exec_net.start_async(
                request_id=0, inputs={input_blob: prepimg})
            infer_status = infer_request_handle.wait()
            outputs = infer_request_handle.outputs[output_blob]
            outputs = outputs.flatten()
            j = 0
            while float(outputs[j + 2] > .9):
                image_id = outputs[j]
                label = outputs[j + 1]
                conf = outputs[j + 2]
                boxTopLeftX = outputs[j + 3] * frame.shape[1]
                boxTopLeftY = outputs[j + 4] * frame.shape[0]
                boxBottomRightX = outputs[j + 5] * frame.shape[1]
                boxBottomRightY = outputs[j + 6] * frame.shape[0]
                faces.append([
                    conf, (int(boxTopLeftX), int(boxTopLeftY)),
                    (int(boxBottomRightX), int(boxBottomRightY))
                ])
                j = j + 7
            #Loop through faces and async infer, adding each handler to a dict. Then go through dict and draw on frame.
            emotion_results = []
            request_handler = None
            for index, face in enumerate(faces):
                prepimg = frame[face[1][1]:face[2][1], face[1][0]:face[2][0]]
                prepimg = cv2.resize(frame, (64, 64))
                prepimg = prepimg[np.newaxis, :, :, :]  # Batch size axis add
                prepimg = prepimg.transpose((0, 3, 1, 2))  # NHWC to NCHW
                if index > 0:
                    start_time = datetime.now()
                    print("Waiting")
                    emotion_outputs_status = request_handler[0].wait()
                    end_time = datetime.now()
                    print(end_time - start_time)
                    emotion_results.append(
                        (request_handler[0].outputs[emotion_output_blob].
                         flatten(), request_handler[1]))
                request_handler = (emotion_exec_net.start_async(
                    request_id=0, inputs={emotion_input_blob: prepimg}), face)
                print("started async infer")
            if request_handler:
                emotion_outputs_status = request_handler[0].wait()
                emotion_results.append(
                    emotion_results.append(
                        (request_handler[0].outputs[emotion_output_blob].
                         flatten(), request_handler[1])))
            for emotion_result in emotion_results:
                if emotion_result != None:
                    emotion_probs = emotion_result[0]
                    face = emotion_result[1]
                    emotion = LABELS[int(np.argmax(emotion_probs))]
                    prob = float(emotion_probs[int(
                        np.argmax(emotion_probs))]) * 100
                    print(emotion)
                    neutral = emotion_probs[0]
                    happy = emotion_probs[1]
                    sad = emotion_probs[2]
                    surprise = emotion_probs[3]
                    anger = emotion_probs[4]
                    prob_scale_x = face[1][0] - 100

                    frame = cv2.rectangle(np.array(frame), face[1], face[2],
                                          (0, 0, 255), 1)
                    frame = cv2.rectangle(np.array(frame),
                                          (face[1][0] - 100, face[1][1]),
                                          (face[1][0], face[1][1] + 50),
                                          (0, 0, 255), cv2.FILLED)

                    frame = cv2.rectangle(
                        np.array(frame), (prob_scale_x, face[1][1]),
                        (face[1][0] + int(neutral * 100 - 100),
                         face[1][1] + 10), (0, 255, 0), cv2.FILLED)
                    frame = cv2.putText(frame, "Neutral",
                                        (prob_scale_x + 60, face[1][1] + 7),
                                        cv2.FONT_HERSHEY_SIMPLEX, .3,
                                        (0, 0, 0), 1)

                    frame = cv2.rectangle(
                        np.array(frame), (prob_scale_x, face[1][1] + 10),
                        (face[1][0] + int(happy * 100 - 100), face[1][1] + 20),
                        (0, 255, 0), cv2.FILLED)
                    frame = cv2.putText(frame, "Happy",
                                        (prob_scale_x + 60, face[1][1] + 17),
                                        cv2.FONT_HERSHEY_SIMPLEX, .3,
                                        (0, 0, 0), 1)

                    frame = cv2.rectangle(
                        np.array(frame), (prob_scale_x, face[1][1] + 20),
                        (face[1][0] + int(sad * 100 - 100), face[1][1] + 30),
                        (0, 255, 0), cv2.FILLED)
                    frame = cv2.putText(frame, "Sad",
                                        (prob_scale_x + 60, face[1][1] + 27),
                                        cv2.FONT_HERSHEY_SIMPLEX, .3,
                                        (0, 0, 0), 1)

                    frame = cv2.rectangle(
                        np.array(frame), (prob_scale_x, face[1][1] + 30),
                        (face[1][0] + int(surprise * 100 - 100),
                         face[1][1] + 40), (0, 255, 0), cv2.FILLED)
                    frame = cv2.putText(frame, "Surprise",
                                        (prob_scale_x + 60, face[1][1] + 37),
                                        cv2.FONT_HERSHEY_SIMPLEX, .3,
                                        (0, 0, 0), 1)

                    frame = cv2.rectangle(
                        np.array(frame), (prob_scale_x, face[1][1] + 40),
                        (face[1][0] + int(anger * 100 - 100), face[1][1] + 50),
                        (0, 255, 0), cv2.FILLED)
                    frame = cv2.putText(frame, "Anger",
                                        (prob_scale_x + 60, face[1][1] + 47),
                                        cv2.FONT_HERSHEY_SIMPLEX, .3,
                                        (0, 0, 0), 1)

                    if prob > 60:
                        frame = cv2.putText(
                            frame,
                            emotion + "  confidence: " + "%.2f" % prob + "%",
                            (face[1][0], face[1][1] - 2),
                            cv2.FONT_HERSHEY_SIMPLEX, .5, (0, 0, 255), 1,
                            cv2.LINE_AA)

                        # print(str(face[0]))

            frame = cv2.putText(frame, "FPS: " + "%.2f" % fps, (0, 15),
                                cv2.FONT_HERSHEY_SIMPLEX, .5, (0, 0, 255), 1)

            print("show image")
            cv2.imshow('frame', frame)
        else:
            break
            # print("show frame")
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        i = i + 1
        if int((datetime.now() - start).total_seconds()) > 0:
            fps = i / int((datetime.now() - start).total_seconds())
            print(fps)
    print("Total time: " + str(datetime.now() - start))
    cap.release()
    cv2.destroyAllWindows()
Exemple #23
0
def main_IE_infer():
    fps = ""
    framepos = 0
    frame_count = 0
    vidfps = 0
    skip_frame = 0
    elapsedTime = 0
    args = build_argparser().parse_args()
    #model_xml = "lrmodels/tiny-YoloV3/FP32/frozen_tiny_yolo_v3.xml" #<--- CPU
    model_xml = "/home/saket/openvino_models/tiger/km_adam_yolov3_tinytiger_ckpt_9.xml" #<--- MYRIAD
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    '''
    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)
    '''
    #cap = cv2.VideoCapture("data/input/testvideo.mp4")
    #camera_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    #camera_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    #frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    #vidfps = int(cap.get(cv2.CAP_PROP_FPS))
    #print("videosFrameCount =", str(frame_count))
    #print("videosFPS =", str(vidfps))

    time.sleep(1)

    plugin = IEPlugin(device=args.device)
    if "CPU" in args.device:
        plugin.add_cpu_extension("lib/libcpu_extension.so")
    net = IENetwork(model=model_xml, weights=model_bin)
    input_blob = next(iter(net.inputs))
    exec_net = plugin.load(network=net)
    import glob
    images = glob.glob('/media/saket/014178da-fdf2-462c-b901-d5f4dbce2e275/nn/PyTorch-YOLOv3/data/tigersamples/*.jpg')
    print ('Num images',len(images))
    counter = 0
    while cv2.waitKey(0):
        if cv2.waitKey(1)&0xFF == ord('q') or  counter>len(images)-1:
            break
        t1 = time.time()
        image =  np.asarray(Image.open(images[counter]).convert('RGB'))
        master = np.zeros((1920, 1920, 3),dtype='uint8')
        master[420:1500,:]=image
        image = master
        camera_height, camera_width = image.shape[0:2]
        new_w,new_h = 416,416
        resized_image = cv2.resize(image, (new_w, new_h), interpolation = cv2.INTER_CUBIC)
        canvas = np.full((m_input_size, m_input_size, 3), 0)
        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
        #print (prepimg.shape)
        outputs = exec_net.infer(inputs={input_blob: prepimg})
        objects = []

        #output_name = detector/yolo-v3-tiny/Conv_12/BiasAdd/YoloRegion
        #output_name = detector/yolo-v3-tiny/Conv_9/BiasAdd/YoloRegion

        objects = []
        for output in outputs.values():
            objects = ParseYOLOV3Output(output,new_h, new_w, camera_height, camera_width, 0.5, 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.5):
                    if objects[i].confidence < objects[j].confidence:
                        objects[i], objects[j] = objects[j], objects[i]
                    objects[j].confidence = 0.0
        
        # Drawing boxes
        image = image[420:1500,:]
        print (image.shape)
        for obj in objects:
            if obj.confidence < 0.5:
                continue
            label = obj.class_id
            confidence = obj.confidence
            if confidence > 0.5:
                label_text = LABELS[label] + " (" + "{:.1f}".format(confidence * 100) + "%)"
                cv2.rectangle(image, (obj.xmin, obj.ymin-420), (obj.xmax, obj.ymax-420), 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)
        
        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
        counter= counter+1

    cv2.destroyAllWindows()
    del net
    del exec_net
    del plugin