mian03.py
4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import glob
import os
from math import sqrt
import cv2
import numpy as np
from paddleocr import PaddleOCR, draw_ocr
from PIL import Image
from pyzbar.pyzbar import decode
# load model
# Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改 lang参数进行切换
# lang参数依次为`ch`, `en`, `french`, `german`, `korean`, `japan`
ocr = PaddleOCR(
use_angle_cls=True,
# use_angle_cls=False,
lang="ch",
# use_gpu=False,
use_gpu=True,
use_tensorrt=True,
det_algorithm="DB",
# det_algorithm="EAST",
det_db_score_mode="slow",
# use_space_char=False, #不可用
det_model_dir="ch_ppocr_server_v4.0_det_infer/",
cls_model_dir="ch_ppocr_mobile_v4.0_cls_infer/",
rec_model_dir="ch_ppocr_server_v4.0_rec_infer/",
det_db_thresh=0.1,
det_db_box_thresh=0.1,
det_db_unclip_ratio=2.0,
max_batch_size=20,
use_dilation=True,
# rec_algorithm="SVTR_LCNet",
)
# load dataset
# img_path = "./08631508_2.jpg"
# img_path = "./image/baidu_image/test1.jpg"
# img_path = "./train/threecode/file_1704852415_6623.jpg"
# 构建文件匹配模式
file_pattern = os.path.join("./train/threecode", "*")
# 获取文件夹下的所有文件
file_list = glob.glob(file_pattern)
def flatten_list(nested_list):
flattened_list = []
for item in nested_list:
if isinstance(item, list):
flattened_list.extend(flatten_list(item))
elif isinstance(item, tuple):
for value in item:
if type(value) in (int, float):
if value > 1.0:
flattened_list.append(value)
else:
flattened_list.append(value)
else:
# flattened_list.append(item)
pass
return flattened_list
# 打印文件列表
for image_path in file_list:
original_image = Image.open(image_path)
# 放大图片
scale_factor = 5 # 放大倍数
# scaled_image = original_image.resize(
# (original_image.width * scale_factor, original_image.height * scale_factor)
# )
dst = original_image.resize(
(original_image.width * scale_factor, original_image.height * scale_factor)
)
# 裁剪放大后的图片
# crop_left = scaled_image.width // 2 - original_image.width // 2 # 裁剪左上角 x 坐标
# crop_top = scaled_image.height // 2 - original_image.height // 2 # 裁剪左上角 y 坐标
# # crop_width = original_image.width # 裁剪宽度
# # crop_height = original_image.height # 裁剪高度
# crop_width = original_image.width * 3 / 4 # 裁剪宽度
# crop_height = original_image.height * 3 / 4 # 裁剪高度
# dst = scaled_image.crop(
# (crop_left, crop_top, crop_left + crop_width, crop_top + crop_height)
# )
# if os.path.exists(image_path.replace("train", "validation")):
# os.remove(image_path.replace("train", "validation"))
# dst.save(image_path.replace("train", "validation"))
# original_image = cv2.imread(img_path)
# # 获取原始图像尺寸
# height, width, _ = original_image.shape
# print(height, width)
# # 将图像放大两倍
# scaled_image = cv2.resize(original_image, (2 * width, 2 * height))
# # 计算裁剪区域的起始坐标
# x = width // 2 # 起始横坐标
# y = height // 2 # 起始纵坐标.
# print(y + height, x + width)
# cropped_image = scaled_image[y : y + height, x : x + width] # 进行裁剪
# dst = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB) # 将图像从BGR转换为RGB
# cv2.imshow("Image", dst)
# result = ocr.ocr(image_path, cls=True)
# 检测条形码
# dst = cv2.imread(image_path)
# barcodes = decode(dst)
# # 遍历条形码区域并进行遮盖
# for barcode in barcodes:
# x, y, w, h = barcode.rect
# cv2.rectangle(np.asarray(dst), (x, y), (x + w, y + h), (0, 0, 0), -1)
# if os.path.exists(image_path.replace("train", "validation")):
# os.remove(image_path.replace("train", "validation"))
# dst.save(image_path.replace("train", "validation"))
# result = ocr.ocr(image_path, cls=True)
result = ocr.ocr(np.asarray(dst), cls=True)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for line in result:
print(line)
# print(line[0][1][0])
result_list = flatten_list(result)
print("result_list", result_list)
with open("./log06.txt", "a+", encoding="utf-8") as file:
if result_list is not None:
file.write(image_path + "----" + str(result_list) + "\n")
else:
file.write(image_path + "----" + "NULL" + "\n")