一、import openCV库,os库与重命名函数(详见[os/Python]基于os的大批量文件遍历重命名系统V1)
二、声明全局变量与列表
三、定义鼠标事件函数(鼠标从按下到弹起视为一次标注操作
四、设置文件路径
五、调用重命名函数
六、预览文件夹
七、标注系统运行
[os/Python]基于os的大批量文件遍历重命名系统V1https://blog.csdn.net/weixin_66896881/article/details/127657833
import cv2 as cv
import os
from rename_groundTruthBox_materials import rename
global x1_onmouse
global y1_onmouse
global x2_onmouse
global y2_onmouse
global tag_num
global x1
x1 = {}
global x2
x2 = {}
global y1
y1 = {}
global y2
y2 = {}
def onmouse(event, x, y, flags, param):
if event == cv.EVENT_LBUTTONDOWN: # 鼠标按下
global x1_onmouse
global y1_onmouse
x1_onmouse = x
y1_onmouse = y
if event == cv.EVENT_LBUTTONUP: # 鼠标弹起
global x2_onmouse
global y2_onmouse
global tag_num
x2_onmouse = x
y2_onmouse = y
tag_num += 1
# 交换坐标,得到左上和右下坐标
if x2_onmouse < x1_onmouse:
t = x1_onmouse
x1_onmouse = x2_onmouse
x2_onmouse = t
if y2_onmouse < y1_onmouse:
t = y1_onmouse
y1_onmouse = y2_onmouse
y2_onmouse = t
x1[tag_num] = x1_onmouse
y1[tag_num] = y1_onmouse
x2[tag_num] = x2_onmouse
y2[tag_num] = y2_onmouse
strtext1 = "(%s,%s)" % (x1_onmouse, y1_onmouse)
strtext2 = "(%s,%s)" % (x2_onmouse, y2_onmouse)
strtext3 = "(%s,%s)" % (x1_onmouse, y2_onmouse)
strtext4 = "(%s,%s)" % (x2_onmouse, y1_onmouse)
print("\ntag" + str(tag_num) + ":")
print(strtext1)
print(strtext2)
cv.circle(img, (x1_onmouse, y1_onmouse), 1, (0, 0, 255), -1)
cv.putText(img, strtext1, (x1_onmouse + 2, y1_onmouse + 15), cv.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 200), 1)
cv.circle(img, (x2_onmouse, y2_onmouse), 1, (0, 0, 255), -1)
cv.putText(img, strtext2, (x2_onmouse + 2, y2_onmouse + 15), cv.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 200), 1)
cv.circle(img, (x1_onmouse, y2_onmouse), 1, (0, 0, 255), -1)
cv.putText(img, strtext3, (x1_onmouse + 2, y2_onmouse + 15), cv.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 200), 1)
cv.circle(img, (x2_onmouse, y1_onmouse), 1, (0, 0, 255), -1)
cv.putText(img, strtext4, (x2_onmouse + 2, y1_onmouse + 15), cv.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 200), 1)
text="tag"+str(tag_num)
cv.putText(img, text, (x1_onmouse, y1_onmouse-5), cv.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 200), 1)
cv.imshow("img", cv.rectangle(img, (x1_onmouse, y1_onmouse), (x2_onmouse, y2_onmouse), (0, 0, 255), 1))
path_XML = 'D:\pythonProject4_camera\\XML'
path_img = 'D:\pythonProject4_camera\\beforeGroundTruthBoxImage'
# 批量重命名
rename(path_img)
xmlnames = os.listdir(path_XML) # 取出文件名列表
print("\n检测到" + path_XML + "下有" + str(len(xmlnames)) + '个文件') # 输出文件数量
imgnames = os.listdir(path_img) # 取出文件名列表
print("\n检测到" + path_img + "下有" + str(len(imgnames)) + '个文件') # 输出文件数量
print("\n进入遍历模式")
startimg = int(input("请输入开始的图片/XML序号:"))
while len(imgnames) < startimg or startimg < 1:
print("\n该数字不合法,请重新输入")
startimg = int(input("请输入开始的图片/XML序号:"))
for imgname in imgnames:
num = int(imgname.split(".")[0])
if num >= startimg:
x1 = {}
x2 = {}
y1 = {}
y2 = {}
print("\n当前正在标注第" + str(num) + "张图片")
imgpath = os.path.join(path_img, imgname) # 路径拼接
img = cv.imread(imgpath)
print("shape为" + str(img.shape))
cv.namedWindow("img")
print("操作顺序:拖曳鼠标以完成标注,请在每一次拖曳后输入标签,如blue")
cv.imshow("img", img)
global tag_num
tag_num = 0
cv.setMouseCallback("img", onmouse)
cv.waitKey(0)
global x1_onmouse
global y1_onmouse
global x2_onmouse
global y2_onmouse
for i in range(tag_num):
if i < tag_num:
print("\n正在显示tag")
strtextlu = "(%s,%s)" % (x1[i + 1], y1[i + 1])
strtextrd = "(%s,%s)" % (x2[i + 1], y2[i + 1])
tag = input(
"\n请输入tag" + str(i + 1) + strtextlu + "~" + strtextrd + "的内容(若输入reshow则重新显示tag):")
while (tag == "reshow"):
cv.imshow("tag", img)
cv.waitKey(0)
tag = input(
"\n请输入tag" + str(i + 1) + strtextlu + "~" + strtextrd + "的内容(若输入reshow则重新显示tag):")
xmlstorage = cv.FileStorage(path_XML + "\\" + str(num) + ".xml", cv.FILE_STORAGE_APPEND)
xmlstorage.write('numOfImage', num)
xmlstorage.write('numOfTag', i + 1)
xmlstorage.write('tag', tag)
xmlstorage.write('xmin', x1[i + 1])
xmlstorage.write('ymin', y1[i + 1])
xmlstorage.write('xmax', x2[i + 1])
xmlstorage.write('ymax', y2[i + 1])
xmlstorage.release()
cv.waitKey(0)
else:
print("\ntag已显示完毕")
cv.destroyAllWindows()
(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)
————————————————
版权声明:本文为CSDN博主「雕雀桑」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。