#!/home/anaconda3/envs/paddle_env/bin/python
import paddlehub as hub
import cv2
import re
def init():
#加载模型
ocr = hub.Module(name="chinese_ocr_db_crnn_server", enable_mkldnn=True) # mkldnn加速仅在CPU下有效
# return ocr[0]
return ocr
def recognize(path,name,ocr):
# 待预测图片
test_img_path = [path+name]
# 读取测试文件夹test.txt中的照片路径
np_images = [cv2.imread(image_path) for image_path in test_img_path]
results = ocr.recognize_text(
images=np_images, # 图片数据,ndarray.shape 为 [H, W, C],BGR格式;
use_gpu=False, # 是否使用 GPU;若使用GPU,请先设置CUDA_VISIBLE_DEVICES环境变量
output_dir=path+'/ocr_result', # 图片的保存路径,默认设为 ocr_result;
visualization=True, # 是否将识别结果保存为图片文件;
box_thresh=0.8, # 检测文本框置信度的阈值;
text_thresh=0.8) # 识别中文文本置信度的阈值;
# resultsData = ""
list = [] ## 空列表
for result in results:
data = result["data"]
for item in data:
if len(item["text"]) == 7 or len(item["text"]) == 8:
state = is_car_number(item["text"])
# confidence = item["confidence"]
if str(state)!=str(False):
# resultsData = '{"text": "' + state + '", "confidence":' + str(confidence) + '}'
del item["text_box_position"]
list.append(item)
return list
# 校验车牌
def is_car_number(string):
# 车牌正则
pattern_str = ["[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{1}(([A-HJ-Z]{1}[A-HJ-NP-Z0-9]{5}))",
"民航[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{1}(([A-HJ-Z]{1}[A-HJ-NP-Z0-9]{3}))",
"民航(([A-HJ-Z]{1}[A-HJ-NP-Z0-9]{4}))"]
cop = re.compile("[^\u4e00-\u9fa5^a-z^A-Z^0-9]")
string = cop.sub('', string)
if re.findall(pattern_str[0], string) or re.findall(pattern_str[1], string) or re.findall(pattern_str[2], string):
return string
else:
return False
评论0
最新资源