Ejemplo n.º 1
0
def test_init_ie_core_no_cfg():
    ie = IECore()
    assert isinstance(ie, IECore)
def main():
    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    args = build_argparser().parse_args()

    log.info("Initializing Inference Engine")
    ie = IECore()
    version = ie.get_versions(args.device)[args.device]
    version_str = "{}.{}.{}".format(version.major, version.minor,
                                    version.build_number)
    log.info("Plugin version is {}".format(version_str))

    # read IR
    model_xml = args.model
    model_bin = model_xml.with_suffix(".bin")
    log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    ie_encoder = ie.read_network(model=model_xml, weights=model_bin)

    # check input and output names
    input_shapes = {
        k: v.input_data.shape
        for k, v in ie_encoder.input_info.items()
    }
    input_names = list(ie_encoder.input_info.keys())
    output_names = list(ie_encoder.outputs.keys())

    assert "input" in input_names, "'input' is not presented in model"
    assert "output" in output_names, "'output' is not presented in model"
    state_inp_names = [n for n in input_names if "state" in n]
    state_param_num = sum(np.prod(input_shapes[n]) for n in state_inp_names)
    log.info("state_param_num = {} ({:.1f}Mb)".format(state_param_num,
                                                      state_param_num * 4e-6))

    # load model to the device
    log.info("Loading model to the {}".format(args.device))
    ie_encoder_exec = ie.load_network(network=ie_encoder,
                                      device_name=args.device)

    sample_inp = wav_read(args.input)

    input_size = input_shapes["input"][1]
    res = None

    samples_out = []
    samples_times = []
    while sample_inp is not None and sample_inp.shape[0] > 0:
        if sample_inp.shape[0] > input_size:
            input = sample_inp[:input_size]
            sample_inp = sample_inp[input_size:]
        else:
            input = np.pad(sample_inp,
                           ((0, input_size - sample_inp.shape[0]), ),
                           mode='constant')
            sample_inp = None

        #forms input
        inputs = {"input": input[None, :]}

        #add states to input
        for n in state_inp_names:
            if res:
                inputs[n] = res[n.replace('inp', 'out')].buffer
            else:
                #on the first iteration fill states by zeros
                inputs[n] = np.zeros(input_shapes[n], dtype=np.float32)

        t0 = time.perf_counter()
        # Set inputs manually through InferRequest functionality to speedup
        infer_request_ptr = ie_encoder_exec.requests[0]
        for n, data in inputs.items():
            info_ptr = ie_encoder.input_info[n]
            blob = Blob(info_ptr.tensor_desc, data)
            infer_request_ptr.set_blob(n, blob, info_ptr.preprocess_info)

        # infer by IE
        infer_request_ptr.infer()
        res = infer_request_ptr.output_blobs

        t1 = time.perf_counter()

        samples_times.append(t1 - t0)
        samples_out.append(res["output"].buffer.squeeze(0))

    log.info("Sequence of length {:0.2f}s is processed by {:0.2f}s".format(
        sum(s.shape[0] for s in samples_out) / 16000,
        sum(samples_times),
    ))
    sample_out = np.concatenate(samples_out, 0)
    wav_write(args.output, sample_out)
Ejemplo n.º 3
0
try:
    from openvino.inference_engine import IECore, IENetwork
    ie = IECore()
except:
    ov_path = '/opt/intel/computer_vision_sdk_2018.5.455/python/python3.6/ubuntu16'

    try:
        import sys
        sys.path.append(ov_path)
        from openvino.inference_engine import IECore, IENetwork

    except:
        pass


def sync_inference(
    net, image
):  # runs syncronously (waits until one inference is done to start the next one)
    net.batch_size = image.shape[0]
    exec_net = ie.load_network(network=net, device_name="CPU")
    input_blob = next(iter(exec_net.inputs))
    return exec_net.infer({input_blob: image})


def async_inference(exec_net, image, request_id=0):
    input_blob = next(iter(exec_net.inputs))
    exec_net.start_async(request_id, inputs={input_blob: image})
    return exec_net


def get_async_output(exec_net, request_id=0):
Ejemplo n.º 4
0
import firebase_admin
from firebase_admin import firestore
from firebase_admin import credentials
import time

#Detection model architecture
det_model = r"C:\Users\LENOVO\Desktop\Detect&Recognize\intel\face-detection-0202\FP16\face-detection-0202.xml"
det_weights = os.path.splitext(det_model)[0] + '.bin'

#Siamese-recognition model
# recogModel=r"C:\Users\LENOVO\Downloads\siamese_model\siamese_model.xml"
recogModel = r"E:\FINAL-YEAR-PROJECT\siamese_networks\intel-savedModel\faceNetOneshotJune.xml"
recogWeights = os.path.splitext(recogModel)[0] + '.bin'

