This commit is contained in:
wangjialiang
2025-11-12 17:18:45 +08:00
commit 343677f2a1
7 changed files with 129 additions and 0 deletions

5
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,5 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/

View File

@@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

10
.idea/label_me1.iml generated Normal file
View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.13" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

7
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.13 (label_me1)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.13" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/label_me1.iml" filepath="$PROJECT_DIR$/.idea/label_me1.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

87
label_me1.py Normal file
View File

@@ -0,0 +1,87 @@
import os
import xml.etree.ElementTree as ET
import cv2
dirpath = r'E:\Train\EM14XML' # 原来存放xml文件的目录
newdir = r'E:\Train\EM14TXT' # 修改label后形成的txt目录
imagepath = r"E:\Train\EM14IMG"
if not os.path.exists(newdir):
os.makedirs(newdir)
for fp in os.listdir(dirpath):
print(fp)
print(fp.split('.')[0])
imagename = fp.split('.')[0] + '.tiff'
imagename_path = os.path.join(imagepath,imagename)
print(imagename_path)
image = cv2.imread(imagename_path)
# cv2.imshow('image',image)
# cv2.waitKey(0)
if fp == '.DS_Store':
continue
root = ET.parse(os.path.join(dirpath, fp)).getroot() #获取xml的根节点
xmin, ymin, xmax, ymax = 0, 0, 0, 0
sz = root.find('size')
print(sz)
width = float(sz[0].text)
height = float(sz[1].text)
filename = root.find('filename').text
for child in root.findall('object'): # 找到图片中的所有框
print(child)
sub = child.find('bndbox') # 找到框的标注值并进行读取
xmin = float(sub[0].text)
ymin = float(sub[1].text)
xmax = float(sub[2].text)
ymax = float(sub[3].text)
try: # 转换成yolov3的标签格式需要归一化到0-1的范围内
x_center = (xmin + xmax) / (2 * width)
y_center = (ymin + ymax) / (2 * height)
w = (xmax - xmin) / width
h = (ymax - ymin) / height
except ZeroDivisionError:
print(filename, '的 width有问题')
sub_class = str(child.find('name').text)
if sub_class == 'EM17':
cls = 2
elif sub_class == 'EM14':
cls = 0
elif sub_class == 'EM170':
cls = 3
elif sub_class == 'EM18':
cls = 1
elif sub_class == 'EM19':
cls = 4
elif sub_class == 'EM190':
cls = 5
elif sub_class == 'EM20':
cls = 6
elif sub_class == 'EM200':
cls = 7
elif sub_class == 'EM201':
cls = 8
elif sub_class == 'EM202':
cls = 9
elif sub_class == 'EM203':
cls = 10
elif sub_class == 'EM180':
cls = 11
elif sub_class == 'EM141':
cls = 12
else:
continue
# cv.namedWindow('rect', 1)
# cv.imshow('rect', image)
# cv.waitKey(0)
with open(os.path.join(newdir, fp.split('.')[0] + '.txt'), 'a+') as f:
f.write(' '.join([str(cls), str(x_center), str(y_center), str(w), str(h) + '\n']))
cv2.rectangle(image, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (0, 0, 255), 2)
cv2.putText(image, str(cls), (200, 200), cv2.FONT_HERSHEY_COMPLEX, 5.0, (100, 200, 200), 2)
cv2.imwrite('E:\\Train\\EM14Result\\' + imagename, image)