-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabel-ocr-ds.py
More file actions
86 lines (70 loc) · 2.91 KB
/
Copy pathlabel-ocr-ds.py
File metadata and controls
86 lines (70 loc) · 2.91 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
import os
import cv2
def process_images(folder_path):
image_extensions = ['.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.webp']
image_files = [
os.path.join(folder_path, f) for f in os.listdir(folder_path)
if os.path.splitext(f)[1].lower() in image_extensions
]
if not image_files:
print("文件夹中没有找到图片文件!")
return
output_file = os.path.join(folder_path, "image_labels.txt")
results = []
for idx, img_path in enumerate(image_files):
img = cv2.imread(img_path)
if img is None:
print(f"无法读取图片: {img_path}")
continue
window_name = f"Image Labeling ({idx+1}/{len(image_files)})"
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
label = None
display_img = img.copy()
while True:
# 显示当前图片和提示信息
cv2.imshow(window_name, display_img)
key = cv2.waitKey(0)
# ESC 键退出程序
if key == 27:
cv2.destroyAllWindows()
print("用户中断操作")
return
# 空格键标记为空
if key == 32:
label = " "
break
# 输入第一个数字
if 48 <= key <= 57 and label is None:
first_digit = chr(key)
# 在图片上显示已输入的第一个数字
display_img = img.copy()
cv2.putText(display_img, f"First Digit: {first_digit}", (20, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.putText(display_img, "Press second digit...", (20, 100),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), 2)
cv2.imshow(window_name, display_img)
# 等待第二个数字
second_key = cv2.waitKey(0)
if 48 <= second_key <= 57:
second_digit = chr(second_key)
label = first_digit + second_digit
break
elif second_key == 27: # 第二个输入按ESC取消
display_img = img.copy()
continue
else: # 非数字输入重置
display_img = img.copy()
# 记录结果
results.append(f"{img_path}\t{label}")
cv2.destroyWindow(window_name)
print(f"已处理: {img_path} -> {label}")
# 保存结果到文件
with open(output_file, 'w', encoding='utf-8') as f:
f.write("\n".join(results))
print(f"标注完成!结果已保存到: {output_file}")
if __name__ == "__main__":
folder = r"C:\Users\qixin\Desktop\test-blue"
if os.path.isdir(folder):
process_images(folder)
else:
print("无效的文件夹路径!")