import requests
from fastapi import FastAPI
from paddleocr import PaddleOCR, draw_ocr
from io import BytesIO
from PIL import Image
import os
import re
from enum import Enum
class contractType(Enum):
Display = 0
Contract = 1
Idcard = 3
IdcardSide = 4
Signature = 5
Lisense = 6
ocr = PaddleOCR(use_angle_cls=True, use_gpu=False, lang="ch")
app = FastAPI()
def carvin():
pass
def carnumber():
pass
def display():
pass
def contract():
pass
def idcard(a):
gender_ethnicity_pattern = re.compile(r'性别(\w+)民族(\w+)')
id_card_pattern = re.compile(r'(\d{17}[\dXx])')
gender_ethnicity_match = gender_ethnicity_pattern.search(a)
id_card_match = id_card_pattern.search(a)
gender = ''
ethnicity = ''
id_card = ''
if gender_ethnicity_match:
gender = gender_ethnicity_match.group(1)
ethnicity = gender_ethnicity_match.group(2)
print("性别:", gender)
print("民族:", ethnicity)
if id_card_match:
id_card = id_card_match.group(1)
print("身份证号:", id_card)
return [gender, ethnicity, id_card]
def licenseside(a):
pattern_company_name = re.compile(r'名\s+称\s+(\S.+?)\s+类')
pattern_credit_code = re.compile(r'统一社会信用代码\s+(\S+)\(')
pattern_business_address = re.compile(r'经营场所\s+(\S.+?)\s+组成')
pattern_operator_name = re.compile(r'经\s+营\s+者\s+(\S+)')
company_name_match = pattern_company_name.search(a)
credit_code_match = pattern_credit_code.search(a)
business_address_match = pattern_business_address.search(a)
operator_name_match = pattern_operator_name.search(a)
company_name = ''
credit_code = ''
business_address = ''
operator_name = ''
if company_name_match:
company_name = company_name_match.group(1)
if credit_code_match:
credit_code = credit_code_match.group(1)
if business_address_match:
business_address = business_address_match.group(1)
if operator_name_match:
operator_name = operator_name_match.group(1)
return [company_name, credit_code, business_address, operator_name]
def signature():
pass
@app.get("/")
async def root(url: str, type: int):
try:
response = requests.get(url, stream=True)
response.raise_for_status()
if 'image' not in response.headers.get('content-type', ''):
return {"error": "The provided URL does not point to an image."}, 400
image_bytes = BytesIO(response.content)
image = Image.open(image_bytes)
temp_image_path = "temp_image.jpg"
with open(temp_image_path, "wb") as image_file:
image.save(image_file, format='JPEG')
result = ocr.ocr(temp_image_path, cls=True)
if os.path.exists(temp_image_path):
os.remove(temp_image_path)
results = ""
for item in result:
for sub_item in item:
text = sub_item[1][0]
probability = sub_item[1][1]
results = results + ' ' + text
if type == contractType.Idcard.value:
return {"error": 0, "message": idcard(results), "type": type}
if type == contractType.Lisense.value:
return {"error": 0, "message": licenseside(results), "type": type}
return {"error": 0, "message": results, "type": type}
except requests.exceptions.RequestException as e:
return {"error": f"An error occurred while downloading the image: {str(e)}"}, 500
except Exception as e:
return {"error": f"An error occurred during OCR processing: {str(e)}"}, 500