Пример #1
0
def predict(filename):

    channel = grpc.insecure_channel("localhost:8500")
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)

    request = predict_pb2.PredictRequest()
    # model_name
    request.model_spec.name = "cropper_model"
    # signature name, default is 'serving_default'
    request.model_spec.signature_name = "serving_default"
    start = time.time()
    """
    =========================================
    ===== Crop and align id card image
    =========================================
    """
    filepath = app.config["IMAGE_UPLOADS"] + "/" + filename
    # preprocess image
    img, original_image, original_width, original_height = preprocess_image(
        filepath, Cropper.TARGET_SIZE)
    if img.ndim == 3:
        img = np.expand_dims(img, axis=0)
    # request to cropper model
    request.inputs["input_1"].CopyFrom(
        tf.make_tensor_proto(img, dtype=np.float32, shape=img.shape))
    try:
        result = stub.Predict(request, 10.0)
        result = result.outputs["tf_op_layer_concat_14"].float_val
        result = np.array(result).reshape((-1, 9))

    except Exception as e:
        print(e)

    cropper = Cropper()
    cropper.set_best_bboxes(result,
                            original_width=original_width,
                            original_height=original_height,
                            iou_threshold=0.5)

    # respone to client if image is invalid
    if not cropper.respone_client(threshold_idcard=0.8):
        return render_template('upload_image_again.html')

    cropper.set_image(original_image=original_image)

    # output of cropper part
    aligned_image = getattr(cropper, "image_output")
    cv2.imwrite('app/static/aligned_images/' + filename, aligned_image)
    aligned_image = cv2.cvtColor(aligned_image, cv2.COLOR_BGR2RGB)
    """
    ===========================================
    ==== Detect informations in aligned image
    ===========================================
    """
    # preprocess aligned image
    original_height, original_width, _ = aligned_image.shape
    img = cv2.resize(aligned_image, Detector.TARGET_SIZE)
    img = np.float32(img / 255.)
    # model_name
    request.model_spec.name = "detector_model"
    # signature name, default is 'serving_default'
    request.model_spec.signature_name = "serving_default"

    if img.ndim == 3:
        img = np.expand_dims(img, axis=0)
    # new request to detector model
    request.inputs["input_1"].CopyFrom(
        tf.make_tensor_proto(img, dtype=np.float32, shape=img.shape))

    try:
        result = stub.Predict(request, 10.0)
        result = result.outputs["tf_op_layer_concat_14"].float_val
        result = np.array(result).reshape((-1, 13))

    except Exception as e:
        print(e)

    detector = Detector()
    detector.set_best_bboxes(result,
                             original_width=original_width,
                             original_height=original_height,
                             iou_threshold=0.5)
    detector.set_info_images(original_image=aligned_image)
    # output of detector part
    info_images = getattr(detector, "info_images")
    """
    =====================================
    ==== Reader infors from infors image
    =====================================
    """
    keys = list(info_images.keys())
    keys.remove("thoi_han")
    keys.remove("chan_dung")
    infors = dict()

    # init default value of quoc_tich, dan_toc
    infors['quoc_tich'] = ""
    infors['dan_toc'] = ""

    if "quoc_tich" in keys:
        infors['quoc_tich'] = ["Việt Nam"]
        keys.remove("quoc_tich")

    if "sex" in keys:
        info_image = info_images["sex"]
        infors["sex"] = list()
        for i in range(len(info_image)):
            img = info_image[i]['image']
            s = reader.predict(img)
            if "Na" in s:
                infors["sex"].append("Nam")
            else:
                infors["sex"].append("Nữ")
        keys.remove("sex")

    if "dan_toc" in keys:
        info_image = info_images["dan_toc"]
        infors["dan_toc"] = list()
        for i in range(len(info_image)):
            img = info_image[i]['image']
            s = reader.predict(img)
            s = s.split(" ")[-1]
            infors["dan_toc"].append(s)

        keys.remove("dan_toc")

    for key in keys:
        infors[key] = list()
        info_image = info_images[key]
        for i in range(len(info_image)):
            img = info_image[i]['image']
            s = reader.predict(img)
            infors[key].append(s)
    que_quan_0 = infors['que_quan'][0]
    que_quan_1 = ''
    noi_thuong_tru_0 = infors['noi_thuong_tru'][0]
    noi_thuong_tru_1 = ''
    if len(infors['que_quan']) == 2:
        que_quan_1 = infors['que_quan'][1]
    if len(infors['noi_thuong_tru']) == 2:
        noi_thuong_tru_1 = infors['noi_thuong_tru'][1]

    print("total_time:{}".format(time.time() - start))
    return render_template('predict.html',
                           id=infors['id'][0].replace(" ", ""),
                           full_name=infors['full_name'][0],
                           date_of_birth=infors['date_of_birth'][0],
                           sex=infors['sex'][0],
                           quoc_tich=infors['quoc_tich'],
                           dan_toc=infors['dan_toc'],
                           que_quan_0=que_quan_0,
                           que_quan_1=que_quan_1,
                           noi_thuong_tru_0=noi_thuong_tru_0,
                           noi_thuong_tru_1=noi_thuong_tru_1,
                           filename=str(filename))
