Commit 7727ddfc by 杨林龙

OCR项目

0 parents
Showing 176 changed files with 68 additions and 0 deletions
/.idea
/.vscode
/target
/*.iml
from paddleocr import PaddleOCR, draw_ocr
from PIL import Image
import re
# load model
# Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改 lang参数进行切换
# lang参数依次为`ch`, `en`, `french`, `german`, `korean`, `japan`
ocr = PaddleOCR(
lang="ch",
# use_gpu=False,
use_gpu=True,
det_model_dir="ch_ppocr_server_v2.0_det_infer/",
cls_model_dir="ch_ppocr_mobile_v2.0_cls_infer/",
rec_model_dir="ch_ppocr_server_v2.0_rec_infer/",
)
class AutoIdentify(object):
"""
自动识别照片service
"""
def __init__(self, img_path):
self.img_path = img_path
def identify(self):
"""
识别照片
:return: 车牌号
"""
result = ocr.ocr(self.img_path, cls=True)
flattened_list = self.flatten_list(result)
for line in flattened_list:
print(line)
if self.validate_license_plate(str(line).replace("·", "")):
return str(line).replace("·", "")
def flatten_list(self, nested_list):
flattened_list = []
for item in nested_list:
if isinstance(item, list):
flattened_list.extend(self.flatten_list(item))
elif isinstance(item, tuple):
for value in item:
flattened_list.append(value)
else:
# flattened_list.append(item)
pass
return flattened_list
def validate_license_plate(self, plate_number):
pattern = (
r"^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}"
r"[A-HJ-NP-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$"
)
# pattern 是匹配车牌号的正则表达式
match = re.match(pattern, str(plate_number))
if match:
return True
else:
return False
No preview for this file type
This file is too large to display.
No preview for this file type
This diff is collapsed. Click to expand it.
No preview for this file type
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!