import cv2
from paddleocr import PaddleOCR
from core.parser import FrontParser
from core.direction import *
from core.ocr import IdCardOcr
import requests
import base64

def send_request():
    img_str: str = base64.encodebytes(open('images/front-180.png', 'rb').read()).decode('utf-8')
    print(img_str)
    r = requests.post('http://localhost:8080/ocr_system/idcard', json={'image': img_str, 'image_type': '0'})
    print(r.json())


# 初始化ocr模型和后处理模型
ocr = PaddleOCR(use_angle_cls=True, rec_model_dir="./idcard_rec_infer/",
                det_model_dir="./idcard_det_infer/",
                cls_model_dir="idcard_cls_infer",
                rec_algorithm='CRNN',
                ocr_version='PP-OCRv2',
                rec_char_dict_path="./ppocr_keys_v1.txt", lang="ch", use_gpu=False)

# 定义文件路径
img_path = "images/front-270.png"
image = cv2.imread(img_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# 正面
image_type = '0'

m = IdCardOcr(ocr)
res = m .predict(image, image_type)
print(res)

if __name__ == '__main__':
    send_request()