Пример #2
0
    def predictcmnd(self, image):

        # check cccd
        """
        =========================================
        ===== Crop and align id card image
        =========================================
        """
        request = predict_pb2.PredictRequest()
        # model_name
        request.model_spec.name = "cropper_cmnd_model"
        # signature name, default is 'serving_default'
        request.model_spec.signature_name = "serving_default"
        # preprocess image
        img, original_image, original_width, original_height = preprocess_image(
            image, Cropper.TARGET_SIZE)
        if img.ndim == 3:
            img = np.expand_dims(img, axis=0)
        # request to cropper model
        request.inputs["input_1"].CopyFrom(
            tf.make_tensor_proto(img, dtype=np.float32, shape=img.shape))
        try:
            result = self.stub.Predict(request, 10.0)
            result = result.outputs["tf_op_layer_concat_14"].float_val
            result = np.array(result).reshape((-1, 9))

        except Exception as e:
            print("Cropper cmnd = ", e)

        cropper = Cropper()
        cropper.set_best_bboxes(result,
                                original_width=original_width,
                                original_height=original_height,
                                iou_threshold=0.5)

        # respone to client if image is invalid
        if cropper.respone_client(threshold_idcard=0.8) == -1:
            # print(cropper.respone_client(threshold_idcard=0.8))
            return
        elif cropper.respone_client(threshold_idcard=0.8) == 0:
            # print("no cropper")
            # cv2.imwrite('app/static/aligned_images/' + filename, original_image)
            aligned_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
        else:
            # print("cropper image")
            cropper.set_image(original_image=original_image)
            # print("Cropper cmnd end")
            # output of cropper part
            aligned_image = getattr(cropper, "image_output")
            cv2.imwrite('storage/c.jpg', aligned_image)
            aligned_image = cv2.cvtColor(aligned_image, cv2.COLOR_BGR2RGB)
        """
        ===========================================
        ==== Detect informations in aligned image
        ===========================================
        """
        # preprocess aligned image
        original_height, original_width, _ = aligned_image.shape
        img = cv2.resize(aligned_image, Detector.TARGET_SIZE)
        img = np.float32(img / 255.)
        # model_name
        request.model_spec.name = "detector_cmnd_model"
        # signature name, default is 'serving_default'
        request.model_spec.signature_name = "serving_default"

        if img.ndim == 3:
            img = np.expand_dims(img, axis=0)
        # new request to detector model
        request.inputs["input_1"].CopyFrom(
            tf.make_tensor_proto(img, dtype=np.float32, shape=img.shape))

        try:
            # print("Detect cmnd = ok")
            result = self.stub.Predict(request, 10.0)
            result = result.outputs["tf_op_layer_concat_14"].float_val
            result = np.array(result).reshape((-1, 13))
            # print("Detect cmnd = end")

        except Exception as e:
            print("Detect cmnd = ", e)

        detector = Detector()
        detector.set_best_bboxes(result,
                                 original_width=original_width,
                                 original_height=original_height,
                                 iou_threshold=0.5)
        detector.set_info_images(original_image=aligned_image)
        # output of detector part
        info_images = getattr(detector, "info_images")
        return info_images