Exemplo n.º 1
0
def text_recognize(_image_info, _box_info):
    """
    文本识别

    Args:
        _image_info:    待识别的完整图像
        _box_info:      图像中文本区域的位置

    Returns:    文本区域位置的识别结果

    """
    to_return_result = {'text': ''}
    oss_handler = get_oss_handler()
    img = oss_handler.download_image_file(
        _image_info['bucket_name'],
        _image_info['path']
    )
    cropped_image = get_rotated_box_roi_from_image(img, _box_info)
    get_image_rotation = text_orientation_op.execute(cropped_image)
    if get_image_rotation['orientation'] == TextImageOrientation.ORIENTATION_90:
        rotated_image, _ = rotate_degree_img(cropped_image, 90)
    elif get_image_rotation['orientation'] == TextImageOrientation.ORIENTATION_180:
        rotated_image, _ = rotate_degree_img(cropped_image, 180)
    elif get_image_rotation['orientation'] == TextImageOrientation.ORIENTATION_270:
        rotated_image, _ = rotate_degree_img(cropped_image, 270)
    else:
        rotated_image = cropped_image
    recognize_result = text_recognize_op.execute(rotated_image)
    to_return_result['text'] = recognize_result['text']
    return to_return_result
Exemplo n.º 2
0
def ocr_result_visualization(_image_info, _box_info_list, _text_list):
    """
    将检测的结果和识别的结果合并到一张图中

    Args:
        _image_info:    图片信息
        _box_info_list:     所有box的列表
        _text_list:     跟box顺序一致的识别结果

    Returns:    合并后的图片的oss的路径

    """
    to_return_result = {'bucket_name': '', 'path': ''}
    oss_handler = get_oss_handler()
    img = oss_handler.download_image_file(_image_info['bucket_name'],
                                          _image_info['path'])
    result_image = annotate_detect_rotated_bbox_and_text_result(
        img, _box_info_list, _text_list, (0, 0, 255), 3)
    date_string = get_date_string()
    uuid_name = get_uuid_name()
    image_path = os.path.join(date_string, uuid_name)
    final_image_path = oss_handler.upload_image_file('result', image_path,
                                                     result_image, True, 50)
    to_return_result['bucket_name'] = 'result'
    to_return_result['path'] = final_image_path
    return to_return_result
Exemplo n.º 3
0
def text_detect(_image_info):
    """
    文本检测

    Args:
        _image_info:    待检测的图像信息

    Returns:    检测得到的所有box

    """
    to_return_result = {'box_info': [], 'box_count': 0}
    oss_handler = get_oss_handler()
    img = oss_handler.download_image_file(
        _image_info['bucket_name'],
        _image_info['path']
    )
    if max(img.shape[:2]) > 1024:
        candidate_img = resize_with_long_side(img, 1024)
    else:
        candidate_img = img
    detect_result = db_res18_op.execute(candidate_img)
    for m_box in detect_result['locations']:
        m_box_info = m_box['box_info']
        m_box_score = m_box['score']
        to_return_result['box_info'].append({
            'degree': m_box_info['degree'],
            'center_x': m_box_info['center_x'],
            'center_y': m_box_info['center_y'],
            'box_height': m_box_info['box_height'],
            'box_width': m_box_info['box_width'],
            'score': m_box_score,
        })
    to_return_result['box_count'] = len(detect_result['locations'])
    return to_return_result
Exemplo n.º 4
0
def face_parsing(_image_info, _face_box_info, _face_landmark_info):
    """
    人脸语义分区

    Args:
        _image_info:    待识别的完整图像
        _face_box_info:  人脸所在区域
        _face_landmark_info:    人脸landmark坐标信息

    Returns:    人脸不同区域的mask的key

    """
    to_return_result = {
        'parsing_info': {
            'bucket_name': '',
            'path': ''
        },
    }
    oss_handler = get_oss_handler()
    img = oss_handler.download_image_file(_image_info['bucket_name'],
                                          _image_info['path'])
    cropped_image = get_rotated_box_roi_from_image(img,
                                                   _face_box_info,
                                                   _scale_ratio=1.5)
    face_parsing_result = face_parsing_handler.execute(cropped_image,
                                                       _face_landmark_info)
    parsing_result = face_parsing_result['semantic_segmentation']
    date_string = get_date_string()
    name_string = get_uuid_name()
    target_path = os.path.join(date_string, name_string)
    target_path = oss_handler.upload_numpy_array('intermediate', target_path,
                                                 parsing_result)
    to_return_result['parsing_info']['bucket_name'] = 'intermediate'
    to_return_result['parsing_info']['path'] = target_path
    return to_return_result
