主要流程:
读取标签文件数据,打开图像,利用opencv-python库,在原图中画出标签的bbox检测框,并可以保存。
先说明一下我们的文件结构:
训练集:我们在案例中将训练集目录命名为images
然后训练集目录内全部都是jpg图像。
例如:
|-- images
| |-- 000000000009.jpg
| |-- 000000000025.jpg
| |-- 000000000030.jpg
| |-- ...
训练集标签:我们在案例中将训练集目录命名为labels
然后训练集标签目录内全部都是txt文件。
|-- labels
| |-- 000000000009.txt
| |-- 000000000025.txt
| |-- 000000000030.txt
| |-- ...
本问代码读取的标签格式是yolo格式的
txt标签内容格式例如:
0 0.3125 0.179104 0.0453125 0.0985075
简单介绍:
每行一共5个数据,数据与数据之间空格隔开。
第一个数据0表示分类的类别序号
第二个数据0.3125表示x_center/width,即检测框中点的x坐标除以图像宽度。
第三个数据0.179104表示y_center/height,即检测框中点的y坐标除以图像高度。
第四个数据0.0453125表示roi_width/width。即检测框的宽度除以图像宽度。
第五个数据0.0985075表示roi_width/width。即检测框的高度除以图像高度。
然后我们的代码如下:
具体路径根据实际情况而定
import shutil
import os
import cv2
import re
def main():
# images-dir
path_image = './1/images'
# labels-dir
path_labels = './1/labels'
type_object = '.txt'
# 画框效果图的输出目录
output_images = './hands/images'
output_labels = './hands/labels'
history_name = []
for ii in os.walk(path_image):
for j in ii[2]:
type = j.split(".")[1]
if type != 'jpg':
continue
path_img = os.path.join(path_labels, j)
label_name = j[:-4] + type_object
path_label = os.path.join(path_labels, label_name)
if os.path.exists(path_label) != True:
continue
f = open(path_label, 'r+', encoding='utf-8')
print(path_img)
img = cv2.imread(path_img)
w = img.shape[1]
h = img.shape[0]
new_lines = []
hands_lines = []
while True:
line = f.readline()
if line:
img_tmp = img.copy()
msg = line.split(" ")
# print(x_center,",",y_center,",",width,",",height)
x1 = int((float(msg[1]) - float(msg[3]) / 2) * w) # x_center - width/2
y1 = int((float(msg[2]) - float(msg[4]) / 2) * h) # y_center - height/2
x2 = int((float(msg[1]) + float(msg[3]) / 2) * w) # x_center + width/2
y2 = int((float(msg[2]) + float(msg[4]) / 2) * h) # y_center + height/2
# print(x1, ",", y1, ",", x2, ",", y2)
cv2.rectangle(img_tmp, (x1, y1), (x2, y2), (0, 0, 255), 5)
cv2.imshow("show", img_tmp)
c = cv2.waitKey(0)
# print("c=",c)
if c == 8:
reflag = True
ii[2].insert(0,j)
if len(history_name) > 0:
his = history_name.pop(len(history_name))
ii[2].insert(0,his)
break
else:
reflag = False
if c != 32:
# 代表不是干扰项,排除
new_lines.append(line)
print("不是干扰项!")
else:
print("判断这是干扰项!")
hands_lines.append(line)
else:
break
if reflag:
continue
if len(hands_lines) > 0:
shutil.copyfile(path_img, os.path.join(output_images, j))
shutil.copyfile(path_label, os.path.join(output_labels, label_name))
if len(new_lines) == 0:
os.remove(path_img)
os.remove(path_label)
print("删除成功!")
else:
# 重写标签
f = open(path_label, 'w', encoding='utf-8')
f.writelines(new_lines)
f.close()
if len(history_name) > 5:
history_name.pop()
history_name.append(j)
if __name__ == '__main__':
main()



