网上找不到,方便大家,自己写一个。
paddleOCR 自动标注cls
鼠标单点击切换
键盘q下一个
鼠标中键退出
dir_path:图形所在文件
label_path: 标记labeltxt文件
import os
import cv2
from shutil import copyfile
'''
paddleOCR 自动标注cls
单点击切换
中键退出
dir_path:图形所在文件
label_path: 标记labeltxt文件
'''
# 待搜索的目录路径
dir_path = r"E:\0_study\13_paddle\PaddleOCR-release-2.5\train_data\cls\train"
label_path = r"E:\0_study\13_paddle\PaddleOCR-release-2.5\train_data\cls\train.txt"
img_list_path = []
cv2.namedWindow("show", 0)
cv2.resizeWindow("show", 800, 600)
font = cv2.FONT_HERSHEY_SIMPLEX
def on_mouse(event, x, y, flags, param):
global img, data, q
img2 = img.copy()
if event == cv2.EVENT_LBUTTONDOWN: # 左键点击
if data == 0:
data = 180
cv2.putText(img2, "cls: " + str(data), (30, 30), font, 1, (0, 0, 255), 2)
cv2.imshow("show", img2)
else:
data = 0
cv2.putText(img2, "cls: " + str(data), (30, 30), font, 1, (0, 0, 255), 2)
cv2.imshow("show", img2)
if event == cv2.EVENT_MBUTTONDBLCLK: # 左键点击
q = 0
cv2.setMouseCallback('show', on_mouse)
# elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON): # 按住左键拖曳
# cv2.rectangle(img2, point1, (x, y), (255, 0, 0), 5)
# cv2.imshow('image', img2)
q = 1
with open(label_path, 'w', encoding='utf-8') as f:
for file in os.listdir(dir_path):
# 利用os.path.join()方法取得路径全名,并存入cur_path变量,否则每次只能遍历一层目录
cur_path = os.path.join(dir_path, file)
img = cv2.imread(cur_path)
img = cv2.resize(img, (800, 600))
data = 0
img2 = img.copy()
cv2.putText(img2, "cls: " + str(data), (30, 30), font, 1, (0, 0, 255), 2)
cv2.imshow("show", img2)
cv2.waitKey(1)
while 1:
if (cv2.waitKey(1) == ord('q')) or (q == 0):
break
f.write(cur_path + "\t" + str(data) + "\n")
if q == 0:
break
# 判断是否是文件夹
f.close()