import os
import json
import numpy as np
dir_json = 'D:/Data/MyAnnoData/landmarks/20200915/json/'
dir_txt = 'D:/Data/MyAnnoData/landmarks/20200915/'
remove_txt = "images/"
if not os.path.exists(dir_txt):
os.makedirs(dir_txt)
list_json = os.listdir(dir_json)
ftxt = open(dir_txt+"label.txt",'w+')
for cnt,json_name in enumerate(list_json):
#print('cnt=%d,name=%s'%(cnt,json_name))
path_json = dir_json + json_name
path_txt = dir_txt + json_name.replace('.json','.txt')
# print(path_json, path_txt)
path_img = dir_txt + json_name.replace('.json', '.jpg')#图片路径
remove_path = remove_txt + json_name.replace('.json', '.jpg')#图片路径
ftxt.writelines("# "+remove_path+"
")
with open(path_json, 'r') as path_json:
jsonx = json.load(path_json) # json文件信息读入
rect_add_landmark = ""
cntPerson = int(len(jsonx['shapes']) / 6)
saveInfo = ''
flag = 0
landmarkInfo = ""
facemaskInfo = ""
for shape in jsonx['shapes']: # 读取shapes目录
xy = np.array(shape['points']) # 读取points信息,五官和人脸框
typeItem = str(shape['shape_type']) #读取shape_type
if(typeItem == "point"):#点
flag+=1
if(flag%6==0):
flag=1
label1 = str(shape['label']) # 读取label信息
if (label1==str(flag)):
for i in range(len(xy)):
for j in range(len(xy[i])):
landmarkInfo += str(xy[i][j]) + ','
else:
print("label not match...")
print(path_img)
elif (typeItem == "rectangle"):
if(flag%5!=0):
print("landmark and face error")
print(path_img)
label2 = str(shape['label']) # 读取label信息
if(label2=="face"):
for i in range(len(xy)):
for j in range(len(xy[i])):
facemaskInfo += str(xy[i][j]) + ','
elif(label2=="mask"):
for i in range(len(xy)):
for j in range(len(xy[i])):
facemaskInfo += str(xy[i][j]) + ','
facemaskList = facemaskInfo.split(",")
landmarkList = landmarkInfo.split(",")
#print(facemaskList)
cutPoint = 0.0
for i in range(cntPerson):
#先存储人脸信息
x = float(facemaskList[i*4])
y = float(facemaskList[i*4+1])
w = float(facemaskList[i*4+2]) - float(facemaskList[i*4])
h = float(facemaskList[i*4+3]) - float(facemaskList[i*4+1])
ftxt.writelines(str(x) + " ")
ftxt.writelines(str(y)+ " ")
ftxt.writelines(str(w)+ " ")
ftxt.writelines(str(h)+ " ")
point1x = str(landmarkList[i * 10])
point1y = str(landmarkList[i * 10 + 1])
ftxt.writelines(str(point1x)+ " ")
ftxt.writelines(str(point1y) + " ")
ftxt.writelines(str(cutPoint) + " ")
point2x = str(landmarkList[i * 10 + 2])
point2y = str(landmarkList[i * 10 + 3])
ftxt.writelines(str(point2x) + " ")
ftxt.writelines(str(point2y) + " ")
ftxt.writelines(str(cutPoint) + " ")
point3x = str(landmarkList[i * 10 + 4])
point3y = str(landmarkList[i * 10 + 5])
ftxt.writelines(str(point3x) + " ")
ftxt.writelines(str(point3y) + " ")
ftxt.writelines(str(cutPoint) + " ")
point4x = str(landmarkList[i * 10 + 6])
point4y = str(landmarkList[i * 10 + 7])
ftxt.writelines(str(point4x) + " ")
ftxt.writelines(str(point4y) + " ")
ftxt.writelines(str(cutPoint) + " ")
point5x = str(landmarkList[i * 10 + 8])
point5y = str(landmarkList[i * 10 + 9])
ftxt.writelines(str(point5x) + " ")
ftxt.writelines(str(point5y) + " ")
ftxt.writelines(str(cutPoint) + " ")
ftxt.writelines(str(cutPoint))
ftxt.writelines("
")
ftxt.close()
print("end...")
//显示
import os
import cv2
if __name__ == '__main__':
#txt_path = r"D:/Detection/Pytorch_Retinaface-master/data/widerface/train/labeltest.txt"
txt_path = r"F:/FaceLandmarks/widerface/train/labelwider.txt"
imgs_path = []
words = []
f = open(txt_path, 'r')
lines = f.readlines()
isFirst = True
labels = []
for line in lines:
line = line.rstrip()
if line.startswith('#'):
if isFirst is True:
isFirst = False
else:
labels_copy = labels.copy()
words.append(labels_copy)
labels.clear()
path = line[2:]
path = txt_path.replace('labelwider.txt', 'imageswider/') + path
#path = txt_path.replace('/label.txt', '/') + path
imgs_path.append(path)
else:
line = line.split(' ')
label = [float(x) for x in line]
labels.append(label)
words.append(labels)
print(len(words))
index = 0
for index, file in enumerate(imgs_path):
labels = words[index]
img_raw = cv2.imread(file)
for label in labels:
x1 = int(label[0])
y1 = int(label[1])
x2 = int(label[0] + label[2])
y2 = int(label[1] + label[3])
if label[4] < 0:
continue
# if label[2] * label[3] > 11 * 12:
# face_num += 1
else:
cv2.rectangle(img_raw, (x1, y1), (x2, y2), (0, 255, 0), 3)
# landmarks
cv2.circle(img_raw,(int(label[4]),int(label[5])),3,(0,0,255),3)
cv2.circle(img_raw, (int(label[7]), int(label[8])), 3, (0, 0, 255), 3)
cv2.circle(img_raw, (int(label[10]), int(label[11])), 3, (0, 0, 255), 3)
cv2.circle(img_raw, (int(label[13]), int(label[14])), 3, (0, 0, 255), 3)
cv2.circle(img_raw, (int(label[16]), int(label[17])), 3, (0, 0, 255), 3)
# if img_raw.shape[0] > 1000:
# fy = 1000 / img_raw.shape[0]
# img_raw = cv2.resize(img_raw, (0, 0), fx=fy, fy=fy, interpolation=cv2.INTER_NEAREST)
cv2.imshow("aaa", img_raw)
cv2.waitKey()