files里面的landmark.txt内容如下:
0.jpg 512-512 262-432 147-241 167-376 175-389 186-401 198-411 212-420 227-427 244-431 146-256 145-272 145-287 146-302 148-317 151-333 155-348 160-362 370-248 348-377 341-389 331-400 320-410 308-418 295-426 280-430 371-263 370-278 369-293 367-307 364-321 361-335 358-349 354-363 205-262 205-254 185-257 194-260 217-261 205-254 228-260 206-248 194-250 219-251 171-227 188-225 205-225 238-230 222-226 187-216 206-212 240-220 225-214 222-361 262-379 243-360 233-369 245-376 283-361 292-370 279-376 263-361 304-362 264-359 254-346 237-352 229-361 244-359 274-346 290-353 298-362 283-359 264-349 263-252 263-269 263-286 245-261 238-299 232-316 242-321 253-323 264-326 281-261 289-299 294-316 285-321 274-323 264-303 316-264 312-255 294-260 305-262 327-261 312-255 336-257 316-248 303-251 327-251 286-226 303-224 319-224 334-226 350-230 286-219 302-215 319-215 336-218
import cv2
import numpy as np
import torch
from PIL import Image
files = 'E:/pycharm_workspace/workspace/datasets/landmark.txt'
class drow_land():
def __init__(self):
super(drow_land,self).__init__()
pass
def draw_l(self,files):
file = open(files, 'r').readlines()
lab_template = np.zeros((512,512,3))
for i,f in enumerate(file):
names,shape = self.get_land(f)
shape = torch.Tensor(shape)
lab = self.drawCircle(lab_template.copy(), shape.data, radius=1, color=(255, 255, 255), thickness=8)
img = Image.fromarray(np.uint8(lab)).convert('RGB')
img.save('{}.jpg'.format(i))
def get_land(self,landmark):
l = landmark.strip().split()
name = l[0]
shape = []
for i in l[2:]:
w,h = [ float(j) for j in i.split('-')]
shape.extend([w,h])
return name,shape
def drawCircle(self, img, shape, radius=1, color=(255, 255, 255), thickness=1):
for i in range(len(shape) // 2):
img = cv2.circle(img, (int(shape[2 * i]), int(shape[2 * i + 1])), radius, color, thickness)
return img
if __name__ == '__main__':
print("--------draw landmark-------")
d = drow_land()
d.draw_l(files)
结果如下:
import os
import cv2
import numpy as np
import torch
from PIL import Image
files = 'E:/pycharm_workspace/workspace/datasets/landmark_headpose.txt'
path = 'E:/pycharm_workspace/workspace/datasets/'
class drow_land():
def __init__(self):
super(drow_land,self).__init__()
pass
def draw_l(self,files):
file = open(files, 'r').readlines()
lab_template = np.zeros((1024,1024,3))
for i,f in enumerate(file):
names,shape = self.get_land(f)
shape = torch.Tensor(shape)
lab = self.drawCircle(lab_template.copy(), shape.data, radius=1, color=(255, 255, 255), thickness=8)
img = Image.fromarray(np.uint8(lab)).convert('RGB')
img.save(os.path.join(path,'{}'.format(names)))
def get_land(self,landmark):
l = landmark.strip().split()
name = l[0]
shape = []
for i in l[2:]:
w,h = [ float(j) for j in i.split('-')]
shape.extend([w,h])
return name,shape
def drawCircle(self, img, shape, radius=1, color=(255, 255, 255), thickness=1):
for i in range(len(shape) // 2):
img = cv2.circle(img, (int(shape[2 * i]), int(shape[2 * i + 1])), radius, color, thickness)
return img
if __name__ == '__main__':
print("--------draw landmark-------")
d = drow_land()
d.draw_l(files)
print('OK')