| 80 | |
| 81 | |
| 82 | def work(i): # a single work |
| 83 | for _ in range(3): # try three times |
| 84 | try: |
| 85 | img = imread(os.path.join(args.img_dir, img_names[i])) |
| 86 | img_crop, tformed_landmarks = align_crop(img, |
| 87 | landmarks[i], |
| 88 | standard_landmark, |
| 89 | crop_size=(args.crop_size_h, args.crop_size_w), |
| 90 | face_factor=args.face_factor, |
| 91 | align_type=args.align_type, |
| 92 | order=args.order, |
| 93 | mode=args.mode) |
| 94 | |
| 95 | name = os.path.splitext(img_names[i])[0] + '.' + args.save_format |
| 96 | path = os.path.join(data_dir, name) |
| 97 | if not os.path.isdir(os.path.split(path)[0]): |
| 98 | os.makedirs(os.path.split(path)[0]) |
| 99 | imwrite(path, img_crop) |
| 100 | |
| 101 | tformed_landmarks.shape = -1 |
| 102 | name_landmark_str = ('%s' + ' %.1f' * n_landmark * 2) % ((name, ) + tuple(tformed_landmarks)) |
| 103 | succeed = True |
| 104 | break |
| 105 | except: |
| 106 | succeed = False |
| 107 | if succeed: |
| 108 | return name_landmark_str |
| 109 | else: |
| 110 | print('%s fails!' % img_names[i]) |
| 111 | |
| 112 | |
| 113 | pool = Pool(args.n_worker) |