def main(): args = parse_args() det_json = args.det_json save_dir = args.save_dir model_dir = args.mtcnn_model_dir gpu_id = args.gpu_id aligner = FaceAligner(model_dir, gpu_id=gpu_id) index = 0 with open(det_json, "r") as f: for line in f: index += 1 print("Processing img %d" % index) line = json.loads(line.strip()) # 一个url对应一个pts url = str(line["url"]) if not line['det']: continue pts = line['det'][0]['boundingBox']['pts'] # 图片以人名为前缀, 若无, 则为neg name = str(url.split('/')[-2]) img_name = url.split('/')[-1] sub_save_dir = os.path.join(save_dir, name) if not os.path.exists(sub_save_dir): os.makedirs(sub_save_dir) img = _pull_img(url) if img is None: continue # 只crop一张脸 face_chip = aligner.get_face_chips(img, [pts], output_square=default_square) save_name = os.path.join(sub_save_dir, img_name) cv2.imwrite(save_name, face_chip[0])
def main(nsplits, split_id, list_file, img_root_dir, mtcnn_model_dir, save_dir=None, rects_fn=None): if not save_dir: save_dir = './facescrub_mtcnn_aligned' 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) aligner = FaceAligner(mtcnn_model_dir) #fp = open(list_file, 'r') #all_lines = fp.readlines() #fp.close() rects_list = load_rect_list(rects_fn) all_lines = rects_list 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 fp_log = open(osp.join(save_dir, 'missing_imgs_split_%d.txt' % split_id), 'w') 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['image']) print('===> Processing img: ' + img_fn) img = cv2.imread(img_fn) if img is None: print 'falied to read image: ', img_fn fp_log.write(img_fn + '\n') continue ht = img.shape[0] wd = img.shape[1] print 'image.shape:', img.shape spl = osp.split(line['image']) #sub_dir = osp.split(spl[0])[1] sub_dir = spl[0] print 'sub_dir: ', sub_dir if CHINESE_2_PINYIN: sub_dir = pinyin.get(sub_dir, format="strip") # replace the dot sign in names sub_dir = sub_dir.replace(u'\xb7', '-').encode('utf-8') 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_rects_fn = osp.join(save_rect_subdir, base_name + '.txt') fp_rect = open(save_rects_fn, 'w') #rect = get_rects_for_image(rects_list, base_name) rect = line['pts'] # boxes, points = aligner.align_face(img, [rect]) boxes, points = aligner.align_face(img, [rect]) 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) dst_img = aligner.get_face_chips(img, [box], [pts])[0] 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() fp_log.close()
# fp_rlt = open(osp.join(save_dir, save_json), 'w') # results = [] img = cv2.imread(img_path) aligner = FaceAligner(model_path) t1 = time.clock() # You can align the faces in two steps like this: # bboxes, points = aligner.align_face(img, face_rects) # face_chips = aligner.get_face_chips(img, bboxes, points) # OR just align them in one step by calling the following function, # which combine last two functions face_chips = aligner.get_face_chips(img, face_rects) t2 = time.clock() for i, chip in enumerate(face_chips): print('---> chip.shape: ', chip.shape) save_name = osp.join(save_dir, 'face_chip_%s_%d' % (name, i) + ext) cv2.imwrite(save_name, chip) if show_img: cv2.imshow('face_chip', chip) cv2.waitKey(0) n_boxes = len(face_rects) print( "-->Alignment cost %f seconds, processed %d face rects, avg time: %f seconds"
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) aligner = FaceAligner(mtcnn_model_dir) 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_fn = osp.join(root_dir, img_fn) print 'process image: ', img_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_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) dst_img = aligner.get_face_chips(image, [box], [pts])[0] cv2.imwrite(save_img_fn, dst_img) item = {} tmp = {'rect': box[0:4], 'score': box[4], 'pts': pts, 'id': id_num} item['faces'] = tmp #item['id'] = data[u'url'].line_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 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) # aligner = MtcnnAligner(mtcnn_model_dir, False) aligner = FaceAligner(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) ] # print 'face rect: ', gt # boxes, points = aligner.align_face(img, [GT_RECT]) 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.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_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) dst_img = aligner.get_face_chips(img, [box], [pts])[0] 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(argv): args = parse_arguments(argv) print '===> args:\n', args config = load_config(args.config) print '===> config:\n', config extractor_config = config['face_feature'] mtcnn_model_path = str(config['mtcnn_model_dir']) do_detect = not args.no_detect do_align = not args.no_align save_dir = args.save_dir if not osp.exists(save_dir): os.makedirs(save_dir) save_img = args.save_image show_img = args.show_image detector = None aligner = None if do_detect: detector = MtcnnDetector(mtcnn_model_path) if do_align: if not do_detect: aligner = FaceAligner(mtcnn_model_path) else: aligner = FaceAligner(None) else: aligner = None feature_extractor = CaffeFeatureExtractor(extractor_config) feat_layer = feature_extractor.get_feature_layers()[0] fp = open(args.img_list_file, 'r') fp_rlt = open(osp.join(save_dir, 'face_feature.json'), 'w') fp_rlt.write('[\n') write_comma_flag = False # result_list = [] img_cnt = 0 faces_cnt = 0 ttl_det_time = 0.0 ttl_feat_time = 0.0 for line in fp: img_path = line.strip() print("\n===>" + img_path) if img_path == '': print 'empty line, not a file name, skip to next' continue if img_path[0] == '#': print 'skip line starts with #, skip to next' continue # result_list.append(rlt) if write_comma_flag: fp_rlt.write(',\n') else: write_comma_flag = True rlt = {} rlt["filename"] = img_path rlt["faces"] = [] rlt['face_count'] = 0 try: if args.image_root_dir: img = cv2.imread(osp.join(args.image_root_dir, img_path)) else: img = cv2.imread(img_path) print '\n---> img.shape: ', img.shape except: print('failed to load image: ' + img_path) #rlt["message"] = "failed to load" json_str = json.dumps(rlt, indent=2) fp_rlt.write(json_str) fp_rlt.flush() continue if img is None: print('failed to load image: ' + img_path) rlt["message"] = "failed to load" # result_list.append(rlt) json_str = json.dumps(rlt, indent=2) fp_rlt.write(json_str) fp_rlt.flush() continue img_cnt += 1 if do_detect: t1 = time.clock() bboxes, points = detector.detect_face(img) t2 = time.clock() ttl_det_time += t2 - t1 print("detect_face() costs %f seconds" % (t2 - t1)) else: print '---> Will not do detection because of option "--no_detect"' shp = img.shape rect = [0, 0, shp[1] - 1, shp[0] - 1, 1.0] bboxes = [rect] points = [None] n_faces = 0 if bboxes is not None: n_faces = len(bboxes) if n_faces > 0: for (box, pts) in zip(bboxes, points): # box = box.tolist() # pts = pts.tolist() tmp = {'rect': box[0:4], 'score': box[4], 'pts': pts} rlt['faces'].append(tmp) rlt['face_count'] = n_faces # print('output bboxes: ' + str(bboxes)) # print('output points: ' + str(points)) # toc() if do_detect: print( "\n===> Detect %d images, costs %f seconds, avg time: %f seconds" % (img_cnt, ttl_det_time, ttl_det_time / img_cnt)) print "---> %d faces detected" % n_faces if not n_faces: continue t1 = time.clock() if do_align: if points is None or points[0] is None: face_chips = aligner.get_face_chips(img, bboxes, None) else: face_chips = aligner.get_face_chips(img, bboxes, points) # face_chips = aligner.get_face_chips(img, bboxes, None) # face_chips = [im.astype(np.float) for im in face_chips_ubyte] else: print '---> Will not do alignment because of option "--no_align"' face_chips = [img.astype(np.float)] features = feature_extractor.extract_features_batch( face_chips)[feat_layer] t2 = time.clock() ttl_feat_time += t2 - t1 print("Cropping and extracting features for %d faces cost %f seconds" % (n_faces, t2 - t1)) faces_cnt += n_faces print( "\n===> Extracting features for %d faces, costs %f seconds, avg time: %f seconds" % (faces_cnt, ttl_feat_time, ttl_feat_time / faces_cnt)) for i, box in enumerate(bboxes): # feat_file = '%s_%d_rect[%d_%d_%d_%d].npy' % ( # osp.basename(img_path), i, box[0], box[1], box[2], box[3]) # feat_file = osp.join(save_dir, feat_file) # np.save(feat_file, features[i]) base_name = osp.basename(img_path) face_fn_prefix = '%s_face_%d' % (osp.splitext(base_name)[0], i) feat_file = face_fn_prefix + '.npy' np.save(osp.join(save_dir, feat_file), features[i]) face_chip_fn = face_fn_prefix + '.jpg' cv2.imwrite(osp.join(save_dir, face_chip_fn), face_chips[i]) rlt['faces'][i]['feat'] = feat_file rlt['faces'][i]['face_chip'] = face_chip_fn rlt['message'] = 'success' # result_list.append(rlt) json_str = json.dumps(rlt, indent=2) fp_rlt.write(json_str) fp_rlt.flush() if save_img or show_img: draw_faces(img, bboxes, points) if save_img: save_name = osp.join(save_dir, osp.basename(img_path)) cv2.imwrite(save_name, img) if show_img: cv2.imshow('img', img) ch = cv2.waitKey(0) & 0xFF if ch == 27: break #json.dump(result_list, fp_rlt, indent=4) fp_rlt.write('\n]\n') fp_rlt.close() fp.close() if show_img: cv2.destroyAllWindows()
def main(json_file, save_dir=None, save_img=True, show_img=True): if not osp.exists(json_file): print 'Cannot find json file: ' + json_file pass if save_dir is None: save_dir = './fa_facex_rlt' save_json = 'mtcnn_align_rlt.json' model_path = "../../model" fp_json = open(json_file, 'r') facex_response = json.load(fp_json) fp_json.close() if (not facex_response or not isinstance(facex_response, dict) or 'facex_det' not in facex_response): print 'Invalid json file: ' + json_file pass facex_det_response = facex_response['facex_det'] if not osp.exists(save_dir): os.makedirs(save_dir) fp_rlt = open(osp.join(save_dir, save_json), 'w') results = [] for item in facex_det_response: img_path = item['name'] print '===> Processing image: ' + img_path if 'detections' not in item: continue face_rects = [] for face in item['detections']: face_rects.append(face['pts']) img = cv2.imread(img_path) aligner = FaceAligner(model_path, False) rlt = {} rlt["filename"] = img_path rlt["faces"] = [] rlt['face_count'] = 0 t1 = time.clock() bboxes, points = aligner.align_face(img, face_rects) t2 = time.clock() n_boxes = len(face_rects) print( "-->Alignment cost %f seconds, processed %d face rects, avg time: %f seconds" % ((t2 - t1), n_boxes, (t2 - t1) / n_boxes)) if bboxes is not None and len(bboxes) > 0: for (box, pts) in zip(bboxes, points): # box = box.tolist() # pts = pts.tolist() tmp = {'rect': box[0:4], 'score': box[4], 'pts': pts} rlt['faces'].append(tmp) rlt['face_count'] = len(bboxes) rlt['message'] = 'success' results.append(rlt) spl = osp.split(img_path) sub_dir = osp.split(spl[0])[1] base_name = spl[1] save_img_subdir = osp.join(save_dir, sub_dir) if not osp.exists(save_img_subdir): os.mkdir(save_img_subdir) # save_rect_subdir = osp.join(save_dir, sub_dir) # if not osp.exists(save_rect_subdir): # os.mkdir(save_rect_subdir) # print pts save_img_fn = osp.join(save_img_subdir, base_name) print 'save face chip into ', save_img_fn # facial5points = np.reshape(pts, (2, -1)) # dst_img = warp_and_crop_face( # img, facial5points, reference_5pts, output_size) dst_img = aligner.get_face_chips(img, [box], [pts], True)[0] cv2.imwrite(save_img_fn, dst_img) json.dump(results, fp_rlt, indent=2) fp_rlt.close()