#Instantiate the plugin
plugin = IECore()
'''
Prepare the detection model
'''
detPlugin = plugin
detNet = detPlugin.read_network(model=det_model, weights=det_weights)
detExecNet = detPlugin.load_network(network=detNet, device_name="MYRIAD")
det_input_blob = list(detNet.input_info.keys())[0]
det_output_blob = next(iter(detNet.outputs))
db, dc, dh, dw = detNet.input_info[det_input_blob].input_data.shape
'''
Prepare the recognition model
'''
recogPlugin = plugin
recogNetwork = recogPlugin.read_network(model=recogModel, weights=recogWeights)
recogExecNet = recogPlugin.load_network(network=recogNetwork,
Ejemplo n.º 5
0
    def load_model(self):
        self.plugin = IECore()

        self.exec_network = self.plugin.load_network(self.model, self.device)

        return
Ejemplo n.º 6
0
def main():
    args = build_argparser().parse_args()

    if not is_correct_args(args):
        return 1

    log.info('OpenVINO Inference Engine')
    log.info('\tbuild: {}'.format(get_version()))
    ie = IECore()

    if args.model_melgan is not None:
        vocoder = MelGANIE(args.model_melgan, ie, device=args.device)
    else:
        vocoder = WaveRNNIE(args.model_upsample,
                            args.model_rnn,
                            ie,
                            device=args.device,
                            upsampler_width=args.upsampler_width)

    forward_tacotron = ForwardTacotronIE(args.model_duration,
                                         args.model_forward,
                                         ie,
                                         args.device,
                                         verbose=False)

    audio_res = np.array([], dtype=np.int16)

    speaker_emb = None
    if forward_tacotron.has_speaker_embeddings():
        if args.speaker_id == -1:
            interactive_parameter = init_parameters_interactive(args)
            args.alpha = 1.0 / interactive_parameter["speed"]
            speaker_emb = forward_tacotron.get_pca_speaker_embedding(
                interactive_parameter["gender"],
                interactive_parameter["style"])
        else:
            speaker_emb = forward_tacotron.get_speaker_embeddings()[
                args.speaker_id, :]

    len_th = 512

    time_forward = 0
    time_wavernn = 0

    time_s_all = perf_counter()
    with open(args.input, 'r') as f:
        count = 0
        for line in f:
            count += 1
            line = line.rstrip()
            log.info("Process line {0} with length {1}.".format(
                count, len(line)))

            if len(line) > len_th:
                texts = []
                prev_begin = 0
                delimiters = '.!?;:'
                for i, c in enumerate(line):
                    if (c in delimiters
                            and i - prev_begin > len_th) or i == len(line) - 1:
                        texts.append(line[prev_begin:i + 1])
                        prev_begin = i + 1
            else:
                texts = [line]

            for text in tqdm(texts):
                time_s = perf_counter()
                mel = forward_tacotron.forward(text,
                                               alpha=args.alpha,
                                               speaker_emb=speaker_emb)
                time_forward += perf_counter() - time_s

                time_s = perf_counter()
                audio = vocoder.forward(mel)
                time_wavernn += perf_counter() - time_s

                audio_res = np.append(audio_res, audio)

    total_latency = (perf_counter() - time_s_all) * 1e3
    log.info("Metrics report:")
    log.info("\tLatency: {:.1f} ms".format(total_latency))
    log.debug("\tVocoder time: {:.1f} ms".format(time_wavernn * 1e3))
    log.debug("\tForwardTacotronTime: {:.1f} ms".format(time_forward * 1e3))

    save_wav(audio_res, args.out)
class ov_model:
    ie = IECore()
    available_devices = ['CPU', 'GPU', 'MYRIAD', 'HDDL', 'FPGA', 'GNA']
    available_vdevices = ['HETERO', 'MULTI']
    model_categories = ['public', 'intel']
    model_precision = 'FP16'

    def __init__(self, device='CPU', model=None, **kwargs):
        self.ie = ov_model.ie  # not deep copy, use given object
        self.net = None
        self.exenet = None
        self.iblob = []
        self.oblob = []
        self.modelDir = '.'
        self.setDevice(device)
        self.postprocess_params = kwargs['kwargs']
        self.labels = None
        if not model is None:
            self.loadModel(model)

    def __del__(self):
        del self.exenet
        del self.net

    def checkDevice(self, device):
        """
        Check if the device descriptor is acceptable by IE.  
        Args:  
          device (string) : Inference device descriptor for IE
        Return:
          True or False : True = descriptor is acceptable
        """
        if ':' in device:
            vdevice, devices = device.split(':')
            if not vdevice in ov_model.available_vdevices:
                return False
            devices = devices.split(',')
            for device in devices:
                if self.checkDevice(device) == False:
                    return False
        elif not device in ov_model.available_devices:
            return False
        return True

    def setDevice(self, device='CPU'):
        """
        Set inference device.  
        Args:  
          device (string) : OpenVINO Inference engine acceptable device descriptor. ('CPU', 'GPU', 'MYRIAD', 'MULTI:CPU,GPU', ...)
        Return:
          None
        """
        if self.checkDevice(device) == False:
            raise Exception('Not supported device ({})'.format(device))
        self.device = device
        if not self.net is None:
            del self.exenet
            self.exenet = self.ie.load_network(self.net,
                                               self.device,
                                               num_requests=4)

    def getInterfaceInfo(self):
        """
        Obtain information of input and output blob of the model
        Args:
         None
        Returns:
         iblob/oblob : [ [ 'name':name0, 'shape':shape0, 'precision':precision0 ], ...]
        """
        if self.net is None or self.exenet is None:
            return
        self.iblob = [{
            'name': bname,
            'shape': self.net.input_info[bname].tensor_desc.dims,
            'precision': self.net.input_info[bname].precision
        } for bname in self.net.input_info]
        self.oblob = [{
            'name': bname,
            'shape': self.net.outputs[bname].shape,
            'precision': self.net.outputs[bname].precision
        } for bname in self.net.outputs]

    def loadLabel(self, labelFile):
        """
        Load label file.  
        Args:  
          labelFile (string) : Label file name
        Return:
          None
        """
        self.labels = None
        if os.path.isfile(labelFile):
            with open(labelFile, 'rt') as f:
                self.labels = [line.rstrip('\n') for line in f]

    def loadModel(self, modelFile):
        """
        Read IR model and load the model to IE.  
        This function will search the model location under `./public` and `./intel`.  
        Args:  
          model : IR model file name without path ('mmmmm.xml')
        Return:  
          None
        """
        self.net = None
        self.exenet = None
        base, ext = os.path.splitext(modelFile)
        for modelcat in ov_model.model_categories:
            model_dir = os.path.join(self.modelDir, modelcat, base)
            if os.path.isdir(model_dir):
                modelfile = os.path.join(model_dir, ov_model.model_precision,
                                         base)
                self.net = self.ie.read_network(modelfile + '.xml',
                                                modelfile + '.bin')
                self.exenet = self.ie.load_network(self.net,
                                                   self.device,
                                                   num_requests=4)
                self.getInterfaceInfo()

    def inference(self, ocvimg):
        """
        Do inference.  
        Args:  
          ocvimg : OpenCV input image for inference.
        Return:  
          res : Inference result returned by OpenVINO IE
        """
        iblobName = self.iblob[0]['name']
        N, C, H, W = self.iblob[0]['shape']
        img = cv2.resize(ocvimg, (W, H))
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img = img.transpose((2, 0, 1))
        img = img.reshape((1, C, H, W))
        res = self.exenet.infer({iblobName: img})
        return res

    # --------------------------- Parsing Algorithms

    def bbox_IOU(self, bbox1, bbox2):
        """
        Calculate IOU of 2 bboxes. bboxes are in SSD format (7 elements)  
        bbox = [id, cls, prob, x1, y1, x2, y2]  
        Args:
            bbox1 (bbox)
            bbox2 (bbox)
        Returns:
            IOU value
        """
        _xmin, _ymin, _xmax, _ymax = [3, 4, 5, 6]
        width_of_overlap_area = min(bbox1[_xmax], bbox2[_xmax]) - max(
            bbox1[_xmin], bbox2[_xmin])
        height_of_overlap_area = min(bbox1[_ymax], bbox2[_ymax]) - max(
            bbox1[_ymin], bbox2[_ymin])
        if width_of_overlap_area < 0 or height_of_overlap_area < 0:
            area_of_overlap = 0
        else:
            area_of_overlap = width_of_overlap_area * height_of_overlap_area
        bbox1_area = (bbox1[_ymax] - bbox1[_ymin]) * (bbox1[_xmax] -
                                                      bbox1[_xmin])
        bbox2_area = (bbox2[_ymax] - bbox2[_ymin]) * (bbox2[_xmax] -
                                                      bbox2[_xmin])
        area_of_union = bbox1_area + bbox2_area - area_of_overlap
        if area_of_union == 0:
            return 0
        return area_of_overlap / area_of_union

    def bbox_NMS(self, bboxes, iou_threshold=0.7):
        """
        Perform non maximum suppression for bboxes to reject redundunt detections.  
        bbox = [id, cls, prob, x1, y1, x2, y2]  
        Args:
            bboxes ([bbox,...]):
            iou_threshold (float): Threshold value of rejection
        Returns:
            NMS applied bboxes
        """
        _clsid, _prob = [1, 2]
        bboxes = sorted(bboxes, key=lambda x: x[_prob], reverse=True)
        for i in range(len(bboxes)):
            if bboxes[i][_prob] == -1:
                continue
            for j in range(i + 1, len(bboxes)):
                iou = self.bbox_IOU(bboxes[i], bboxes[j])
                if iou > iou_threshold:
                    bboxes[j][_prob] = -1
        res = [bbox for bbox in bboxes if bbox[_prob] != -1]
        return res

    def parse_yolo_region_v3(self, blob, resized_image_shape, params,
                             threshold):
        """
        Parse YOLO region. This function is intented to be called from decode_yolo_result().
        Args:
            blob               : An output blob of YOLO model inference result (one blob only).
            resized_image_shape: Shape information of the resized input image.
            params (dict)      : YOLO parameters to decode the result
            threshold (float)  : Threshold value for object rejection
        Returns:
            objs ([bbox]): bbox = [id, clsId, prob, x1, y1, x2, y2]
        """
        def entry_index(side, coord, classes, location, entry):
            side_power_2 = side**2
            n = location // side_power_2
            loc = location % side_power_2
            return int(side_power_2 * (n * (coord + classes + 1) + entry) +
                       loc)

        def scale_bbox(x, y, h, w, class_id, confidence, h_scale, w_scale):
            xmin = int((x - w / 2) * w_scale)
            ymin = int((y - h / 2) * h_scale)
            xmax = int(xmin + w * w_scale)
            ymax = int(ymin + h * h_scale)
            return [class_id, confidence, xmin, ymin, xmax, ymax]

        param_num = 3 if 'num' not in params else int(params['num'])
        param_coords = 4 if 'coords' not in params else int(params['coords'])
        param_classes = 80 if 'classes' not in params else int(
            params['classes'])
        param_side = int(params['side'])
        if 'anchors' not in params:
            anchors = [
                10.0, 13.0, 16.0, 30.0, 33.0, 23.0, 30.0, 61.0, 62.0, 45.0,
                59.0, 119.0, 116.0, 90.0, 156.0, 198.0, 373.0, 326.0
            ]
        else:
            anchors = [
                float(anchor) for anchor in params['anchors'].split(',')
            ]

        if 'mask' not in params:
            param_anchors = anchors
            param_isYoloV3 = False
        else:
            if params['mask'] == '':
                param_anchors = anchors
                param_isYoloV3 = False
            else:
                masks = [int(m) for m in params['mask'].split(',')]
                param_num = len(masks)
                param_anchors = [[anchors[mask * 2], anchors[mask * 2 + 1]]
                                 for mask in masks]
                param_isYoloV3 = True

        out_blob_h, out_blob_w = blob.shape[-2:]

        resized_image_h, resized_image_w = resized_image_shape
        objects = list()
        predictions = blob.flatten()
        side_square = param_side * param_side

        for i in range(side_square):
            row = i // param_side
            col = i % param_side
            for n in range(param_num):
                obj_index = entry_index(param_side, param_coords,
                                        param_classes, n * side_square + i,
                                        param_coords)
                scale = predictions[obj_index]
                if scale < threshold:
                    continue
                box_index = entry_index(param_side, param_coords,
                                        param_classes, n * side_square + i, 0)

                x = (col +
                     predictions[box_index + 0 * side_square]) / param_side
                y = (row +
                     predictions[box_index + 1 * side_square]) / param_side
                try:
                    w_exp = exp(predictions[box_index + 2 * side_square])
                    h_exp = exp(predictions[box_index + 3 * side_square])
                except OverflowError:
                    continue
                w = w_exp * param_anchors[n][0] / (
                    resized_image_w if param_isYoloV3 else param_side)
                h = h_exp * param_anchors[n][1] / (
                    resized_image_h if param_isYoloV3 else param_side)
                for j in range(param_classes):
                    class_index = entry_index(param_side, param_coords,
                                              param_classes,
                                              n * side_square + i,
                                              param_coords + 1 + j)
                    confidence = scale * predictions[class_index]
                    if confidence < threshold:
                        continue
                    objects.append([
                        0., j, confidence, x - w / 2, y - h / 2, x + w / 2,
                        y + h / 2
                    ])
        return objects

    def softmax_channel(self, data):
        for i in range(0, len(data), 2):
            m = max(data[i], data[i + 1])
            data[i] = math.exp(data[i] - m)
            data[i + 1] = math.exp(data[i + 1] - m)
            s = data[i] + data[i + 1]
            data[i] /= s
            data[i + 1] /= s
        return data

    def findRoot(self, point, group_mask):
        root = point
        update_parent = False
        while group_mask[root] != -1:
            root = group_mask[root]
            update_parent = True
        if update_parent:
            group_mask[point] = root
        return root

    def join(self, p1, p2, group_mask):
        root1 = self.findRoot(p1, group_mask)
        root2 = self.findRoot(p2, group_mask)
        if root1 != root2:
            group_mask[root1] = root2

    def get_all(self, points, w, h, group_mask):
        root_map = {}
        mask = np.zeros((h, w), np.int32)
        for px, py in points:
            point_root = self.findRoot(px + py * w, group_mask)
            if not point_root in root_map:
                root_map[point_root] = len(root_map) + 1
            mask[py, px] = root_map[point_root]
        return mask

    def decodeImageByJoin(self, segm_data, segm_data_shape, link_data,
                          link_data_shape, segm_conf_thresh, link_conf_thresh):
        h = segm_data_shape[1]
        w = segm_data_shape[2]
        pixel_mask = np.full((h * w, ), False, dtype=np.bool)
        group_mask = {}
        points = []
        for i, segm in enumerate(segm_data):
            if segm > segm_conf_thresh:
                pixel_mask[i] = True
                points.append((i % w, i // w))
                group_mask[i] = -1
            else:
                pixel_mask[i] = False

        link_mask = np.array([ld >= link_conf_thresh for ld in link_data])

        neighbours = int(link_data_shape[3])
        for px, py in points:
            neighbor = 0
            for ny in range(py - 1, py + 1 + 1):
                for nx in range(px - 1, px + 1 + 1):
                    if nx == px and ny == py:
                        continue
                    if nx < 0 or nx >= w or ny < 0 or ny >= h:
                        continue
                    pixel_value = pixel_mask[ny * w + nx]
                    link_value = link_mask[py * w + px * neighbours + neighbor]
                    if pixel_value and link_value:
                        self.join(px + py * w, nx + ny * w, group_mask)
                    neighbor += 1
        return self.get_all(points, w, h, group_mask)

    def maskToBoxes(self, mask, min_area, min_height, image_size):
        _X, _Y = 0, 1
        bboxes = []
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(mask)
        max_bbox_idx = int(max_val)
        resized_mask = cv2.resize(mask,
                                  image_size,
                                  interpolation=cv2.INTER_NEAREST)

        for i in range(1, max_bbox_idx + 1):
            bbox_mask = np.where(resized_mask == i, 255, 0).astype(np.uint8)
            contours, hierarchy = cv2.findContours(bbox_mask, cv2.RETR_CCOMP,
                                                   cv2.CHAIN_APPROX_SIMPLE)
            if len(contours) == 0:
                continue
            center, size, angle = cv2.minAreaRect(contours[0])
            if min(size[_X], size[_Y]) < min_height:
                continue
            if size[_X] * size[_Y] < min_area:
                continue
            bboxes.append((center, size, angle))
        return bboxes

    def text_detection_postprocess(self, link, segm, image_size,
                                   segm_conf_thresh, link_conf_thresh):
        _N, _C, _H, _W = 0, 1, 2, 3
        kMinArea = 300
        kMinHeight = 10

        link_shape = link.shape
        link_data_size = reduce(lambda a, b: a * b, link_shape)
        link_data = link.transpose((_N, _H, _W, _C))
        link_data = link_data.flatten()
        link_data = self.softmax_channel(link_data)
        link_data = link_data.reshape((-1, 2))[:, 1]
        new_link_data_shape = [
            link_shape[0], link_shape[2], link_shape[3], link_shape[1] / 2
        ]

        segm_shape = segm.shape
        segm_data_size = reduce(lambda a, b: a * b, segm_shape)
        segm_data = segm.transpose((_N, _H, _W, _C))
        segm_data = segm_data.flatten()
        segm_data = self.softmax_channel(segm_data)
        segm_data = segm_data.reshape((-1, 2))[:, 1]
        new_segm_data_shape = [
            segm_shape[0], segm_shape[2], segm_shape[3], segm_shape[1] / 2
        ]

        mask = self.decodeImageByJoin(segm_data, new_segm_data_shape,
                                      link_data, new_link_data_shape,
                                      segm_conf_thresh, link_conf_thresh)
        rects = self.maskToBoxes(mask, kMinArea, kMinHeight, image_size)
        return rects

    # Crop image by rotated rectangle from the input image
    def cropRotatedImage(self, image, rect):
        def topLeftPoint(points):
            big_number = 1e10
            _X, _Y = 0, 1
            most_left = [big_number, big_number]
            almost_most_left = [big_number, big_number]
            most_left_idx = -1
            almost_most_left_idx = -1
            for i, point in enumerate(points):
                px, py = point
                if most_left[_X] > px:
                    if most_left[_X] < big_number:
                        almost_most_left = most_left
                        almost_most_left_idx = most_left_idx
                    most_left = [px, py]
                    most_left_idx = i
                if almost_most_left[_X] > px and [px, py] != most_left:
                    almost_most_left = [px, py]
                    almost_most_left_idx = i
            if almost_most_left[_Y] < most_left[_Y]:
                most_left = almost_most_left
                most_left_idx = almost_most_left_idx
            return most_left_idx, most_left

        _X, _Y, _C = 1, 0, 2
        points = cv2.boxPoints(rect).astype(np.int32)
        most_left_idx, most_left = topLeftPoint(points)
        point0 = points[most_left_idx]
        point1 = points[(most_left_idx + 1) % 4]
        point2 = points[(most_left_idx + 2) % 4]
        target_size = (int(np.linalg.norm(point2 - point1, ord=2)),
                       int(np.linalg.norm(point1 - point0, ord=2)), 3)
        crop = np.full(target_size, 255, np.uint8)
        _from = np.array([point0, point1, point2], dtype=np.float32)
        _to = np.array([[0, 0], [target_size[_X] - 1, 0],
                        [target_size[_X] - 1, target_size[_Y] - 1]],
                       dtype=np.float32)
        M = cv2.getAffineTransform(_from, _to)
        crop = cv2.warpAffine(image, M, (target_size[_X], target_size[_Y]))
        return crop
Ejemplo n.º 8
0
def test_load_network_without_device():
    ie = IECore()
    net = ie.read_network(model=test_net_xml, weights=test_net_bin)
    exec_net = ie.load_network(net)
    assert isinstance(exec_net, ExecutableNetwork)
Ejemplo n.º 9
0
def test_load_network_from_file_without_device():
    ie = IECore()
    exec_net = ie.load_network(test_net_xml)
    assert isinstance(exec_net, ExecutableNetwork)
Ejemplo n.º 10
0
def test_incorrect_bin():
    ie = IECore()
    with pytest.raises(Exception) as e:
        ie.read_network(model=test_net_xml, weights="./model.bin")
    assert "Path to the weights ./model.bin doesn't exist or it's a directory" in str(
        e.value)
Ejemplo n.º 11
0
def test_init_ie_core_with_cfg():
    ie = IECore(plugins_xml)
    assert isinstance(ie, IECore)
Ejemplo n.º 12
0
def test_incorrect_xml():
    ie = IECore()
    with pytest.raises(Exception) as e:
        ie.read_network(model="./model.xml", weights=Path(test_net_bin))
    assert "Path to the model ./model.xml doesn't exist or it's a directory" in str(
        e.value)
Ejemplo n.º 13
0
def test_read_network_from_onnx_as_path():
    ie = IECore()
    net = ie.read_network(model=Path(test_net_onnx))
    assert isinstance(net, IENetwork)
Ejemplo n.º 14
0
def test_read_network_from_onnx():
    ie = IECore()
    net = ie.read_network(model=test_net_onnx)
    assert isinstance(net, IENetwork)
from openvino.inference_engine import IENetwork, IECore

import numpy as np
import time

# Loading model
model_path = 'sep_cnn/sep_cnn'
model_weights = model_path + '.bin'
model_structure = model_path + '.xml'

# COMPLETED: Load the model
network = IENetwork(model_structure, model_weights)

core = IECore()
model = core.load_network(network=network, device_name='CPU', num_requests=1)

input_name = next(iter(network.inputs))

# Reading and Preprocessing Image
input_img = np.load('image.npy')
input_img = input_img.reshape(1, 28, 28)

# COMPLETED: Using the input image, run inference on the model for 10 iterations
input_dict = {input_name: input_img}

start = time.time()
for _ in range(10):
    # input_dict: Run Inference in a Loop
    model.infer(input_dict)

# COMPLETED: Finish the print statement
Ejemplo n.º 16
0
def main():
    args = build_argparser().parse_args()

    if not is_correct_args(args):
        return 1

    ie = IECore()

    if args.model_melgan is not None:
        vocoder = MelGANIE(args.model_melgan, ie, device=args.device)
    else:

        vocoder = WaveRNNIE(args.model_upsample,
                            args.model_rnn,
                            ie,
                            device=args.device,
                            upsampler_width=args.upsampler_width)

    forward_tacotron = ForwardTacotronIE(args.model_duration,
                                         args.model_forward,
                                         ie,
                                         args.device,
                                         verbose=False)

    audio_res = np.array([], dtype=np.int16)

    len_th = 512

    time_forward = 0
    time_wavernn = 0

    time_s_all = time.perf_counter()
    with open(args.input, 'r') as f:
        count = 0
        for line in f:
            count += 1
            line = line.rstrip()
            print("Process line {0} with length {1}.".format(count, len(line)))

            if len(line) > len_th:
                texts = []
                prev_begin = 0
                delimiters = '.!?;:'
                for i, c in enumerate(line):
                    if (c in delimiters
                            and i - prev_begin > len_th) or i == len(line) - 1:
                        texts.append(line[prev_begin:i + 1])
                        prev_begin = i + 1
            else:
                texts = [line]

            for text in tqdm(texts):
                time_s = time.perf_counter()
                mel = forward_tacotron.forward(text)
                time_e = time.perf_counter()
                time_forward += (time_e - time_s) * 1000

                time_s = time.perf_counter()
                audio = vocoder.forward(mel)
                time_e = time.perf_counter()
                time_wavernn += (time_e - time_s) * 1000

                audio_res = np.append(audio_res, audio)

            if count % 5 == 0:
                print('WaveRNN time: {:.3f}ms. ForwardTacotronTime {:.3f}ms'.
                      format(time_wavernn, time_forward))
    time_e_all = time.perf_counter()

    print(
        'All time {:.3f}ms. WaveRNN time: {:.3f}ms. ForwardTacotronTime {:.3f}ms'
        .format((time_e_all - time_s_all) * 1000, time_wavernn, time_forward))

    save_wav(audio_res, args.out)
def main():
    _H = 0
    _W = 1
    _C = 2

    global g_canvas, g_mask
    global g_threshold
    global g_UIState
    global g_inpaintFlag
    global g_clickedFlag

    if len(sys.argv) < 2:
        print('Please specify an input file', file=sys.stderr)
        return -1
    g_canvas = cv2.imread(sys.argv[1])

    ie = IECore()

    model = 'gmcnn-places2-tf'
    model = './public/' + model + '/FP16/' + model
    net = ie.read_network(model + '.xml', model + '.bin')
    input_blob1 = 'Placeholder'
    input_blob2 = 'Placeholder_1'
    out_blob = 'Minimum'
    in_shape1 = net.inputs[input_blob1].shape  # 1,3,512,680
    in_shape2 = net.inputs[input_blob2].shape
    out_shape = net.outputs[out_blob].shape  # 1,3,512,680
    exec_net = ie.load_network(net, 'CPU')

    clearMask()
    cv2.namedWindow('canvas')
    cv2.setMouseCallback('canvas', onMouse)
    cv2.createTrackbar('Pen size', 'canvas', 8, 32, onTrackbar)

    while True:
        g_UIState = 0
        while g_inpaintFlag == False:
            dispCanvas()
            key = cv2.waitKey(100)
            if key == 27:
                return
            if key == ord(' '):
                break
        g_inpaintFlag = False
        g_UIState = 1

        img = g_canvas | g_mask
        img = cv2.resize(img, (in_shape1[3], in_shape1[2]))
        img = img.transpose((_C, _H, _W))
        img = img.reshape(in_shape1)

        msk = cv2.resize(g_mask, (in_shape2[3], in_shape2[2]))
        msk = msk.transpose((_C, _H, _W))
        msk = msk[0, :, :]
        msk = np.where(msk > 0., 1., 0.).astype(np.float32)
        msk = msk.reshape(in_shape2)

        res = exec_net.infer(inputs={input_blob1: img, input_blob2: msk})

        out = np.transpose(res[out_blob], (0, 2, 3, 1)).astype(np.uint8)
        out = cv2.cvtColor(out[0], cv2.COLOR_RGB2BGR)

        cv2.imshow('Result', out)
        cv2.waitKey(1)

    return 0
Ejemplo n.º 18
0
    def __init__(self,
                 device='CPU',
                 track=False,
                 visualize=False,
                 prob_threshold=0.3,
                 max_seq_len=10,
                 iou_threshold=0.4,
                 model_type='FP32',
                 rgb2bgr=True):

        assert (model_type == 'FP32') or (model_type == 'FP16')

        mask_rcnn_model_xml = get_models()[
            '{}/text-spotting-0001-detector.xml'.format(model_type)]
        mask_rcnn_model_bin = get_models()[
            '{}/text-spotting-0001-detector.bin'.format(model_type)]

        text_enc_model_xml = get_models()[
            '{}/text-spotting-0001-recognizer-encoder.xml'.format(model_type)]
        text_enc_model_bin = get_models()[
            '{}/text-spotting-0001-recognizer-encoder.bin'.format(model_type)]

        text_dec_model_xml = get_models()[
            '{}/text-spotting-0001-recognizer-decoder.xml'.format(model_type)]
        text_dec_model_bin = get_models()[
            '{}/text-spotting-0001-recognizer-decoder.bin'.format(model_type)]

        # Plugin initialization for specified device and load extensions library if specified.
        log.info('Creating Inference Engine...')
        ie = IECore()
        # Read IR
        log.info('Loading network files:\n\t{}\n\t{}'.format(
            mask_rcnn_model_xml, mask_rcnn_model_bin))
        mask_rcnn_net = IENetwork(model=mask_rcnn_model_xml,
                                  weights=mask_rcnn_model_bin)

        log.info('Loading network files:\n\t{}\n\t{}'.format(
            text_enc_model_xml, text_enc_model_bin))
        text_enc_net = IENetwork(model=text_enc_model_xml,
                                 weights=text_enc_model_bin)

        log.info('Loading network files:\n\t{}\n\t{}'.format(
            text_dec_model_xml, text_dec_model_bin))
        text_dec_net = IENetwork(model=text_dec_model_xml,
                                 weights=text_dec_model_bin)

        supported_layers = ie.query_network(mask_rcnn_net, 'CPU')
        not_supported_layers = [
            l for l in mask_rcnn_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 sample's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)

        required_input_keys = {'im_data', 'im_info'}
        assert required_input_keys == set(mask_rcnn_net.inputs.keys()), \
            'Demo supports only topologies with the following input keys: {}'.format(', '.join(required_input_keys))
        required_output_keys = {
            'boxes', 'scores', 'classes', 'raw_masks', 'text_features'
        }
        assert required_output_keys.issubset(mask_rcnn_net.outputs.keys()), \
            'Demo supports only topologies with the following output keys: {}'.format(', '.join(required_output_keys))

        n, c, h, w = mask_rcnn_net.inputs['im_data'].shape
        assert n == 1, 'Only batch 1 is supported by the demo application'
        self.shape = [n, c, h, w]
        log.info('Loading IR to the plugin...')
        self.mask_rcnn_exec_net = ie.load_network(network=mask_rcnn_net,
                                                  device_name=device,
                                                  num_requests=2)
        self.text_enc_exec_net = ie.load_network(network=text_enc_net,
                                                 device_name=device)
        self.text_dec_exec_net = ie.load_network(network=text_dec_net,
                                                 device_name=device)
        self.hidden_shape = text_dec_net.inputs['prev_hidden'].shape
        self.prob_threshold = prob_threshold
        self.tracker = None
        self.visualizer = None
        self.iou_threshold = iou_threshold
        self.max_seq_len = max_seq_len
        self.rgb2bgr = rgb2bgr
        self.device_names = get_fields_info()
        if track:
            self.tracker = StaticIOUTracker()
        if visualize:
            self.visualizer = Visualizer(['__background__', 'text'],
                                         show_boxes=True,
                                         show_scores=True)
        log.info('Model ready...')
Ejemplo n.º 19
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()

    if args.extension and args.device == 'CPU':
        log.info(f'Loading the {args.device} extension: {args.extension}')
        ie.add_extension(args.extension, args.device)

    if args.config and args.device in ('GPU', 'MYRIAD', 'HDDL'):
        log.info(f'Loading the {args.device} configuration: {args.config}')
        ie.set_config({'CONFIG_FILE': args.config}, args.device)

# ---------------------------Step 2. Read a model in OpenVINO Intermediate Representation or ONNX format---------------
    log.info(f'Reading the network: {args.model}')
    # (.xml and .bin files) or (.onnx file)
    net = ie.read_network(model=args.model)

    if len(net.input_info) != 1:
        log.error('Sample supports only single input topologies')
        return -1
    if len(net.outputs) != 1:
        log.error('Sample supports only single output topologies')
        return -1

# ---------------------------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---------------------------------------------------------------------
    original_images = []

    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 = cv2.imread(args.input[i])
        original_images.append(image)

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

        # Change data layout from HWC to CHW
        image = image.transpose((2, 0, 1))

        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--------------------------------------------------------------------
    res = res[out_blob]

    for i in range(n):
        output_image = res[i]
        # Change data layout from CHW to HWC
        output_image = output_image.transpose((1, 2, 0))
        # Convert BGR color order to RGB
        output_image = cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB)

        # Apply mean argument values
        output_image = output_image[::] - (args.mean_val_r, args.mean_val_g,
                                           args.mean_val_b)
        # Set pixel values bitween 0 and 255
        output_image = np.clip(output_image, 0, 255)

        # Resize a output image to original size
        if args.original_size:
            h, w, _ = original_images[i].shape
            output_image = cv2.resize(output_image, (w, h))

        cv2.imwrite(f'out_{i}.bmp', output_image)
        log.info(f'Image out_{i}.bmp created!')


# ----------------------------------------------------------------------------------------------------------------------
    log.info(
        'This sample is an API example, '
        'for any performance measurements please use the dedicated benchmark_app tool\n'
    )
    return 0
Ejemplo n.º 20
0
def main():
    # Plugin initialization for specified device and load extensions library
    global rolling_log
    #defaultTarget = TARGET_DEVICE

    env_parser()
    args_parser()
    check_args()
    parse_conf_file()
    job_id = os.environ['PBS_JOBID']
    job_id = job_id.rstrip().split('.')[0]

    # if TARGET_DEVICE not in acceptedDevices:
    #     print ("Unsupporterd device " + TARGET_DEVICE + ". Defaulting to CPU")
    #     TARGET_DEVICE = 'CPU'

    print("Initializing plugin for {} device...".format(TARGET_DEVICE))
    #plugin = IEPlugin(device=TARGET_DEVICE)
    ie = IECore()
    if CPU_EXTENSION and 'CPU' == TARGET_DEVICE:
        #plugin.add_cpu_extension(CPU_EXTENSION)
        ie.add_extension(CPU_EXTENSION, "CPU")

    # Read IR
    print("Reading IR...")
    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"
    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))

    # Load the IR
    print("Loading IR to the plugin...")
    #exec_net = plugin.load(network=net, num_requests=2)
    exec_net = ie.load_network(network=net, num_requests=2, device_name=TARGET_DEVICE)
    # Read and pre-process input image
    n, c, h, w = net.inputs[input_blob].shape
    del net

    minFPS = min([i.cap.get(cv2.CAP_PROP_FPS) for i in videoCaps])
    minlength = min([i.cap.get(cv2.CAP_PROP_FRAME_COUNT) for i in videoCaps])
    for vc in videoCaps:
        vc.rate = int(math.ceil(vc.length/minlength))
    print(minFPS)
    waitTime = int(round(1000 / minFPS / len(videoCaps))) # wait time in ms between showing frames
    frames_sum = 0
    for vc in videoCaps:
        vc.init_vw(h, w, minFPS)
        frames_sum += vc.length
    statsWidth = w if w > 345 else 345
    statsHeight = h if h > (len(videoCaps) * 20 + 15) else (len(videoCaps) * 20 + 15)
    statsVideo = cv2.VideoWriter(os.path.join(output_dir,'Statistics.mp4'), cv2.VideoWriter_fourcc(*"AVC1"), minFPS, (statsWidth, statsHeight), True)
    if not statsVideo.isOpened():
        print ("Couldn't open stats video for writing")
        sys.exit(4)

    # Read the labels file
    if labels_file:
        with open(labels_file, 'r') as f:
            labels_map = [x.strip() for x in f]
    else:
        labels_map = None

    # Init a rolling log to store events
    rolling_log_size = int((h - 15) / 20)
    rolling_log = collections.deque(maxlen=rolling_log_size)

    # Init inference request IDs
    cur_request_id = 0
    next_request_id = 1
    # Start with async mode enabled
    is_async_mode = True

    if not UI_OUTPUT:
        # Arrange windows so they are not overlapping
        #arrange_windows(w, h)
        print("To stop the execution press Esc button")

    no_more_data = False

    for vc in videoCaps:
        vc.start_time = datetime.datetime.now()
    frame_count = 0
    progress_file_path = os.path.join(output_dir, job_id, 'i_progress.txt')
    infer_start_time = time.time()

    #Start while loop
    while True:
        # If all video captures are closed stop the loop
        if False not in [videoCap.closed for videoCap in videoCaps]:
            print("I broke here line 387")
            break

        no_more_data = False

        # loop over all video captures
        for idx, videoCapInfer in enumerate(videoCaps):
            # read the next frame
            #print("Video {0} has length {1} and fps {2}".format(idx, videoCapInfer.length,  videoCapInfer.fps))
            if not videoCapInfer.closed:
                 #print("ID {0}".format(idx))
                 vfps = int(round(videoCapInfer.cap.get(cv2.CAP_PROP_FPS)))
                 #for i in range(0, int(round(vfps / minFPS))):
                 for i in range(videoCapInfer.rate):
                     frame_count += 1
                     #print("i = {0}".format(i))
                     ret, frame = videoCapInfer.cap.read()
                     videoCapInfer.cur_frame_count += 1
                     # If the read failed close the program
                     if not ret:
                         videoCapInfer.closed = True
                         no_more_data = True
                         break


                 if videoCapInfer.closed:
                     print("Video {0} is done".format(idx))
                     print("Video has  {0} frames ".format(videoCapInfer.length))
                     break

                 # Copy the current frame for later use
                 videoCapInfer.cur_frame = frame.copy()
                 videoCapInfer.initial_w = videoCapInfer.cap.get(3)
                 videoCapInfer.initial_h = videoCapInfer.cap.get(4)
                 # Resize and change the data layout so it is compatible
                 in_frame = cv2.resize(videoCapInfer.cur_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))

                 infer_start = datetime.datetime.now()
                 if is_async_mode:
                     exec_net.start_async(request_id=next_request_id, inputs={input_blob: in_frame})
                     # Async enabled and only one video capture
                     if(len(videoCaps) == 1):
                         videoCapResult = videoCapInfer
                     # Async enabled and more than one video capture
                     else:
                         # Get previous index
                         videoCapResult = videoCaps[idx - 1 if idx - 1 >= 0 else len(videoCaps) - 1]
                 else:
                     # Async disabled
                     exec_net.start_async(request_id=cur_request_id, inputs={input_blob: in_frame})
                     videoCapResult = videoCapInfer

                 if exec_net.requests[cur_request_id].wait(-1) == 0:
                     infer_end = datetime.datetime.now()
                     infer_duration = infer_end - infer_start
                     current_count = 0
                     # Parse detection results of the current request
                     res = exec_net.requests[cur_request_id].outputs[out_blob]
                     for obj in res[0][0]:
                         class_id = int(obj[1])
                         # Draw only objects when probability more than specified threshold
                         if (obj[2] > PROB_THRESHOLD and
                             videoCapResult.req_label in labels_map and
                             labels_map.index(videoCapResult.req_label) == class_id - 1):
                             current_count += 1
                             xmin = int(obj[3] * videoCapResult.initial_w)
                             ymin = int(obj[4] * videoCapResult.initial_h)
                             xmax = int(obj[5] * videoCapResult.initial_w)
                             ymax = int(obj[6] * videoCapResult.initial_h)
                             # Draw box
                             cv2.rectangle(videoCapResult.cur_frame, (xmin, ymin), (xmax, ymax), (0, 255, 0), 4, 16)

                     if videoCapResult.candidate_count is current_count:
                         videoCapResult.candidate_confidence += 1
                     else:
                         videoCapResult.candidate_confidence = 0
                         videoCapResult.candidate_count = current_count

                     if videoCapResult.candidate_confidence is FRAME_THRESHOLD:
                         videoCapResult.candidate_confidence = 0
                         if current_count > videoCapResult.last_correct_count:
                             videoCapResult.total_count += current_count - videoCapResult.last_correct_count

                         if current_count is not videoCapResult.last_correct_count:
                             if UI_OUTPUT:
                                 currtime = datetime.datetime.now().strftime("%H:%M:%S")
                                 fr = FrameInfo(videoCapResult.frames, current_count, currtime)
                                 videoCapResult.countAtFrame.append(fr)
                             new_objects = current_count - videoCapResult.last_correct_count
                             for _ in range(new_objects):
                                 string = "{} - {} detected on {}".format(time.strftime("%H:%M:%S"), videoCapResult.req_label, videoCapResult.cap_name)
                                 rolling_log.append(string)

                         videoCapResult.frames+=1
                         videoCapResult.last_correct_count = current_count
                     else:
                         videoCapResult.frames+=1

                     videoCapResult.cur_frame = cv2.resize(videoCapResult.cur_frame, (w, h))

                     if not UI_OUTPUT:
                         # Add log text to each frame
                         log_message = "Async mode is on." if is_async_mode else \
                                       "Async mode is off."
                         cv2.putText(videoCapResult.cur_frame, log_message, (15, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
                         log_message = "Total {} count: {}".format(videoCapResult.req_label, videoCapResult.total_count)
                         cv2.putText(videoCapResult.cur_frame, log_message, (10, h - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
                         log_message = "Current {} count: {}".format(videoCapResult.req_label, videoCapResult.last_correct_count)
                         cv2.putText(videoCapResult.cur_frame, log_message, (10, h - 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
                         cv2.putText(videoCapResult.cur_frame, 'Infer wait: %0.3fs' % (infer_duration.total_seconds()), (10, h - 70), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)

                         # Display inferred frame and stats
                         stats = numpy.zeros((statsHeight, statsWidth, 1), dtype = 'uint8')
                         for i, log in enumerate(rolling_log):
                             cv2.putText(stats, log, (10, i * 20 + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
                         #cv2.imshow(STATS_WINDOW_NAME, stats)
                         if idx == 0:
                             stats = cv2.cvtColor(stats, cv2.COLOR_GRAY2BGR)
                             #Write
                             statsVideo.write(stats)
                         end_time = datetime.datetime.now()
                         cv2.putText(videoCapResult.cur_frame, 'FPS: %0.2fs' % (1 / (end_time - videoCapResult.start_time).total_seconds()), (10, h - 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
                         #cv2.imshow(videoCapResult.cap_name, videoCapResult.cur_frame)
                         videoCapResult.start_time = datetime.datetime.now()
                         #Write
                         videoCapResult.video.write(videoCapResult.cur_frame)
                      
            if frame_count%10 == 0: 
                progressUpdate(progress_file_path, time.time()-infer_start_time, frame_count, frames_sum) 


            # Wait if necessary for the required time
            #key = cv2.waitKey(waitTime)
            key = cv2.waitKey(1)
            
            # Esc key pressed
            if key == 27:
                cv2.destroyAllWindows()
                del exec_net
                #del plugin
                del ie
                print("Finished")
                return
            # Tab key pressed
            if key == 9:
                is_async_mode = not is_async_mode
                print("Switched to {} mode".format("async" if is_async_mode else "sync"))

            if is_async_mode:
                # Swap infer request IDs
                cur_request_id, next_request_id = next_request_id, cur_request_id

            # Loop video if LOOP_VIDEO = True and input isn't live from USB camera
            if LOOP_VIDEO and not videoCapInfer.is_cam:
                vfps = int(round(videoCapInfer.cap.get(cv2.CAP_PROP_FPS)))
                # If a video capture has ended restart it
                if (videoCapInfer.cur_frame_count > videoCapInfer.cap.get(cv2.CAP_PROP_FRAME_COUNT) - int(round(vfps / minFPS))):
                    videoCapInfer.cur_frame_count = 0
                    videoCapInfer.cap.set(cv2.CAP_PROP_POS_FRAMES, 0)

        if no_more_data:
            progressUpdate(progress_file_path, time.time()-infer_start_time, frames_sum, frames_sum)
            break
#End of while loop--------------------
    no_more_data = True
    t2 = round(time.time()-infer_start_time,2)
    for videos in videoCaps:
        print(videos.length)
        print(videos.closed)
    print("End loop")
    print("Total time {0}".format(t2))
    print("Total frame count {0}".format(frame_count))
    print("fps {0}".format(frame_count/t2))
    stats = {}
    stats['time'] = str(t2) # Total Time
    stats['frames'] = str(frame_count)
    stats['fps'] = str(round(frame_count / t2,2))
    with open(os.path.join(output_dir, job_id, 'stats.json'), 'w') as json_file:
        json.dump(stats, json_file)

    for vc in videoCaps:
        print("Frames processed {}".format(vc.cur_frame_count))
        print("Frames count {}".format(vc.length))

    for vc in videoCaps:
        vc.video.release()
        vc.cap.release()

        if no_more_data:
            break
def main():
    args = build_argparser().parse_args()

    # ------------- 1. Plugin initialization for specified device and load extensions library if specified -------------
    log.info("Creating Inference Engine...")
    ie = IECore()

    config_user_specified = {}
    config_min_latency = {}

    devices_nstreams = {}
    if args.num_streams:
        devices_nstreams = {device: args.num_streams for device in ['CPU', 'GPU'] if device in args.device} \
                           if args.num_streams.isdigit() \
                           else dict([device.split(':') for device in args.num_streams.split(',')])

    if 'CPU' in args.device:
        if args.cpu_extension:
            ie.add_extension(args.cpu_extension, 'CPU')
        if args.number_threads is not None:
            config_user_specified['CPU_THREADS_NUM'] = str(args.number_threads)
        if 'CPU' in devices_nstreams:
            config_user_specified['CPU_THROUGHPUT_STREAMS'] = devices_nstreams['CPU'] \
                                                              if int(devices_nstreams['CPU']) > 0 \
                                                              else 'CPU_THROUGHPUT_AUTO'

        config_min_latency['CPU_THROUGHPUT_STREAMS'] = '1'

    if 'GPU' in args.device:
        if 'GPU' in devices_nstreams:
            config_user_specified['GPU_THROUGHPUT_STREAMS'] = devices_nstreams['GPU'] \
                                                              if int(devices_nstreams['GPU']) > 0 \
                                                              else 'GPU_THROUGHPUT_AUTO'

        config_min_latency['GPU_THROUGHPUT_STREAMS'] = '1'

    # -------------------- 2. Reading the IR generated by the Model Optimizer (.xml and .bin files) --------------------
    log.info("Loading network")
    net = ie.read_network(args.model, os.path.splitext(args.model)[0] + ".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.input_info
    ) == 1, "Sample supports only YOLO V3 based single input topologies"

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

    # Read and pre-process input images
    if net.input_info[input_blob].input_data.shape[1] == 3:
        input_height, input_width = net.input_info[
            input_blob].input_data.shape[2:]
        nchw_shape = True
    else:
        input_height, input_width = net.input_info[
            input_blob].input_data.shape[1:3]
        nchw_shape = False

    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

    mode = Mode(Modes.USER_SPECIFIED)
    cap = cv2.VideoCapture(input_stream)
    wait_key_time = 1

    # ----------------------------------------- 5. Loading model to the plugin -----------------------------------------
    log.info("Loading model to the plugin")
    exec_nets = {}

    exec_nets[Modes.USER_SPECIFIED] = ie.load_network(
        network=net,
        device_name=args.device,
        config=config_user_specified,
        num_requests=args.num_infer_requests)
    exec_nets[Modes.MIN_LATENCY] = ie.load_network(
        network=net,
        device_name=args.device.split(":")[-1].split(",")[0],
        config=config_min_latency,
        num_requests=1)

    empty_requests = deque(exec_nets[mode.current].requests)
    completed_request_results = {}
    next_frame_id = 0
    next_frame_id_to_show = 0
    mode_info = {mode.current: ModeInfo()}
    event = threading.Event()
    callback_exceptions = []

    # ----------------------------------------------- 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 min_latency/user_specified modes, press TAB key in the output window"
    )

    presenter = monitors.Presenter(
        args.utilization_monitors, 55,
        (round(cap.get(cv2.CAP_PROP_FRAME_WIDTH) / 4),
         round(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) / 8)))

    while (cap.isOpened() \
           or completed_request_results \
           or len(empty_requests) < len(exec_nets[mode.current].requests)) \
          and not callback_exceptions:
        if next_frame_id_to_show in completed_request_results:
            frame, output, start_time, is_same_mode = completed_request_results.pop(
                next_frame_id_to_show)

            next_frame_id_to_show += 1
            if is_same_mode:
                mode_info[mode.current].frames_count += 1

            objects = get_objects(output, net, (input_height, input_width),
                                  frame.shape[:-1], args.prob_threshold,
                                  args.keep_aspect_ratio)
            objects = filter_objects(objects, args.iou_threshold,
                                     args.prob_threshold)

            if len(objects) and args.raw_output_message:
                log.info(
                    " Class ID | Confidence | XMIN | YMIN | XMAX | YMAX | COLOR "
                )

            origin_im_size = frame.shape[:-1]
            presenter.drawGraphs(frame)
            for obj in objects:
                # Validation bbox of detected object
                obj['xmax'] = min(obj['xmax'], origin_im_size[1])
                obj['ymax'] = min(obj['ymax'], origin_im_size[0])
                obj['xmin'] = max(obj['xmin'], 0)
                obj['ymin'] = max(obj['ymin'], 0)
                color = (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
            if mode_info[mode.current].frames_count != 0:
                fps_message = "FPS: {:.1f}".format(mode_info[mode.current].frames_count / \
                                                   (perf_counter() - mode_info[mode.current].last_start_time))
                mode_info[
                    mode.current].latency_sum += perf_counter() - start_time
                latency_message = "Latency: {:.1f} ms".format((mode_info[mode.current].latency_sum / \
                                                              mode_info[mode.current].frames_count) * 1e3)

                put_highlighted_text(frame, fps_message, (15, 20),
                                     cv2.FONT_HERSHEY_COMPLEX, 0.75,
                                     (200, 10, 10), 2)
                put_highlighted_text(frame, latency_message, (15, 50),
                                     cv2.FONT_HERSHEY_COMPLEX, 0.75,
                                     (200, 10, 10), 2)

            mode_message = "{} mode".format(mode.current.name)
            put_highlighted_text(frame, mode_message,
                                 (10, int(origin_im_size[0] - 20)),
                                 cv2.FONT_HERSHEY_COMPLEX, 0.75, (10, 10, 200),
                                 2)

            if not args.no_show:
                cv2.imshow("Detection Results", frame)
                key = cv2.waitKey(wait_key_time)

                if key in {ord("q"), ord("Q"), 27}:  # ESC key
                    break
                if key == 9:  # Tab key
                    prev_mode = mode.current
                    mode.next()

                    await_requests_completion(exec_nets[prev_mode].requests)
                    empty_requests.clear()
                    empty_requests.extend(exec_nets[mode.current].requests)

                    mode_info[prev_mode].last_end_time = perf_counter()
                    mode_info[mode.current] = ModeInfo()
                else:
                    presenter.handleKey(key)

        elif empty_requests and cap.isOpened():
            start_time = perf_counter()
            ret, frame = cap.read()
            if not ret:
                if args.loop_input:
                    cap.open(input_stream)
                else:
                    cap.release()
                continue

            request = empty_requests.popleft()

            # resize input_frame to network size
            in_frame = preprocess_frame(frame, input_height, input_width,
                                        nchw_shape, args.keep_aspect_ratio)

            # Start inference
            request.set_completion_callback(
                py_callback=async_callback,
                py_data=(request, next_frame_id, mode.current, frame,
                         start_time, completed_request_results, empty_requests,
                         mode, event, callback_exceptions))
            request.async_infer(inputs={input_blob: in_frame})
            next_frame_id += 1

        else:
            event.wait()

    if callback_exceptions:
        raise callback_exceptions[0]

    for mode_value in mode_info.keys():
        log.info("")
        log.info("Mode: {}".format(mode_value.name))

        end_time = mode_info[mode_value].last_end_time if mode_value in mode_info \
                                                          and mode_info[mode_value].last_end_time is not None \
                                                       else perf_counter()
        log.info("FPS: {:.1f}".format(mode_info[mode_value].frames_count / \
                                      (end_time - mode_info[mode_value].last_start_time)))
        log.info("Latency: {:.1f} ms".format((mode_info[mode_value].latency_sum / \
                                             mode_info[mode_value].frames_count) * 1e3))
    print(presenter.reportMeans())

    for exec_net in exec_nets.values():
        await_requests_completion(exec_net.requests)
Ejemplo n.º 22
0
 def load_model(self):
     core = IECore()
     self.net = core.load_network(network=self.model,
                                  device_name=self.device,
                                  num_requests=1)
def main():
    parser = argparse.ArgumentParser(description='Whiteboard inpainting demo')
    parser.add_argument(
        '-i',
        '--input',
        required=True,
        help='Required. Path to a video file or a device node of a web-camera.'
    )
    parser.add_argument('--loop',
                        default=False,
                        action='store_true',
                        help='Optional. Enable reading the input in a loop.')
    parser.add_argument('-o',
                        '--output',
                        required=False,
                        help='Optional. Name of the output file(s) to save.')
    parser.add_argument('-limit',
                        '--output_limit',
                        required=False,
                        default=1000,
                        type=int,
                        help='Optional. Number of frames to store in output. '
                        'If 0 is set, all frames are stored.')
    parser.add_argument(
        '-m_i',
        '--m_instance_segmentation',
        type=str,
        required=False,
        help='Required. Path to the instance segmentation model.')
    parser.add_argument(
        '-m_s',
        '--m_semantic_segmentation',
        type=str,
        required=False,
        help='Required. Path to the semantic segmentation model.')
    parser.add_argument(
        '-t',
        '--threshold',
        type=float,
        default=0.6,
        help='Optional. Threshold for person instance segmentation model.')
    parser.add_argument('--no_show',
                        help="Optional. Don't show output.",
                        action='store_true')
    parser.add_argument(
        '-d',
        '--device',
        type=str,
        default='CPU',
        help=
        'Optional. Specify a target device to infer on. CPU, GPU, HDDL or MYRIAD is '
        'acceptable. The demo will look for a suitable plugin for the device specified.'
    )
    parser.add_argument('-l',
                        '--cpu_extension',
                        type=str,
                        default=None,
                        help='MKLDNN (CPU)-targeted custom layers. Absolute \
                              path to a shared library with the kernels impl.')
    parser.add_argument('-u',
                        '--utilization_monitors',
                        default='',
                        type=str,
                        help='Optional. List of monitors to show initially.')
    args = parser.parse_args()

    cap = open_images_capture(args.input, args.loop)
    if cap.get_type() not in ('VIDEO', 'CAMERA'):
        raise RuntimeError(
            "The input should be a video file or a numeric camera ID")

    if bool(args.m_instance_segmentation) == bool(
            args.m_semantic_segmentation):
        raise ValueError(
            'Set up exactly one of segmentation models: '
            '--m_instance_segmentation or --m_semantic_segmentation')

    labels_dir = Path(__file__).resolve().parents[3] / 'data/dataset_classes'
    mouse = MouseClick()
    if not args.no_show:
        cv2.namedWindow(WINNAME)
        cv2.setMouseCallback(WINNAME, mouse.get_points)

    log.info('OpenVINO Inference Engine')
    log.info('\tbuild: {}'.format(get_version()))
    ie = IECore()

    model_path = args.m_instance_segmentation if args.m_instance_segmentation else args.m_semantic_segmentation
    log.info('Reading model {}'.format(model_path))
    if args.m_instance_segmentation:
        labels_file = str(labels_dir / 'coco_80cl_bkgr.txt')
        segmentation = MaskRCNN(ie, args.m_instance_segmentation, labels_file,
                                args.threshold, args.device,
                                args.cpu_extension)
    elif args.m_semantic_segmentation:
        labels_file = str(labels_dir / 'cityscapes_19cl_bkgr.txt')
        segmentation = SemanticSegmentation(ie, args.m_semantic_segmentation,
                                            labels_file, args.threshold,
                                            args.device, args.cpu_extension)
    log.info('The model {} is loaded to {}'.format(model_path, args.device))

    metrics = PerformanceMetrics()
    video_writer = cv2.VideoWriter()
    black_board = False
    frame_number = 0
    key = -1

    start_time = perf_counter()
    frame = cap.read()
    if frame is None:
        raise RuntimeError("Can't read an image from the input")

    out_frame_size = (frame.shape[1], frame.shape[0] * 2)
    output_frame = np.full((frame.shape[0], frame.shape[1], 3),
                           255,
                           dtype='uint8')
    presenter = monitors.Presenter(
        args.utilization_monitors, 20,
        (out_frame_size[0] // 4, out_frame_size[1] // 16))
    if args.output and not video_writer.open(args.output,
                                             cv2.VideoWriter_fourcc(*'MJPG'),
                                             cap.fps(), out_frame_size):
        raise RuntimeError("Can't open video writer")

    while frame is not None:
        mask = None
        detections = segmentation.get_detections([frame])
        expand_mask(detections, frame.shape[1] // 27)
        if len(detections[0]) > 0:
            mask = detections[0][0][2]
            for i in range(1, len(detections[0])):
                mask = cv2.bitwise_or(mask, detections[0][i][2])

        if mask is not None:
            mask = np.stack([mask, mask, mask], axis=-1)
        else:
            mask = np.zeros(frame.shape, dtype='uint8')

        clear_frame = remove_background(frame, invert_colors=not black_board)

        output_frame = np.where(mask, output_frame, clear_frame)
        merged_frame = np.vstack([frame, output_frame])
        merged_frame = cv2.resize(merged_frame, out_frame_size)

        metrics.update(start_time, merged_frame)

        if video_writer.isOpened() and (args.output_limit <= 0 or
                                        frame_number <= args.output_limit - 1):
            video_writer.write(merged_frame)

        presenter.drawGraphs(merged_frame)
        if not args.no_show:
            cv2.imshow(WINNAME, merged_frame)
            key = check_pressed_keys(key)
            if key == 27:  # 'Esc'
                break
            if key == ord('i'):  # catch pressing of key 'i'
                black_board = not black_board
                output_frame = 255 - output_frame
            else:
                presenter.handleKey(key)

        if mouse.crop_available:
            x0, x1 = min(mouse.points[0][0], mouse.points[1][0]), \
                     max(mouse.points[0][0], mouse.points[1][0])
            y0, y1 = min(mouse.points[0][1], mouse.points[1][1]), \
                     max(mouse.points[0][1], mouse.points[1][1])
            x1, y1 = min(x1, output_frame.shape[1] - 1), min(
                y1, output_frame.shape[0] - 1)
            board = output_frame[y0:y1, x0:x1, :]
            if board.shape[0] > 0 and board.shape[1] > 0:
                cv2.namedWindow('Board', cv2.WINDOW_KEEPRATIO)
                cv2.imshow('Board', board)

        frame_number += 1
        start_time = perf_counter()
        frame = cap.read()

    metrics.log_total()
    for rep in presenter.reportMeans():
        log.info(rep)
Ejemplo n.º 24
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()

    if args.extension and args.device == 'CPU':
        log.info(f'Loading the {args.device} extension: {args.extension}')
        ie.add_extension(args.extension, args.device)

    if args.config and args.device in ('GPU', 'MYRIAD', 'HDDL'):
        log.info(f'Loading the {args.device} configuration: {args.config}')
        ie.set_config({'CONFIG_FILE': args.config}, args.device)

# ---------------------------Step 2. Read a model in OpenVINO Intermediate Representation or ONNX format---------------
    log.info(f'Reading the network: {args.model}')
    # (.xml and .bin files) or (.onnx file)
    net = ie.read_network(model=args.model)

    if len(net.input_info) != 1:
        log.error('Sample supports only single input topologies')
        return -1
    if len(net.outputs) != 1:
        log.error('Sample supports only single output topologies')
        return -1

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

    # Get a number of input images
    num_of_input = len(args.input)
    # Get a number of classes recognized by a model
    num_of_classes = max(net.outputs[out_blob].shape)

# ---------------------------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, num_requests=num_of_input)

# ---------------------------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---------------------------------------------------------------------
    input_data = []
    _, _, h, w = net.input_info[input_blob].input_data.shape

    for i in range(num_of_input):
        image = cv2.imread(args.input[i])

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

        # Change data layout from HWC to CHW
        image = image.transpose((2, 0, 1))
        # Add N dimension to transform to NCHW
        image = np.expand_dims(image, axis=0)

        input_data.append(image)

# ---------------------------Step 7. Do inference----------------------------------------------------------------------
    log.info('Starting inference in asynchronous mode')
    for i in range(num_of_input):
        exec_net.requests[i].async_infer({input_blob: input_data[i]})

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

    # Create a list to control a order of output
    output_queue = list(range(num_of_input))

    while True:
        for i in output_queue:
            # Immediately returns a inference status without blocking or interrupting
            infer_status = exec_net.requests[i].wait(0)

            if infer_status == StatusCode.RESULT_NOT_READY:
                continue

            log.info(f'Infer request {i} returned {infer_status}')

            if infer_status != StatusCode.OK:
                return -2

            # Read infer request results from buffer
            res = exec_net.requests[i].output_blobs[out_blob].buffer
            # Change a shape of a numpy.ndarray with results to get another one with one dimension
            probs = res.reshape(num_of_classes)
            # 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('')

            output_queue.remove(i)

        if len(output_queue) == 0:
            break

# ----------------------------------------------------------------------------------------------------------------------
    log.info('This sample is an API example, '
             'for any performance measurements please use the dedicated benchmark_app tool\n')
    return 0
Ejemplo n.º 25
0
from .launcher import Launcher, LauncherConfigValidator
from .dlsdk_launcher_config import MULTI_DEVICE_KEYWORD, HETERO_KEYWORD, NIREQ_REGEX
from ..utils import get_or_parse_value, get_path

try:
    compile_args = cv2.compile_args
except AttributeError:
    try:
        compile_args = cv2.gapi.compile_args
    except AttributeError:
        def compile_args(*args):
            return list(map(cv2.GCompileArg, args))

try:
    from openvino.inference_engine import IECore # pylint:disable=W9902
    _ie_core = IECore()
except ImportError:
    _ie_core = None


try:
    from openvino.inference_engine import known_plugins # pylint:disable=W9902
except ImportError:
    known_plugins = []


class GAPILauncherConfigValidator(LauncherConfigValidator):
    def create_device_regex(self, available_devices):
        self.regular_device_regex = r"(?:^(?P<device>{devices})$)".format(devices="|".join(available_devices))
        self.hetero_regex = r"(?:^{hetero}(?P<devices>(?:{devices})(?:,(?:{devices}))*)$)".format(
            hetero=HETERO_KEYWORD, devices="|".join(available_devices)
Ejemplo n.º 26
0
def main():
    args = build_argparser().parse_args()
    profile = get_profile(args.profile)
    if args.block_size is None:
        sr = profile['model_sampling_rate']
        args.block_size = round(sr * 10) if not args.realtime else round(
            sr * profile['frame_stride_seconds'] * 16)

    log.info('OpenVINO Inference Engine')
    log.info('\tbuild: {}'.format(get_version()))
    ie = IECore()

    start_load_time = time.perf_counter()
    stt = DeepSpeechSeqPipeline(
        ie=ie,
        model=args.model,
        lm=args.lm,
        beam_width=args.beam_width,
        max_candidates=args.max_candidates,
        profile=profile,
        device=args.device,
        online_decoding=args.realtime,
    )
    log.debug(
        "Loading, including network weights, IE initialization, LM, building LM vocabulary trie: {} s"
        .format(time.perf_counter() - start_load_time))
    start_time = time.perf_counter()
    with wave.open(args.input, 'rb') as wave_read:
        channel_num, sample_width, sampling_rate, pcm_length, compression_type, _ = wave_read.getparams(
        )
        assert sample_width == 2, "Only 16-bit WAV PCM supported"
        assert compression_type == 'NONE', "Only linear PCM WAV files supported"
        assert channel_num == 1, "Only mono WAV PCM supported"
        assert abs(sampling_rate / profile['model_sampling_rate'] -
                   1) < 0.1, "Only {} kHz WAV PCM supported".format(
                       profile['model_sampling_rate'] / 1e3)
        log.debug("Audio file length: {} s".format(pcm_length / sampling_rate))

        audio_pos = 0
        play_start_time = time.perf_counter()
        iter_wrapper = tqdm if not args.realtime else (lambda x: x)
        for audio_iter in iter_wrapper(range(0, pcm_length, args.block_size)):
            audio_block = np.frombuffer(
                wave_read.readframes(args.block_size * channel_num),
                dtype=np.int16).reshape((-1, channel_num))
            if audio_block.shape[0] == 0:
                break
            audio_pos += audio_block.shape[0]
            #
            # It is possible to call stt.recognize_audio(): 1) for either whole audio files or
            # by splitting files into blocks, and 2) to reuse stt object for multiple files like this:
            #   transcription1 = stt.recognize_audio(whole_audio1, sampling_rate)
            #   transcription2 = stt.recognize_audio(whole_audio2, sampling_rate)
            #   stt.recognize_audio(whole_audio3_block1, sampling_rate, finish=False)
            #   transcription3 = stt.recognize_audio(whole_audio3_block2, sampling_rate, finish=True)
            # If you need intermediate features, you can call pipeline stage by stage: see
            # the implementation of DeepSpeechSeqPipeline.recognize_audio() method.
            #
            partial_transcr = stt.recognize_audio(audio_block,
                                                  sampling_rate,
                                                  finish=False)
            if args.realtime:
                if partial_transcr is not None and len(partial_transcr) > 0:
                    print('\r' +
                          partial_transcr[0].text[-args.realtime_window:],
                          end='')
                to_wait = play_start_time + audio_pos / sampling_rate - time.perf_counter(
                )
                if to_wait > 0:
                    time.sleep(to_wait)

    transcription = stt.recognize_audio(None, sampling_rate, finish=True)
    if args.realtime:
        # Replace the transcription with its finalized version for real-time mode
        if transcription is not None and len(transcription) > 0:
            print('\r' + transcription[0].text[-args.realtime_window:])
    else:  #  not args.realtime
        # Only show processing time in offline mode because real-time mode is being slowed down by time.sleep()

        total_latency = (time.perf_counter() - start_time) * 1e3
        log.info("Metrics report:")
        log.info("\tLatency: {:.1f} ms".format(total_latency))

    print("\nTranscription(s) and confidence score(s):")
    for candidate in transcription:
        print("{}\t{}".format(candidate.conf, candidate.text))
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)

    # create one inference request for asynchronous execution
    request_id = 0
    infer_request = exec_net.requests[request_id]

    num_iter = 10
    request_wrap = InferReqWrap(infer_request, request_id, num_iter)
    # Start inference request execution. Wait for last execution being completed
    request_wrap.execute("async", {input_blob: images})

    # Processing output blob
    log.info("Processing output blob")
    res = infer_request.outputs[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
    classid_str = "classid"
    probability_str = "probability"
    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]))
        print(classid_str, probability_str)
        print("{} {}".format('-' * len(classid_str),
                             '-' * len(probability_str)))
        for id in top_ind:
            det_label = labels_map[id] if labels_map else "{}".format(id)
            label_length = len(det_label)
            space_num_before = (7 - label_length) // 2
            space_num_after = 7 - (space_num_before + label_length) + 2
            space_num_before_prob = (11 - len(str(probs[id]))) // 2
            print("{}{}{}{}{:.7f}".format(' ' * space_num_before, det_label,
                                          ' ' * space_num_after,
                                          ' ' * space_num_before_prob,
                                          probs[id]))
        print("\n")
    log.info(
        "This sample is an API example, for any performance measurements please use the dedicated benchmark_app tool\n"
    )
Ejemplo n.º 28
0
# bin/setupvar.bat
#project in openvino
#pip install opencv-python
#https://download.01.org/opencv/2019/open_model_zoo/R1/models_bin/emotions-recognition-retail-0003/
#https://docs.openvinotoolkit.org/latest/_docs_install_guides_installing_openvino_raspbian.html

from openvino.inference_engine import IENetwork, IECore
#from Pillow import Image
net = IENetwork('C:\\Users\\maria\\Downloads\\face-detection-adas-0001.xml',
                'C:\\Users\\maria\\Downloads\\face-detection-adas-0001.bin')

ie = IECore()
ie.add_extension('cpu_extension_avx2.dll', "CPU")
exec_net = ie.load_network(net, 'CPU')

#"C:\Users\maria\Downloads\face-detection-adas-0001.xml"
import cv2

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

if vc.isOpened():  # try to get the first frame
    rval, frame = vc.read()
else:
    rval = False

while rval:
    cv2.imshow("preview", frame)
    rval, frame = vc.read()

    input = cv2.dnn.blobFromImage(frame, size=(672, 384))
Ejemplo n.º 29
0
    def load_model(self,
                   model,
                   device,
                   input_size,
                   output_size,
                   num_requests,
                   cpu_extension=None,
                   ie=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
        log.info("Initializing plugin for {} device...".format(device))
        self.ie = IECore()
        if cpu_extension and 'CPU' in device:
            self.ie.add_extension(cpu_extension, "CPU")

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

        if "CPU" in device:
            supported_layers = self.ie.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(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)

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

        log.info("Loading IR to the plugin...")
        if num_requests == 0:
            self.net_plugin = self.ie.load_network(network=self.net,
                                                   device_name=device)
        else:
            self.net_plugin = self.ie.load_network(network=self.net,
                                                   device_name=device,
                                                   num_requests=num_requests)

        return self.get_input_shape()
Ejemplo n.º 30
0
def test_get_metric_str():
    ie = IECore()
    param = ie.get_metric("CPU", "FULL_DEVICE_NAME")
    assert isinstance(param, str), "Parameter value for 'FULL_DEVICE_NAME' " \
                                   f"metric must be string but {type(param)} is returned"