线上销售平台有哪些,seo免费,asp网站做消息提醒功能,公司网站制作费用初赛之特征构造 写在前面一、安装paddleOCR二、代码部分三、模型优缺点四、写在最后 写在前面 
通过前面两篇文章的介绍#xff0c;我们可以大致的知道模型用到的特征分为四块#xff1a;qCap#xff0c;qImg#xff0c;captions#xff0c;imgs。根据这些特征#xff0c… 初赛之特征构造 写在前面一、安装paddleOCR二、代码部分三、模型优缺点四、写在最后 写在前面 
通过前面两篇文章的介绍我们可以大致的知道模型用到的特征分为四块qCapqImgcaptionsimgs。根据这些特征我们得到的模型效果在0.7左右。是否能加入更多的特征进一步提升模型的效果呢 
通过数据分析我们发现了部分图片中存在文字且具有判断文本类别的作用。所以本文采用paddleocr模型来提取图像中的文字特征。 
一、安装paddleOCR 
在安装paddleOCR前需要安装依赖组件Shapely 
pip install Shapely接下来就可以安装paddleOCR了也是一行代码就完成安装 
pip install --user paddleocr -i https://mirror.baidu.com/pypi/simple接下来我们就可以进行测试了 
from paddleocr import PaddleOCR
import os
os.environ[KMP_DUPLICATE_LIB_OK]TRUEcaptions_list  []
# Paddleocr目前支持中英文、英文、法语、德语、韩语、日语可以通过修改lang参数进行切换
# 参数依次为ch, en, french, german, korean, japan。
ocr  PaddleOCR(use_angle_clsTrue, langch)  # need to run only once to download and load model into memory
caption  []
img_path  0.jpg
result  ocr.ocr(img_path, clsTrue)
for idx in range(len(result)):res  result[idx]print(res)for line in res:if line[1][1]0.9:   # line[1][1]是提取文本的置信度print(line[1][0])   # line[1][0]是提取文本# 显示结果
from PIL import Image
result  result[0]
image  Image.open(img_path).convert(RGB)
boxes  [line[0] for line in result]
txts  [line[1][0] for line in result]
scores  [line[1][1] for line in result]
im_show  draw_ocr(image, boxes, txts, scores, font_path/path/to/PaddleOCR/doc/fonts/simfang.ttf)
im_show  Image.fromarray(im_show)
im_show.save(result.jpg)测试结果如下  可以发现识别效果还是不错的。 
paddleOCR以ppocr轻量级模型作为默认模型如果你想尝试更多可以参考以下链接的第3节自定义模型进行自定义更换。 https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.6/doc/doc_ch/whl.md 
二、代码部分 
运行该部分代码可以得到train、test、val各个img文件夹中图片中的文字一行文字代表一张图片。 
#读取数据
import json
from paddleocr import PaddleOCR
import os
os.environ[KMP_DUPLICATE_LIB_OK]TRUEdata_items_train  json.load(open(queries_dataset_merge/dataset_items_train.json,r,encodingUTF8))
data_items_val  json.load(open(queries_dataset_merge/dataset_items_val.json,r,encodingUTF8))
data_items_test  json.load(open(queries_dataset_merge/dataset_items_test.json,r,encodingUTF8))# 写入txt文件
def load_ocr_captions(context_data_items_dict,queries_root_dir,split):if split  train:fname  ocr/ocr_qimg_train.txtif split  val:fname  ocr/ocr_qimg_val.txtif split  test:fname  ocr/ocr_qimg_test.txt# image_path  os.path.join(queries_root_dir,fname)# Paddleocr目前支持中英文、英文、法语、德语、韩语、日语可以通过修改lang参数进行切换# 参数依次为ch, en, french, german, korean, japan。with open(fname, w, encodingUTF8) as f:for key in range(len(context_data_items_dict)):print(key)captions_list  []image_path  os.path.join(queries_root_dir, context_data_items_dict[str(key)][image_path])ocr  PaddleOCR(use_angle_clsTrue, langch,show_logFalse)  # need to run only once to download and load model into memoryresult  ocr.ocr(image_path, clsTrue)for idx in range(len(result)):res  result[idx]for line in res:if line[1][1]  0.8:  # 置信度captions_list.append(line[1][0])captions  ,.join(captions_list)f.write(captions\n)#### load Datasets ####
train_dump_ocr_captions load_ocr_captions(data_items_train, queries_dataset_merge,train)
val_dump_ocr_captions  load_ocr_captions(data_items_val,queries_dataset_merge,val)
test_dump_ocr_captions  load_ocr_captions(data_items_test,queries_dataset_merge,test)三、模型优缺点 
优点是模型识别的准确率较高缺点是模型不能多线程跑读完整个数据集耗时1day。建议在入模前就通过ocr采集存储每个图片的文字后续调用直接通过图片id匹配即可。 
四、写在最后 
文本主要展现用什么方法来做数据特征加工对baseline改动的代码就不贴了想要的uu们可以私信我。 
本次记录主要还是以学习为主抽了工作之余来进行OCR特征加工。探索了一个带大家最快上手的路径降低大家的入门难度。 
看完觉得有用的话记得点个赞不做白嫖党~