网站怎么免费做推广,好品质高端网站设计新感觉建站,冷门行业做网站的优势,佛山网红打卡点对比了多种ocr识别算法#xff0c;最终选择了百度paddle官方的ocr算法 在所在的虚拟环境下运行
pip install paddleocr --userfrom paddleocr import PaddleOCR
import os
import csv# 创建 PaddleOCR 对象
ocr PaddleOCR(use_gpuTrue) # 无gpu时选择False# 指定图片文件夹…对比了多种ocr识别算法最终选择了百度paddle官方的ocr算法 在所在的虚拟环境下运行
pip install paddleocr --userfrom paddleocr import PaddleOCR
import os
import csv# 创建 PaddleOCR 对象
ocr PaddleOCR(use_gpuTrue) # 无gpu时选择False# 指定图片文件夹和结果保存文件夹的路径
image_folder ../页面截图/全部截图
result_folder ../提取结果/csv/all# 确保结果保存文件夹存在
if not os.path.exists(result_folder):os.makedirs(result_folder)# 用于计数处理的图片数量
count 0
max_count 2000 # 最多处理的图片数量# 遍历图片文件夹中的所有图片
for image_file in os.listdir(image_folder):if count max_count:breakif image_file.endswith((.png, .jpg, .jpeg)):image_path os.path.join(image_folder, image_file)results ocr.ocr(image_path, clsTrue)# 初始化行数据lines {}for line in results[0]:# 得到相应文本text line[1][0]# 根据您的计算方法计算平均 x 和 y 坐标x_avg sum([point[0] for point in line[0]]) / 4y_avg sum([point[1] for point in line[0]]) / 4# 寻找或创建相应的行found_row Falsefor key in lines:if abs(y_avg - key) 5: # 判断是否为同一行lines[key].append((x_avg, text))found_row Truebreakif not found_row:lines[y_avg] [(x_avg, text)]# 排序行和列sorted_lines sorted(lines.items(), keylambda x: x[0])for i, (y, items) in enumerate(sorted_lines):sorted_lines[i] sorted(items, keylambda x: x[0])# 写入 CSV 文件result_csv_path os.path.join(result_folder, os.path.splitext(image_file)[0] .csv)with open(result_csv_path, w, newline, encodingutf-8) as csvfile:csvwriter csv.writer(csvfile)for items in sorted_lines: # 修改此处csvwriter.writerow([text for _, text in items])print(f结果已保存到 {result_csv_path})count 1注paddleocr反馈回来文字内容以及包含文字的最小矩形四个点坐标。这里由于我的图很标准我选择将每个字段的xy坐标做一个平均按照xy进行排序生成对应的csv文件。 如果非标准我是横平竖直的电脑截图图片文件请自行修改判别算法