#读取txt文件
import os
from scipy.io import loadmat
f = open("/home/lisongj/Downloads/ps2.0/vaild.txt","r")#设置文件对象
data = f.readlines()
#读取mat文件,转txt
for da in data:
str1=da.split('/')[-1].split('.')[0]
filpath=os.path.join('/home/lisongj/Downloads/ps2.0/testing/all',str1+'.mat')
data0=loadmat(filpath)
data1=data0.get('marks')
nums=[]
#print(data1)
for line in data1:
#print(line)
num=[]
num.append(0)
num.append(round(line[0]/600,6))
num.append(round(line[1]/600,6))
num.append(round(20/600,6))
num.append(round(20/600,6))
#print(num)
nums.append(num)
#print(nums)
path=('/home/lisongj/Downloads/ps2.0/labels/vaild')
with open(os.path.join(path, str1 + '.txt'), 'w') as f:
for i in nums:
i = str(i).strip('[').strip(']').replace(',','')+'\n' #.replace('\'','')
f.write(i)
f.close()
提取xml想要信息连并通过鼠标标记获取每张图片点坐标x、y和生成框信息的w、h,存入对应txt文件。
#xml转txt
import cv2
import os
import xml.etree.ElementTree as ET
def on_EVENT_LBUTTONDOWN(event, x, y, flags, param):
global new_points
if event == cv2.EVENT_LBUTTONDOWN:
xy = "%d,%d" % (x, y)
new_points.append(['corner', x / img.shape[1], y / img.shape[0], 0.02, 0.02])
refresh()
#x,y点的坐标,框占图片百分比2%
def refresh():
tmp_img = img.copy()
for new_point in new_points:
# print(new_point)
cv2.circle(tmp_img, (int(new_point[1] * img.shape[1]), int(new_point[2] * img.shape[0])), 3, (0,0,255), thickness=-1)
# cv2.putText(img, xy, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 0), thickness=1)
cv2.imshow("img", tmp_img)
#建立文件夹
image_path = r"/home/lisongj/Downloads/mark/image"
label_path = r"/home/lisongj/Downloads/mark/label"
output_path = r"/home/lisongj/Downloads/mark/output"
if not os.path.isdir(output_path):
os.mkdir(output_path)
if not os.path.isdir(os.path.join(output_path, 'ima')):
os.mkdir(os.path.join(output_path, 'ima'))
if not os.path.isdir(os.path.join(output_path, 'lab')):
os.mkdir(os.path.join(output_path, 'lab'))
new_points = []
try:
with open('last', 'r') as f:
r = f.read()
last_marked = int(r)
except:
last_marked = 0
image_names = os.listdir(image_path)
#print(image_names)
cv2.namedWindow("img") #新建一个显示窗口
cv2.setMouseCallback("img", on_EVENT_LBUTTONDOWN)
for index, image_name in enumerate(image_names[last_marked:]):
with open('last', 'w') as f:
f.write(str(last_marked+index))
src = cv2.imread(os.path.join(image_path, image_name))
img = cv2.resize(src, (1600, 900))
cv2.imshow('img', img)
while True:
key = cv2.waitKey(0) #z键撤销
if key == 1048698:
if len(new_points) > 0:
new_points = new_points[:-1]
refresh()
#x键表下一张图片
if key == 1048696:
#提取xml文件中name+坐标信息
tree = ET.parse(os.path.join(label_path, image_name[:-4] + '.xml')) #将一个文件或者字符串解析为element tree。
print(tree)
root = tree.getroot() #获取根节点
objs = root.findall('object') #寻找所有匹配子元素,匹配对象可以为tag(表示数据代表的种类,当为节点时为节点名称)或path
out_objs = []
for obj in objs:
out_obj = []
out_obj.append(obj.find('name').text.replace(' ', '_'))
print(out_obj)
bndbox = obj.find('bndbox')
xmin = int(bndbox.find('xmin').text)
ymin = int(bndbox.find('ymin').text)
xmax = int(bndbox.find('xmax').text)
ymax = int(bndbox.find('ymax').text)
out_obj.append((xmin + xmax) / 2 / src.shape[1])
out_obj.append((ymin + ymax) / 2 / src.shape[0])
out_obj.append((xmax - xmin) / src.shape[1])
out_obj.append((ymax - ymin) / src.shape[0])
out_objs.append(out_obj)
print(out_objs)
out_objs += new_points
#生成txt文件
new_points = []
with open(os.path.join(output_path, 'lab', image_name[:-4] + '.txt'), 'w') as f:
f.write(out_objs)
cv2.imwrite(os.path.join(output_path, 'ima', image_name[:-4] + '.jpg'), cv2.resize(src, (416,416)))
break