python --飞浆离线ocr使用/paddleocr
python 3.10.3 和 飞浆ocr 3.2使用依赖paddlepaddle3.2.0paddleocr3.2.0运行测试paddleocr ocr-i https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_002.png--use_doc_orientation_classifyFalse--use_doc_unwarpingFalse--use_textline_orientationFalse代码frompaddleocrimportPaddleOCRimportcv2 ocrPaddleOCR(use_doc_orientation_classifyFalse,# 文档整体方向分类 比如你拍的合同是 90° 横过来的、扫描件是 180° 倒过来的开启后会自动把文档旋转回正常方向再识别。use_doc_unwarpingFalse,# 文档弯曲矫正 比如拍的书本内页有弧度、褶皱的纸质文档拍照后文字变形开启后会先把弯曲的文档还原成平整的再识别文字。use_textline_orientationFalse,# 文档里既有横排文字又有竖排文字某一行文字是 90° 旋转的开启后会逐行矫正文字方向避免把「竖排的字」识别成乱码。text_detection_model_dirrC:\Users\123\.paddlex\official_models\PP-OCRv5_server_det,text_detection_model_namePP-OCRv5_server_det,text_recognition_model_dirrC:\Users\123\.paddlex\official_models\en_PP-OCRv5_mobile_rec,text_recognition_model_nameen_PP-OCRv5_mobile_rec,text_det_limit_side_len300,# 核心适中尺寸平衡精度和速度text_det_limit_typemin# 缩放最小边到736放大小数字区域#ocr_versionPP-OCRv5,#langen,)regions[[[219,196],[721,196],[721,399],[219,399]]]x[x[0]foriinregionsforxini]y[x[1]foriinregionsforxini]min_x,min_ymin(x),min(y)max_x,max_ymax(x),max(y)imagecv2.imread(rC:\Users\123\Desktop\12.png)imageimage[min_y:max_y,min_x:max_x]imagecv2.copyMakeBorder(image,top50,# 上边框50像素bottom50,# 下边框50像素left50,# 左边框50像素right50,# 右边框50像素borderTypecv2.BORDER_CONSTANT,# 固定颜色边框value(255,255,255)# 边框颜色纯白色)cv2.imshow(,image)cv2.waitKey(0)resocr.predict(image)foriinres:print(i[rec_texts])classFeijiangOcr(object):飞浆ocrdef__init__(self,weights_file):self.ocrPaddleOCR(text_detection_model_diros.path.join(weights_file,PP-OCRv5_server_det),text_detection_model_namePP-OCRv5_server_det,# 文字检测模型text_recognition_model_diros.path.join(weights_file,en_PP-OCRv5_mobile_rec),text_recognition_model_nameen_PP-OCRv5_mobile_rec,# 文字识别text_recognition_batch_size1,# 识别阶段的批量推理批次大小doc_orientation_classify_model_diros.path.join(weights_file,PP-LCNet_x1_0_doc_ori),# 文档方向分类doc_orientation_classify_model_namePP-LCNet_x1_0_doc_ori,doc_unwarping_model_diros.path.join(weights_file,UVDoc),# 文字矫正doc_unwarping_model_nameUVDoc,textline_orientation_model_diros.path.join(weights_file,PP-LCNet_x1_0_textline_ori),# 单行文本方向分类 单行文字旋转问题textline_orientation_model_namePP-LCNet_x1_0_textline_ori,textline_orientation_batch_size1,# ocr_versionPP-OCRv5,# langen,)defextract_min_rectangle(self,regions:list):提取最小矩形,且检测框在最小矩形内x[x[0]foriinregionsforxini]y[x[1]foriinregionsforxini]min_x,min_ymin(x),min(y)max_x,max_ymax(x),max(y)returnmin_x,min_y,max_x,max_ydefcrop_img__add_border(self,image,regions):裁剪图并加边框(不加边框识别时因为文字太大导致不能识别)x1,y1,x2,y2self.extract_min_rectangle(regions)crop_imageimage[y1:y2,x1:x2]# 裁剪图crop_imagecv2.copyMakeBorder(crop_image,top50,# 上边框50像素bottom50,# 下边框50像素left50,# 左边框50像素right50,# 右边框50像素borderTypecv2.BORDER_CONSTANT,# 固定颜色边框value(255,255,255)# 边框颜色纯白色)returncrop_imagedefocr_predict(self,image,regions,scores_threshold):识别ifregions:imageself.crop_img__add_border(image,regions)# 裁剪图并加边框resself.ocr.predict(image,use_doc_orientation_classifyFalse,# 文档整体方向分类 比如你拍的合同是 90° 横过来的、扫描件是 180° 倒过来的开启后会自动把文档旋转回正常方向再识别。use_doc_unwarpingFalse,# 文档弯曲矫正 比如拍的书本内页有弧度、褶皱的纸质文档拍照后文字变形开启后会先把弯曲的文档还原成平整的再识别文字。use_textline_orientationFalse,# 文档里既有横排文字又有竖排文字某一行文字是 90° 旋转的开启后会逐行矫正文字方向避免把「竖排的字」识别成乱码。text_det_limit_side_len300,# 核心适中尺寸平衡精度和速度text_det_limit_typemin,# 缩放最小边到300放大小数字区域text_det_thresh0.3,# 判定「单个像素是否属于文字区域」的置信度text_det_box_thresh0.5,# 判定「整个文字框是否有效」的置信度临界值text_det_unclip_ratio1.5,# 检测框的膨胀系数 检测出的文本框通常偏小值越大框越大text_rec_score_threshscores_threshold,# 判定「识别结果是否有效」的临界值return_word_boxFalse,# 是否返回单字级别的检测框)text.join(res[0][rec_texts])# 识别结果logger.success(focr结果:【{text}】)returntext通过网盘分享的文件飞浆V5模型链接:https://pan.baidu.com/s/1TJUUmbZbQF_HMtRmNCC6LA提取码:1111依赖# python3.7.3paddleocr2.7.0.2paddlepaddle2.5.2loguru0.7.3frompaddleocrimportPaddleOCRimportcv2importnumpyasnpif__name____main__:OCRPaddleOCR(use_doc_orientation_classifyFalse,# 检测文档方向use_doc_unwarpingFalse,# 矫正扭曲文档use_textline_orientationFalse)# 识别文本行方向image_pathrC:\Users\123\Desktop\2.jpgimagecv2.imread(image_path)resultOCR.ocr(image_path,clsFalse)forboxinresult[0]:print(f区域:【{box[0]}】,结果:【{box[1]}】)pointsnp.array(box[0],dtypenp.int32).reshape((-1,1,2))cv2.polylines(image,[points],isClosedTrue,color(0,0,255),thickness1)cv2.imshow(,image)cv2.waitKey(0)cv2.destroyAllWindows()源码下载链接: https://pan.baidu.com/s/1RwxzllNdwhiICHKGpIu2wQ 提取码: cvfr
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2419963.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!