Exemplo n.º 5
0
def download_image_from_url(_image_url):
    oss_helper = get_oss_handler()
    download_result = image_download_op.execute(_image_url,
                                                oss_helper,
                                                _image_size_threshold=None)
    return {
        'image_info': {
            'bucket_name': download_result['bucket_name'],
            'path': download_result['saved_path'],
            'height': download_result['image_height'],
            'width': download_result['image_width'],
            'channel': download_result['image_channel'],
        },
    }
Exemplo n.º 6
0
def face_detect(_image_info):
    """
    人脸检测

    Args:
        _image_info:    待识别的完整图像

    Returns:    检测到的人脸区域

    """
    to_return_result = {'face_count': 0, 'face_box_info': []}
    oss_handler = get_oss_handler()
    img = oss_handler.download_image_file(_image_info['bucket_name'],
                                          _image_info['path'])
    face_detect_result = face_detect_op.execute(img)
    to_return_result['face_count'] = len(face_detect_result['locations'])
    to_return_result['face_box_info'] = face_detect_result['locations']
    return to_return_result
Exemplo n.º 7
0
def text_recognize(_image_info, _box_info):
    """
    文本识别

    Args:
        _image_info:    待识别的完整图像
        _box_info:      图像中文本区域的位置

    Returns:    文本区域位置的识别结果

    """
    to_return_result = {'text': ''}
    oss_handler = get_oss_handler()
    img = oss_handler.download_image_file(_image_info['bucket_name'],
                                          _image_info['path'])
    cropped_image = get_rotated_box_roi_from_image(img, _box_info)
    recognize_result = text_recognize_op.execute(cropped_image)
    to_return_result['text'] = recognize_result['text']
    return to_return_result
Exemplo n.º 8
0
def face_liveness_detect(_image_info, _face_box_info):
    """
    静默人脸活体检测

    Args:
        _image_info:    待识别的完整图像
        _face_box_info:  人脸所在区域

    Returns:    人脸的特征向量

    """
    to_return_result = {"is_fake": False}
    oss_handler = get_oss_handler()
    img = oss_handler.download_image_file(_image_info['bucket_name'],
                                          _image_info['path'])
    cropped_image = get_rotated_box_roi_from_image(img, _face_box_info, 2.7)
    liveness_result = mini_fasnetv2_handler.execute(cropped_image)
    score = liveness_result['classification_scores']
    to_return_result['is_fake'] = (score[1] > score[0]) and (score[1] >
                                                             score[2])
    return to_return_result
Exemplo n.º 9
0
def face_embedding(_image_info, _face_box_info, _face_landmark_info):
    """
    人脸特征向量提取

    Args:
        _image_info:    待识别的完整图像
        _face_box_info:  人脸所在区域
        _face_landmark_info:    人脸landmark坐标信息

    Returns:    人脸的特征向量

    """
    to_return_result = {"face_feature_vector": []}
    oss_handler = get_oss_handler()
    img = oss_handler.download_image_file(_image_info['bucket_name'],
                                          _image_info['path'])
    cropped_image = get_rotated_box_roi_from_image(img, _face_box_info, 1.35)
    embedding_result = asia_face_embedding_handler.execute(
        cropped_image, _face_landmark_info)
    to_return_result['face_feature_vector'] = embedding_result[
        'feature_vector']
    return to_return_result
Exemplo n.º 10
0
def face_landmark(_image_info, _face_box_info):
    """
    人脸landmark检测

    Args:
        _image_info:    待识别的完整图像
        _face_box_info:  人脸所在区域

    Returns:    人脸landmark坐标

    """
    to_return_result = {
        'points_count': 106,
        'x_locations': [0] * 106,
        'y_locations': [0] * 106,
    }
    oss_handler = get_oss_handler()
    img = oss_handler.download_image_file(_image_info['bucket_name'],
                                          _image_info['path'])
    cropped_image = get_rotated_box_roi_from_image(img, _face_box_info, 1.35)
    landmark_detect_result = landmark106p_detect_handler.execute(cropped_image)
    to_return_result = landmark_detect_result.copy()
    return to_return_result