def main(args): rect_root_dir = args.rect_root_dir save_dir = args.save_dir mtcnn_model_dir = args.mtcnn_model_dir img_root_dir = args.image_root_dir gpu_id = args.gpu_id MAX_LINE = 500 rect_list = get_rect_list(rect_root_dir) print('all %d images' % (len(rect_list))) if not save_dir: save_dir = './aligned_root_dir' if not osp.exists(save_dir): print('makedirs for aligned root dir: ', save_dir) os.makedirs(save_dir) save_aligned_dir = osp.join(save_dir, 'aligned_imgs') if not osp.exists(save_aligned_dir): print('makedirs for aligned/cropped face imgs: ', save_dir) os.makedirs(save_aligned_dir) save_rects_dir = osp.join(save_dir, 'face_rects') if not osp.exists(save_rects_dir): print('makedirs for face rects/landmarks: ', save_rects_dir) os.makedirs(save_rects_dir) aligner = MtcnnAligner(mtcnn_model_dir, True, gpu_id=gpu_id) for i in range(len(rect_list)): with open(osp.join(rect_root_dir, rect_list[i]), 'r') as f: #lines = f.readlines() contents = json.load(f) img_name = contents['filename'] face_count = contents['face_count'] if face_count == 0: print('not detect face: %s' % img_name) continue img = cv2.imread(img_name) img_h = img.shape[0] img_w = img.shape[1] max_line = 0 resize_scale = 0 new_img_h = img_h new_img_w = img_w if img_h >= img_w: max_line = img_h if max_line > MAX_LINE: resize_scale = max_line / MAX_LINE new_img_h = MAX_LINE new_img_w = img_w / resize_scale if img_w > img_h: max_line = img_w if max_line > MAX_LINE: resize_scale = max_line / MAX_LINE new_img_w = MAX_LINE new_img_h = img_h / resize_scale print 'image.shape:', img.shape img = cv2.resize(img, (new_img_w, new_img_h)) img_center_x = img_w / 2 img_center_y = img_h / 2 #choose center face center_face_index = 0 min_dist = 9999999 gt_rect = '' for j in range(face_count): gt_rect = contents['faces'][j]['rect'] face_center_x = int(gt_rect[0]) + int(gt_rect[2] / 2) face_center_y = int(gt_rect[1]) + int(gt_rect[3] / 2) dist = math.sqrt( math.pow(face_center_x - img_center_x, 2) + math.pow(face_center_y - img_center_y, 2)) if dist < min_dist: min_dist = dist center_face_index = j gt_rect = contents['faces'][center_face_index]['rect'] boxes, points = aligner.align_face(img, [gt_rect]) box = boxes[0] pts = points[0] spl = img_name.split('/') base_name = spl[-1] save_img_subdir = osp.join(save_aligned_dir, spl[-2]) if not osp.exists(save_img_subdir): os.makedirs(save_img_subdir) save_img_fn = osp.join(save_img_subdir, base_name) print('save_img_fn: %s' % save_img_fn) facial5points = np.reshape(pts, (2, -1)) dst_img = warp_and_crop_face(img, facial5points, reference_5pts, output_size) cv2.imwrite(save_img_fn, dst_img)
fp_log3.write("--> max_overlap_idx = {}\n".format(max_overlap_idx)) fp_log3.write("--> scores = {}\n".format(scores)) fp_log3.write("--> overlaps = {}\n".format(overlaps)) if overlaps[max_overlap_idx] >= overlap_thresh_0: fp_log1.write(item['filename'] + ': ' + " max_overlap_idx=" + str(max_overlap_idx) + '\n') if do_align: points = np.array(faces[max_overlap_idx]['pts']) facial5points = np.reshape(points, (2, -1)) # print facial5points try: image = cv2.imread(img_fn, True) dst_img = warp_and_crop_face( image, facial5points, reference_5pts, output_size) cv2.imwrite(save_fn, dst_img) except Exception as e: failed_count1 += 1 fp_log2.write(item['filename'] + ': ' + "exception when loading image" " or aligning faces or saving results" + '\n') fp_log2.write("\texception: {}".format(e) + '\n') continue fp_log1.write(item['filename'] + ': ' + " succeeded to align" + '\n') else: failed_count2 += 1 fp_log2.write(item['filename'] + ': ' +
def main(args): save_dir = args.save_dir list_file = args.image_list nsplits = args.nsplits split_id = args.split_id # mtcnn_model_dir = args.mtcnn_model_dir img_root_dir = args.image_root_dir rect_root_dir = args.rect_root_dir # gpu_id = args.gpu_id if not save_dir: save_dir = './aligned_root_dir' if not osp.exists(save_dir): print('makedirs for aligned root dir: ', save_dir) os.makedirs(save_dir) save_aligned_dir = osp.join(save_dir, 'aligned_imgs') if not osp.exists(save_aligned_dir): print('makedirs for aligned/cropped face imgs: ', save_dir) os.makedirs(save_aligned_dir) save_rects_dir = osp.join(save_dir, 'face_rects') if not osp.exists(save_rects_dir): print('makedirs for face rects/landmarks: ', save_rects_dir) os.makedirs(save_rects_dir) # aligner = MtcnnAligner(mtcnn_model_dir, True, gpu_id=gpu_id) fp = open(list_file, 'r') # all_lines = fp.readlines() all_lines = json.load(fp) fp.close() total_line_cnt = len(all_lines) print('--->%d imgs in total' % total_line_cnt) if nsplits < 2: if split_id > 0: print('===> Will only process first %d imgs' % split_id) start_line = 0 end_line = split_id else: print('===> Will process all of the images') start_line = 0 end_line = total_line_cnt else: assert(split_id < nsplits) lines_per_split = float(total_line_cnt) / nsplits start_line = int(lines_per_split * split_id) end_line = int(lines_per_split * (split_id + 1)) if end_line + 1 >= total_line_cnt: end_line = total_line_cnt print('===> Will only process imgs in the range [%d, %d)]' % ( start_line, end_line)) count = start_line all_lines = all_lines[start_line:end_line] img_ext_list = ['.jpg', '.JPG', '.bmp', '.BMP', '.png', 'PNG', '.jpeg', '.JPEG' ] def correct_img_fn(img_fn): new_fn = None base_fn = osp.splitext(img_fn)[0] for ext in img_ext_list: _fn = base_fn + ext if osp.exists(_fn): new_fn = _fn break return new_fn for i, item in enumerate(all_lines): line = item["filename"].strip() print '%d\n' % count count = count + 1 img_fn = osp.join(img_root_dir, line) print('===> Processing img: ' + img_fn) if not osp.exists(img_fn): print('img fn not exists, try to correct') new_img_fn = correct_img_fn(img_fn) print('corrected img fn is:', new_img_fn) if not new_img_fn: print('---> Failed to correct img:', img_fn) continue img_fn = new_img_fn spl = osp.split(line) sub_dir = spl[0] base_name = spl[-1] save_img_subdir = osp.join(save_aligned_dir, sub_dir) if not osp.exists(save_img_subdir): os.makedirs(save_img_subdir) save_rect_subdir = osp.join(save_rects_dir, sub_dir) if not osp.exists(save_rect_subdir): os.makedirs(save_rect_subdir) # print pts save_img_fn = osp.join(save_img_subdir, base_name) if SKIP_EXISTING_RLT and osp.exists(save_img_fn): print '---> skip existing result image: ', save_img_fn continue rect_fn = osp.join(rect_root_dir, line[0:-4] + '.txt') if not osp.exists(rect_fn): print('rect file not exists, skip') continue img = cv2.imread(img_fn) # ht = img.shape[0] # wd = img.shape[1] # print 'image.shape:', img.shape box, pts = get_rect_and_landmarks(rect_fn) # if box is None: # print('Failed to get_rect_and_landmarks(), skip to next image') # continue # print 'face rect: ', gt # boxes, points = aligner.align_face(img, [rect]) # box = boxes[0] # pts = points[0] # facial5points = np.reshape(pts, (2, -1)) facial5points = np.array(pts) dst_img = warp_and_crop_face( img, facial5points, reference_5pts, output_size) cv2.imwrite(save_img_fn, dst_img) save_rect_fn = osp.join( save_rect_subdir, osp.splitext(base_name)[0] + '.txt') fp_rect = open(save_rect_fn, 'w') for it in box: fp_rect.write('%5.2f\t' % it) fp_rect.write('\n') for i in range(5): fp_rect.write('%5.2f\t%5.2f\n' % (facial5points[i][0], facial5points[i][1])) fp_rect.close()
def main(face_json_file, mtcnn_model_dir, save_dir=None): if not osp.exists(save_dir): print('mkdir for aligned faces, aligned root dir: ', save_dir) os.makedirs(save_dir) if not osp.exists(aligned_save_dir): print('mkdir for aligned faces, aligned root dir: ', save_dir) os.makedirs(aligned_save_dir) aligner = MtcnnAligner(mtcnn_model_dir, False) fp = open('./identity_json_list_new.json', 'r') all_lines = fp.readlines() print len(all_lines) count = 107230 for line in all_lines[107230:400000]: err_msg = '' print count #print line count = count + 1 data = json.loads(line) #print data #origin_image_url = data[u'url'] image_url = data[u'url'] #splits = origin_image_url.split('/') #image_url = splits[0] + '//' + osp.join(splits[2],splits[3],splits[4],splits[6],splits[7],splits[8]) #print '*******',image_url gt = data[u'label'][u'detect'][u'general_d'][u'bbox'][0][u'pts'] print image_url overlap_thresh_0 = overlap_thresh save_path = osp.join(data[u'url'].split('/')[-3], data[u'url'].split('/')[-2]) #print save_path isExists1 = osp.exists(osp.join(save_dir, save_path)) if not isExists1: os.makedirs(osp.join(save_dir, save_path)) save_img_fn = osp.join(data[u'url'].split('/')[-3], data[u'url'].split('/')[-2]) save_fn = aligned_save_dir + '/' + save_img_fn + '/' + data[ u'url'].split('/')[-1] #print save_fn isExists2 = osp.exists(osp.join(aligned_save_dir, save_img_fn)) if not isExists2: os.makedirs(osp.join(aligned_save_dir, save_img_fn)) filename = data[u'url'].split('/')[-1][:-4] + '.json' print filename fp_rlt = open(osp.join(save_dir, save_path, filename), 'w') item = {} item[u'url'] = image_url try: #print('===> Processing image: ' + save_path + '/' + filename) imageBGR = io.imread(image_url) image = cv2.cvtColor(imageBGR, cv2.COLOR_BGR2RGB) print image.shape #GT_RECT = [0,0,image.shape[0],image.shape[1]] #GT_RECT = [item['gt'][0][0],item['gt'][0][1],item['gt'][2][0],item['gt'][2][1]] GT_RECT = [gt[0][0], gt[0][1], gt[2][0], gt[2][1]] #print 'gt',gt #print 'GT_RECT',GT_RECT boxes, points = aligner.align_face(image, [GT_RECT]) box = boxes[0] pts = points[0] #print pts facial5points = np.reshape(points, (2, -1)) dst_img = warp_and_crop_face(image, facial5points, reference_5pts, output_size) cv2.imwrite(save_fn, dst_img) #print '########' print('===> Processing image: ' + save_path + '/' + filename) tmp = {'rect': box[0:4], 'score': box[4], 'gt': gt, 'pts': pts} item['faces'] = tmp item['id'] = data[u'url'].split('/')[-2] item['shape'] = image.shape except: continue json.dump(item, fp_rlt, indent=4) fp_rlt.close()
def main(nsplits, split_id, list_file, img_root_dir, mtcnn_model_dir, save_dir=None): if not save_dir: save_dir = './aligned_root_dir' if not osp.exists(save_dir): print('mkdir for aligned root dir: ', save_dir) os.makedirs(save_dir) save_aligned_dir = osp.join(save_dir, 'aligned_imgs') if not osp.exists(save_aligned_dir): print('mkdir for aligned/cropped face imgs: ', save_dir) os.makedirs(save_aligned_dir) save_rects_dir = osp.join(save_dir, 'face_rects') if not osp.exists(save_rects_dir): print('mkdir for face rects/landmarks: ', save_rects_dir) os.makedirs(save_rects_dir) detector = MtcnnDetector(mtcnn_model_dir) fp = open(list_file, 'r') all_lines = fp.readlines() fp.close() total_line_cnt = len(all_lines) print('--->%d imgs in total' % total_line_cnt) if nsplits < 2: if split_id > 0: print('===> Will only process first %d imgs' % split_id) start_line = 0 end_line = split_id else: print('===> Will process all of the images') start_line = 0 end_line = total_line_cnt else: assert (split_id < nsplits) lines_per_split = float(total_line_cnt) / nsplits start_line = int(lines_per_split * split_id) end_line = int(lines_per_split * (split_id + 1)) if end_line + 1 >= total_line_cnt: end_line = total_line_cnt print('===> Will only process imgs in the range [%d, %d)]' % (start_line, end_line)) count = start_line for line in all_lines[start_line:end_line]: line = line.strip() print count count = count + 1 img_fn = osp.join(img_root_dir, line) print('===> Processing img: ' + img_fn) img = cv2.imread(img_fn) ht = img.shape[0] wd = img.shape[1] print 'image.shape:', img.shape spl = osp.split(line) sub_dir = osp.split(spl[0])[1] print 'sub_dir: ', sub_dir if CHINESE_2_PINYIN: sub_dir = pinyin.get(sub_dir, format="strip") sub_dir = sub_dir.replace(u'\xb7', '-').encode( 'utf-8') # replace the dot sign in names base_name = osp.splitext(spl[1])[0] save_img_subdir = osp.join(save_aligned_dir, sub_dir) if not osp.exists(save_img_subdir): os.mkdir(save_img_subdir) save_rect_subdir = osp.join(save_rects_dir, sub_dir) if not osp.exists(save_rect_subdir): os.mkdir(save_rect_subdir) # print pts save_rect_fn = osp.join(save_rect_subdir, base_name + '.txt') fp_rect = open(save_rect_fn, 'w') boxes, points = detector.detect_face(img) nfaces = len(boxes) fp_rect.write('%d\n' % nfaces) for i in range(nfaces): box = boxes[i] pts = points[i] if i: save_img_fn = osp.join(save_img_subdir, base_name + '_%d.jpg' % (i + 1)) else: save_img_fn = osp.join(save_img_subdir, base_name + '.jpg') facial5points = np.reshape(pts, (2, -1)) dst_img = warp_and_crop_face(img, facial5points, reference_5pts, output_size) cv2.imwrite(save_img_fn, dst_img) print 'aligend face saved into: ', save_img_fn for it in box: fp_rect.write('%5.2f\t' % it) fp_rect.write('\n') for i in range(5): fp_rect.write('%5.2f\t%5.2f\n' % (facial5points[0][i], facial5points[1][i])) fp_rect.close()
def main(args): save_dir = args.save_dir list_file = args.image_list nsplits = args.nsplits split_id = args.split_id # mtcnn_model_dir = args.mtcnn_model_dir img_root_dir = args.image_root_dir rect_root_dir = args.rect_root_dir # gpu_id = args.gpu_id if not save_dir: save_dir = './aligned_root_dir' if not osp.exists(save_dir): print('makedirs for aligned root dir: ', save_dir) os.makedirs(save_dir) save_aligned_dir = osp.join(save_dir, 'aligned_imgs') if not osp.exists(save_aligned_dir): print('makedirs for aligned/cropped face imgs: ', save_dir) os.makedirs(save_aligned_dir) save_rects_dir = osp.join(save_dir, 'face_rects') if not osp.exists(save_rects_dir): print('makedirs for face rects/landmarks: ', save_rects_dir) os.makedirs(save_rects_dir) # aligner = MtcnnAligner(mtcnn_model_dir, True, gpu_id=gpu_id) fp = open(list_file, 'r') all_lines = fp.readlines() fp.close() total_line_cnt = len(all_lines) print('--->%d imgs in total' % total_line_cnt) if nsplits < 2: if split_id > 0: print('===> Will only process first %d imgs' % split_id) start_line = 0 end_line = split_id else: print('===> Will process all of the images') start_line = 0 end_line = total_line_cnt else: assert (split_id < nsplits) lines_per_split = float(total_line_cnt) / nsplits start_line = int(lines_per_split * split_id) end_line = int(lines_per_split * (split_id + 1)) if end_line + 1 >= total_line_cnt: end_line = total_line_cnt print('===> Will only process imgs in the range [%d, %d)]' % (start_line, end_line)) count = start_line all_lines = all_lines[start_line:end_line] for line in all_lines: line = line.strip() print '%d\n' % count count = count + 1 img_fn = osp.join(img_root_dir, line) print('===> Processing img: ' + img_fn) img = cv2.imread(img_fn) # ht = img.shape[0] # wd = img.shape[1] # print 'image.shape:', img.shape rect_fn = osp.join(rect_root_dir, line[0:-4] + '.txt') box, pts = get_rect_and_landmarks(rect_fn) # if box is None: # print('Failed to get_rect_and_landmarks(), skip to next image') # continue # print 'face rect: ', gt # boxes, points = aligner.align_face(img, [rect]) # box = boxes[0] # pts = points[0] spl = osp.split(line) sub_dir = spl[0] base_name = spl[-1] save_img_subdir = osp.join(save_aligned_dir, sub_dir) if not osp.exists(save_img_subdir): os.makedirs(save_img_subdir) save_rect_subdir = osp.join(save_rects_dir, sub_dir) if not osp.exists(save_rect_subdir): os.makedirs(save_rect_subdir) # print pts save_img_fn = osp.join(save_img_subdir, base_name) # facial5points = np.reshape(pts, (2, -1)) facial5points = np.array(pts) dst_img = warp_and_crop_face(img, facial5points, reference_5pts, output_size) cv2.imwrite(save_img_fn, dst_img) save_rect_fn = osp.join(save_rect_subdir, osp.splitext(base_name)[0] + '.txt') fp_rect = open(save_rect_fn, 'w') for it in box: fp_rect.write('%5.2f\t' % it) fp_rect.write('\n') for i in range(5): fp_rect.write('%5.2f\t%5.2f\n' % (facial5points[i][0], facial5points[i][1])) fp_rect.close()
def main(face_json_file, mtcnn_model_dir, save_dir=None): if not osp.exists(save_dir): print('mkdir for aligned faces, aligned root dir: ', save_dir) os.makedirs(save_dir) if not osp.exists(aligned_save_dir): print('mkdir for aligned faces, aligned root dir: ', save_dir) os.makedirs(aligned_save_dir) aligner = MtcnnAligner(mtcnn_model_dir, False) #root_dir = '/disk2/du/face-asian/' #path_walk = os.walk(root_dir) fp = open('./face_asian_list.txt', 'r') all_lines = fp.readlines() print len(all_lines) count = 1 for line in all_lines[:180000]: print line #for root,dirs,files in path_walk: err_msg = '' print count count = count + 1 #data = json.loads(line) #image_url = data["url"] #print image_url overlap_thresh_0 = overlap_thresh #save_path = osp.join(data[u'url'].split('/')[-5],data[u'url'].split('/')[-4],data[u'url'].split('/')[-3],data[u'url'].split('/')[-2]) save_path = line.split('/')[-2] print osp.join(save_dir, save_path) isExists1 = osp.exists(osp.join(save_dir, save_path)) if not isExists1: os.makedirs(osp.join(save_dir, save_path)) #save_img_fn = osp.join(data[u'url'].split('/')[-5],data[u'url'].split('/')[-4],data[u'url'].split('/')[-3],data[u'url'].split('/')[-2]) save_img_fn = line.split('/')[-2] save_fn = aligned_save_dir + '/' + save_img_fn + '/' + line.split( '/')[-1] print save_fn isExists2 = osp.exists(osp.join(aligned_save_dir, save_img_fn)) if not isExists2: os.makedirs(osp.join(aligned_save_dir, save_img_fn)) filename = line.split('/')[-1][:-6] + '.json' print filename fp_rlt = open(osp.join(save_dir, save_path, filename), 'w') item = {} print('===> Processing image: ' + save_path + '/' + filename) #imageBGR = io.imread(data[u'url']) #image = cv2.cvtColor(imageBGR, cv2.COLOR_BGR2RGB) image = cv2.imread(line[:-2]) print image.shape GT_RECT = [0, 0, 256, 256] boxes, points = aligner.align_face(image, [GT_RECT]) box = boxes[0] pts = points[0] facial5points = np.reshape(points, (2, -1)) dst_img = warp_and_crop_face(image, facial5points, reference_5pts, output_size) cv2.imwrite(save_fn, dst_img) tmp = {'rect': box[0:4], 'score': box[4], 'pts': pts} item['faces'] = tmp #item['id'] = data[u'url'].split('/')[-3] item['shape'] = image.shape json.dump(item, fp_rlt, indent=4) fp_rlt.close()
def main(nsplits, split_id, list_file, img_root_dir, mtcnn_model_dir, save_dir=None): if not save_dir: save_dir = './aligned_root_dir' if not osp.exists(save_dir): print('makedirs for aligned root dir: ', save_dir) os.makedirs(save_dir) save_aligned_dir = osp.join(save_dir, 'aligned_imgs') if not osp.exists(save_aligned_dir): print('makedirs for aligned/cropped face imgs: ', save_dir) os.makedirs(save_aligned_dir) save_rects_dir = osp.join(save_dir, 'face_rects') if not osp.exists(save_rects_dir): print('makedirs for face rects/landmarks: ', save_rects_dir) os.makedirs(save_rects_dir) aligner = MtcnnAligner(mtcnn_model_dir, False) fp = open(list_file, 'r') all_lines = fp.readlines() fp.close() total_line_cnt = len(all_lines) print('--->%d imgs in total' % total_line_cnt) if nsplits < 2: if split_id > 0: print('===> Will only process first %d imgs' % split_id) start_line = 0 end_line = split_id else: print('===> Will process all of the images') start_line = 0 end_line = total_line_cnt else: assert (split_id < nsplits) lines_per_split = float(total_line_cnt) / nsplits start_line = int(lines_per_split * split_id) end_line = int(lines_per_split * (split_id + 1)) if end_line + 1 >= total_line_cnt: end_line = total_line_cnt print('===> Will only process imgs in the range [%d, %d)]' % (start_line, end_line)) count = start_line for line in all_lines[start_line:end_line]: line = line.strip() print count count = count + 1 img_fn = osp.join(img_root_dir, line) print('===> Processing img: ' + img_fn) img = cv2.imread(img_fn) ht = img.shape[0] wd = img.shape[1] print 'image.shape:', img.shape # GT_RECT = [0,0,img.shape[0],img.shape[1]] # GT_RECT = [int(wd * 0.25), int(ht * 0.25), # int(wd * 0.75), int(ht * 0.72)] GT_RECT = get_gt_rect(img) # print 'face rect: ', gt boxes, points = aligner.align_face(img, [GT_RECT]) box = boxes[0] pts = points[0] spl = osp.split(line) sub_dir = spl[0] base_name = spl[-1] save_img_subdir = osp.join(save_aligned_dir, sub_dir) if not osp.exists(save_img_subdir): os.makedirs(save_img_subdir) save_rect_subdir = osp.join(save_rects_dir, sub_dir) if not osp.exists(save_rect_subdir): os.makedirs(save_rect_subdir) # print pts save_img_fn = osp.join(save_img_subdir, base_name) facial5points = np.reshape(pts, (2, -1)) dst_img = warp_and_crop_face(img, facial5points, reference_5pts, output_size) cv2.imwrite(save_img_fn, dst_img) save_rect_fn = osp.join(save_rect_subdir, osp.splitext(base_name)[0] + '.txt') fp_rect = open(save_rect_fn, 'w') for it in box: fp_rect.write('%5.2f\t' % it) fp_rect.write('\n') for i in range(5): fp_rect.write('%5.2f\t%5.2f\n' % (facial5points[0][i], facial5points[1][i])) fp_rect.close()
def main(img_list_file, root_dir, mtcnn_model_dir, save_dir=None): if not save_dir: save_dir = './aligned_images' if not osp.exists(save_dir): print('mkdir for aligned faces, aligned root dir: ', save_dir) os.makedirs(save_dir) aligned_save_dir = osp.join(save_dir, 'aligned_faces') if not osp.exists(aligned_save_dir): print('mkdir for aligned faces, aligned images dir: ', aligned_save_dir) os.makedirs(aligned_save_dir) aligner = MtcnnAligner(mtcnn_model_dir, False) fp = open(img_list_file,'r') fn_rlt = osp.join(save_dir, 'fd_rlt.json') fp_rlt = open(fn_rlt, 'w') fp_rlt.write('[\n') count = 0 for line in fp: print line line_split = line.split() img_fn = line_split[0] id_num = line_split[1] img_fn_split = img_fn.split('/') img_full_fn = osp.join(root_dir, img_fn) print 'process image: ', img_full_fn, " id_num: ", id_num #for root,dirs,files in path_walk: err_msg = '' if not count: fp_rlt.write(',\n') count = count + 1 print 'count: ', count overlap_thresh_0 = overlap_thresh save_subdir = osp.join(aligned_save_dir, img_fn_split[-2]) save_img_fn = osp.join(save_subdir, img_fn_split[-1]) if not osp.exists(save_subdir): os.makedirs(save_subdir) image = cv2.imread(img_full_fn) print image.shape boxes, points = aligner.align_face(image, [GT_RECT]) box = boxes[0] pts = points[0] facial5points = np.reshape(points, (2, -1)) dst_img = warp_and_crop_face(image, facial5points, reference_5pts, output_size) cv2.imwrite(save_img_fn, dst_img) item = { 'filename': img_base_fn, 'face_count': 1, } tmp = {'rect': box[0:4], 'score': box[4], 'pts': pts, 'id': id_num } item['faces'] = [tmp] #item['id'] = data[u'url'].splitit('/')[-3] item['shape'] = image.shape json_str = json.dumps(item, indent=2) fp_rlt.write(json_str+'\n') fp_rlt.flush() fp_rlt.write(']\n') fp_rlt.close() fp.close()
def align_faces(landmark_fn, img_root_dir, aligned_save_dir, do_align=True): fp_in = open(landmark_fn, 'r') img_list = json.load(fp_in) fp_in.close() if not osp.exists(img_root_dir): print('ERROR: webface root dir not found!!!') else: if not osp.exists(aligned_save_dir): print('mkdir for aligned faces, aligned root dir: ', aligned_save_dir) os.makedirs(aligned_save_dir) # fp_log_params = open(osp.join(aligned_save_dir, log_align_params), 'w') # params_template = ''' ## output_square = {} ## padding_factor = {} ## output_padding = {} ## output_size = {} # ''' # params_template = ('output_square = {}\n' # 'padding_factor = {}\n' # 'output_padding = {}\n' # 'output_size = {}\n') # # fp_log_params.write(params_template.format( # output_square, padding_factor, # output_padding, output_size) # ) # fp_log_params.close() fp_log1 = open(osp.join(aligned_save_dir, log_fn1), 'w') fp_log2 = open(osp.join(aligned_save_dir, log_fn2), 'w') fp_log3 = open(osp.join(aligned_save_dir, log_fn3), 'w') # imgSize = [112, 96]; # coord5points = [[30.2946, 65.5318, 48.0252, 33.5493, 62.7299], # [51.6963, 51.5014, 71.7366, 92.3655, 92.2041]]; # pts_dst = np.float32(coord5points).transpose() failed_count1 = 0 failed_count2 = 0 for item in img_list: err_msg = '' if 'filename' not in item: err_msg = "'filename' not in item, break..." print(err_msg) fp_log2.write(err_msg + '\n') break img_fn = osp.join(img_root_dir, item['filename']) save_fn = osp.join(aligned_save_dir, osp.basename(item['filename'])) save_fn_dir = osp.dirname(save_fn) print('===> Processing image: ' + img_fn) if 'faces' not in item: err_msg = "'faces' not in item" fp_log2.write(item['filename'] + ': ' + err_msg + '\n') continue elif 'face_count' not in item: err_msg = "'face_count' not in item" fp_log2.write(item['filename'] + ': ' + err_msg + '\n') continue if do_align and not osp.exists(save_fn_dir): os.makedirs(save_fn_dir) nfaces = item['face_count'] if nfaces < 1: fp_log2.write(item['filename'] + ': ' + "item['face_count'] < 1" + '\n') continue if nfaces != len(item['faces']): fp_log2.write(item['filename'] + ': ' + "item['face_count'] != len(item['faces']" + '\n') continue faces = item['faces'] scores = np.array([it['score'] for it in faces]) max_score_idx = scores.argmax() # max_score_idx = 0 # # if nfaces > 1: # for idx in range(1, nfaces): # if faces[idx]['score'] > faces[max_score_idx]['score']: # max_score_idx = idx max_overlap_idx = 0 fp_log1.write(item['filename'] + ': ' + " max_overlap_idx=" + str(max_overlap_idx) + '\n') if do_align: points = np.array(faces[max_overlap_idx]['pts']) facial5points = np.reshape(points, (2, -1)) # print facial5points try: image = cv2.imread(img_fn, True) dst_img = warp_and_crop_face(image, facial5points, reference_5pts, output_size) cv2.imwrite(save_fn, dst_img) except Exception as e: failed_count1 += 1 fp_log2.write(item['filename'] + ': ' + "exception when loading image" " or aligning faces or saving results" + '\n') fp_log2.write("\texception: {}".format(e) + '\n') continue fp_log1.write(item['filename'] + ': ' + " succeeded to align" + '\n') fp_log1.close() fp_log2.close() fp_log3.close()
def main(args): rect_root_dir = args.rect_root_dir save_dir = args.save_dir mtcnn_model_dir = args.mtcnn_model_dir img_root_dir = args.image_root_dir gpu_id = args.gpu_id rect_list = get_rect_list(rect_root_dir) print('all %d images' % (len(rect_list))) if not save_dir: save_dir = './aligned_root_dir' if not osp.exists(save_dir): print('makedirs for aligned root dir: ', save_dir) os.makedirs(save_dir) save_aligned_dir = osp.join(save_dir, 'aligned_imgs') if not osp.exists(save_aligned_dir): print('makedirs for aligned/cropped face imgs: ', save_dir) os.makedirs(save_aligned_dir) save_rects_dir = osp.join(save_dir, 'face_rects') if not osp.exists(save_rects_dir): print('makedirs for face rects/landmarks: ', save_rects_dir) os.makedirs(save_rects_dir) aligner = MtcnnAligner(mtcnn_model_dir, True, gpu_id=gpu_id) for i in range(len(rect_list)): with open(osp.join(rect_root_dir, rect_list[i]), 'r') as f: #lines = f.readlines() contents = json.load(f) img_name = contents['filename'] gt_rect = contents['faces'][0]['rect'] if gt_rect is None: print('Failed to get_gt_rect(), skip to next image') continue img = cv2.imread(img_name) ht = img.shape[0] wd = img.shape[1] print 'image.shape:', img.shape boxes, points = aligner.align_face(img, [gt_rect]) box = boxes[0] pts = points[0] spl = img_name.split('/') base_name = spl[-1] save_img_subdir = osp.join(save_aligned_dir, spl[-2]) if not osp.exists(save_img_subdir): os.makedirs(save_img_subdir) save_img_fn = osp.join(save_img_subdir, base_name) print('save_img_fn: %s' % save_img_fn) facial5points = np.reshape(pts, (2, -1)) dst_img = warp_and_crop_face(img, facial5points, reference_5pts, output_size) cv2.imwrite(save_img_fn, dst_img)
def main(args): rect_root_dir = args.rect_root_dir save_dir = args.save_dir mtcnn_model_dir = args.mtcnn_model_dir img_root_dir = args.image_root_dir gpu_id = args.gpu_id rect_list = get_rect_list(rect_root_dir) if not save_dir: save_dir = './aligned_root_dir' if not osp.exists(save_dir): print('makedirs for aligned root dir: ', save_dir) os.makedirs(save_dir) save_aligned_dir = osp.join(save_dir, 'aligned_imgs') if not osp.exists(save_aligned_dir): print('makedirs for aligned/cropped face imgs: ', save_dir) os.makedirs(save_aligned_dir) save_rects_dir = osp.join(save_dir, 'face_rects') if not osp.exists(save_rects_dir): print('makedirs for face rects/landmarks: ', save_rects_dir) os.makedirs(save_rects_dir) aligner = MtcnnAligner(mtcnn_model_dir, True, gpu_id=gpu_id) for i in range(len(rect_list)): with open(osp.join(rect_root_dir, rect_list[i]), 'r') as f: lines = f.readlines() for line in lines: img_name = line.split(',')[0].replace('\\', '/') gt_rect = get_gt_rect(line) if gt_rect is None: print('Failed to get_gt_rect(), skip to next image') continue img_fn = osp.join(img_root_dir, img_name) print('===>Processing img: ' + img_fn) img = cv2.imread(img_fn) ht = img.shape[0] wd = img.shape[1] print 'image.shape:', img.shape boxes, points = aligner.align_face(img, [gt_rect]) box = boxes[0] pts = points[0] spl = img_name.split('/') sub_dir = '' for j in range(len(spl) - 1): sub_dir = osp.join(sub_dir, spl[j]) base_name = spl[-1] save_img_subdir = osp.join(save_aligned_dir, sub_dir) if not osp.exists(save_img_subdir): os.makedirs(save_img_subdir) save_img_fn = osp.join(save_img_subdir, base_name) facial5points = np.reshape(pts, (2, -1)) dst_img = warp_and_crop_face(img, facial5points, reference_5pts, output_size) cv2.imwrite(save_img_fn, dst_img)