def main(lfw_list_fn, lfw_root, save_dir, save_img=False, show_img=False): minsize = 20 caffe_model_path = "../../model" threshold = [0.6, 0.7, 0.7] scale_factor = 0.709 if not osp.exists(save_dir): os.makedirs(save_dir) fp_rlt = open(osp.join(save_dir, 'lfw_mtcnn_fd_rlt.json'), 'w') # result_list = [] fp_rlt.write('[\n') t1 = time.clock() detector = MtcnnDetector(caffe_model_path) t2 = time.clock() print("initFaceDetector() costs %f seconds" % (t2 - t1)) fp = open(lfw_list_fn, 'r') ttl_time = 0.0 img_cnt = 0 for line in fp: imgpath = line.strip() print("\n===>" + imgpath) if imgpath == '': print 'empty line, not a file name, skip to next' continue if imgpath[0] == '#': print 'skip line starts with #, skip to next' continue splits = imgpath.split() imgpath = splits[0] id = 'unkown' if len(splits) < 2 else splits[1] if not imgpath.startswith('/'): fullpath = osp.join(lfw_root, imgpath) else: fullpath = imgpath rlt = {} rlt["filename"] = imgpath rlt["faces"] = [] rlt['face_count'] = 0 rlt['id'] = id try: img = cv2.imread(fullpath) except: print('failed to load image: ' + fullpath) rlt["message"] = "failed to load" result_list.append(rlt) continue if img is None: print('failed to load image: ' + fullpath) rlt["message"] = "failed to load" result_list.append(rlt) continue img_cnt += 1 t1 = time.clock() bboxes, points = detector.detect_face(img, minsize, threshold, scale_factor) t2 = time.clock() ttl_time += t2 - t1 print("detect_face() costs %f seconds" % (t2 - t1)) if 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' # result_list.append(rlt) s = json.dumps(rlt, indent=2) fp_rlt.write(s + ',\n') # fp_rlt.write(',\n' + s) # print('output bboxes: ' + str(bboxes)) # print('output points: ' + str(points)) # toc() if bboxes is None: continue print("\n===> Processed %d images, costs %f seconds, avg time: %f seconds" % ( img_cnt, ttl_time, ttl_time / img_cnt)) if save_img or show_img: draw_faces(img, bboxes, points) if save_img: save_name = osp.join(save_dir, osp.basename(imgpath)) 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=2) # print fp_rlt.tell() # delete the last ',' if sys.platform is 'win32': fp_rlt.seek(-3, 1) else: fp_rlt.seek(-2, 1) fp_rlt.write('\n]') fp_rlt.close() fp.close() if show_img: cv2.destroyAllWindows()
def main(save_dir=None, save_img=False, show_img=False): minsize = 20 caffe_model_path = MODEL_PATH threshold = [0.6, 0.7, OUTPUT_THRESHOLD] scale_factor = 0.709 if not save_dir: save_dir = './fd_rlt' if not osp.exists(save_dir): os.makedirs(save_dir) fp_time = open(osp.join(save_dir, 'fd_time.txt'), 'w') t1 = time.clock() detector = MtcnnDetector(caffe_model_path) t2 = time.clock() msg = "initFaceDetector() costs %f seconds" % (t2 - t1) print(msg) fp_time.write(msg + '\n') ttl_time = 0.0 img_cnt = 0 for k in range(1, FOLDS_CNT + 1): k_str = str(k) if k != 10: k_str = "0" + k_str fn_list = osp.join(FDDB_FOLDS_DIR, "FDDB-fold-" + k_str + ".txt") fn_fd_rlt = osp.join(save_dir, "fold-" + k_str + "-out.txt") print('===========================') print('Process image list: ' + fn_list) print('Save results into: ' + fn_fd_rlt) fp_list = open(fn_list, 'r') fp_fd_rlt = open(fn_fd_rlt, 'w') for line in fp_list: imgname = line.strip() imgpath = osp.join(FDDB_IMG_ROOT_DIR, imgname + ".jpg") msg = "---> " + imgpath print(msg) fp_time.write(msg + 'n') img = cv2.imread(imgpath) if img is None: raise Exception('failed to load image: ' + imgpath) resize_factor = 1.0 if DO_RESIZE: print('original image shape: {}'.format(img.shape)) ht, wd, chs = img.shape if ht > wd: resize_factor = float(RESIZED_LONG_SIDE) / ht else: resize_factor = float(RESIZED_LONG_SIDE) / wd wd_new = int(resize_factor * wd) ht_new = int(resize_factor * ht) resized_img = cv2.resize(img, (wd_new, ht_new)) print('resized image shape: {}'.format(resized_img.shape)) # if show_img: # cv2.imshow('resied_img', resized_img) # ch = cv2.waitKey(0) & 0xFF # if ch == 27: # break else: resized_img = img resize_factor_inv = 1.0 / resize_factor img_cnt += 1 t1 = time.clock() bboxes, points = detector.detect_face(resized_img, minsize, threshold, scale_factor) t2 = time.clock() ttl_time += t2 - t1 msg = "detect_face() costs %f seconds" % (t2 - t1) print(msg) fp_time.write(msg + '\n') fp_fd_rlt.write(imgname + "\n") fp_fd_rlt.write(str(len(bboxes)) + "\n") print points if DO_RESIZE: for i in range(len(bboxes)): for j in range(4): bboxes[i][j] *= resize_factor_inv for j in range(10): points[i][j] *= resize_factor_inv for i in range(len(bboxes)): fp_fd_rlt.write(str(bboxes[i][0]) + " ") fp_fd_rlt.write(str(bboxes[i][1]) + " ") fp_fd_rlt.write(str(bboxes[i][2] - bboxes[i][0]) + " ") fp_fd_rlt.write(str(bboxes[i][3] - bboxes[i][1]) + " ") fp_fd_rlt.write(str(bboxes[i][4]) + "\n") fp_fd_rlt.flush() msg = "===> Processed %d images, costs %f seconds, avg time: %f seconds" % ( img_cnt, ttl_time, ttl_time / img_cnt) print(msg) fp_time.write(msg + '\n') fp_time.flush() if save_img or show_img: draw_faces(img, bboxes, points) if save_img: save_name = osp.join(save_dir, osp.basename(imgpath)) cv2.imwrite(save_name, img) if show_img: cv2.imshow('img', img) ch = cv2.waitKey(0) & 0xFF if ch == 27: break fp_list.close() fp_fd_rlt.close() fp_time.close() if show_img: cv2.destroyAllWindows()
def main(img_list_fn, root_dir, save_dir, save_img=True, show_img=False): MAX_LINE = 500 minsize = 20 caffe_model_path = "../../model" threshold = [0.6, 0.7, 0.7] scale_factor = 0.709 if not osp.exists(save_dir): os.makedirs(save_dir) fp = open(img_list_fn, 'r') # if not SAVE_INTO_SEPERATE_JSON: #fp_rlt = open(osp.join(save_dir, 'mtcnn_fd_rlt.json'), 'w') result_list = [] t1 = time.clock() gpu_id = 7 detector = MtcnnDetector(caffe_model_path, gpu_id) t2 = time.clock() print("initFaceDetector() costs %f seconds" % (t2 - t1)) ttl_time = 0.0 img_cnt = 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 img_path = osp.join(root_dir, img_path) print("\nfull path: " + img_path) rlt = {} rlt["filename"] = img_path rlt["faces"] = [] rlt['face_count'] = 0 try: img = cv2.imread(img_path) except: print('failed to load image: ' + img_path) rlt["message"] = "failed to load" result_list.append(rlt) continue if img is None: print('failed to load image: ' + img_path) rlt["message"] = "failed to load" result_list.append(rlt) continue img_cnt += 1 t1 = time.clock() 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 img = cv2.resize(img, (new_img_w, new_img_h)) bboxes, points = detector.detect_face(img, minsize, threshold, scale_factor) t2 = time.clock() ttl_time += t2 - t1 print("detect_face() costs %f seconds" % (t2 - t1)) 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' if SAVE_INTO_SEPERATE_JSON: fn_json = osp.join( save_dir, osp.splitext(osp.basename(img_path))[0] + '.json') fp_json = open(fn_json, 'w') json.dump(rlt, fp_json, indent=2) fp_json.close() # else: result_list.append(rlt) # print('output bboxes: ' + str(bboxes)) # print('output points: ' + str(points)) # toc() print( "\n===> Processed %d images, costs %f seconds, avg time: %f seconds" % (img_cnt, ttl_time, ttl_time / img_cnt)) if bboxes is None: continue 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 # if not SAVE_INTO_SEPERATE_JSON: #json.dump(result_list, fp_rlt, indent=2) fp_rlt.close() fp.close() if show_img: cv2.destroyAllWindows()
def main(img_list_fn, save_dir, save_img=True, show_img=False): minsize = 20 caffe_model_path = "./model" threshold = [0.6, 0.7, 0.7] scale_factor = 0.709 if not osp.exists(save_dir): os.makedirs(save_dir) fp_rlt = open(osp.join(save_dir, 'mtcnn_fd_rlt.json'), 'w') result_list = [] t1 = time.clock() detector = MtcnnDetector(caffe_model_path) t2 = time.clock() print("initFaceDetector() costs %f seconds" % (t2 - t1)) fp = open(img_list_fn, 'r') ttl_time = 0.0 img_cnt = 0 for line in fp: imgpath = line.strip() print("\n===>" + imgpath) if imgpath == '': print 'empty line, not a file name, skip to next' continue if imgpath[0] == '#': print 'skip line starts with #, skip to next' continue rlt = {} rlt["filename"] = imgpath rlt["faces"] = [] rlt['face_count'] = 0 try: img = cv2.imread(imgpath) except: print('failed to load image: ' + imgpath) rlt["message"] = "failed to load" result_list.append(rlt) continue if img is None: print('failed to load image: ' + imgpath) rlt["message"] = "failed to load" result_list.append(rlt) continue img_cnt += 1 t1 = time.clock() bboxes, points = detector.detect_face(img, minsize, threshold, scale_factor) t2 = time.clock() ttl_time += t2 - t1 print("detect_face() costs %f seconds" % (t2 - t1)) 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' result_list.append(rlt) # print('output bboxes: ' + str(bboxes)) # print('output points: ' + str(points)) # toc() print( "\n===> Processed %d images, costs %f seconds, avg time: %f seconds" % (img_cnt, ttl_time, ttl_time / img_cnt)) if bboxes is None: continue if save_img or show_img: draw_faces(img, bboxes, points) if save_img: save_name = osp.join(save_dir, osp.basename(imgpath)) 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.close() fp.close() if show_img: cv2.destroyAllWindows()
def main(vggface_list_fn, save_dir, save_img=False, show_img=False): minsize = 20 caffe_model_path = "../model" threshold = [0.6, 0.7, 0.7] scale_factor = 0.709 if not osp.exists(save_dir): os.makedirs(save_dir) #fp_rlt = open(osp.join(save_dir, 'vggface_mtcnn_fd_rlt.json'), 'w') #result_list = [] t1 = time.clock() detector = MtcnnDetector(caffe_model_path) t2 = time.clock() print("initFaceDetector() costs %f seconds" % (t2 - t1)) #fp = open(lfw_list_fn, 'r') ttl_time = 0.0 img_cnt = 0 fp = open(vggface_list_fn,'r') all_lines = fp.readlines() count = 1 #lines = all_lines[:100] for line in all_lines: data = json.loads(line) print count count = count + 1 #img_cnt +=1 gt = data[u'label'][u'detect'][u'general_d'][u'bbox'][0][u'pts'] pose = data[u'label'][u'detect'][u'general_d'][u'bbox'][0][u'pose'] isExists=os.path.exists('./dataset/vgg_face_dataset/vgg_face/%s' % data[u'url'].split('/')[-2]) if not isExists: os.makedirs('./dataset/vgg_face_dataset/vgg_face/%s' % data[u'url'].split('/')[-2]) # urllib.urlretrieve(data[u'url'],'./dataset/vgg_face_dataset/vgg_face/%s/%s' % (data[u'url'].split('/')[-2],data[u'url'].split('/')[-1])) #print data[u'url'].split('/')[-1][:-4] result_list = [] resultpath = '%s/%s' % (data[u'url'].split('/')[-2],data[u'url'].split('/')[-1][:-4]) result_name = resultpath + '.json' imgpath = './dataset/vgg_face_dataset/vgg_face/%s/%s' % (data[u'url'].split('/')[-2],data[u'url'].split('/')[-1]) #print("\n===>" + imgpath) isExists2=os.path.exists('./result_json/%s' % data[u'url'].split('/')[-2]) isExists3=os.path.exists(osp.join(save_dir,result_name)) if not isExists2: os.makedirs('./result_json/%s' % data[u'url'].split('/')[-2]) if isExists3: continue print("\n===>" + imgpath) fp_rlt = open(osp.join(save_dir,result_name),'w') #id = 'unkown' if len(splits) < 2 else splits[1] id = data[u'facecluster'] rlt = {} rlt["filename"] = imgpath rlt["faces"] = [] rlt['face_count'] = 0 rlt['id'] = id rlt['gt'] = gt rlt['pose'] = pose try: img = io.imread(data[u'url']) #print "img.shape",img.shape except: print('failed to load image: ' + imgpath) rlt["message"] = "failed to load" result_list.append(rlt) json.dump(result_list,fp_rlt,indent=4) fp_rlt.close() continue #if img is None: if len(img.shape) != 3: print('failed to load image: ' + imgpath) rlt["message"] = "failed to load" result_list.append(rlt) json.dump(result_list,fp_rlt,indent=4) fp_rlt.close() continue img_cnt += 1 t1 = time.clock() bboxes, points = detector.detect_face(img, minsize, threshold, scale_factor) t2 = time.clock() ttl_time += t2 - t1 print("detect_face() costs %f seconds" % (t2 - t1)) if 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' result_list.append(rlt) # print('output bboxes: ' + str(bboxes)) # print('output points: ' + str(points)) # toc() if bboxes is None: json.dump(result_list,fp_rlt,indent=4) fp_rlt.close() continue print("\n===> Processed %d images, costs %f seconds, avg time: %f seconds" % ( img_cnt, ttl_time, ttl_time / img_cnt)) if save_img or show_img: draw_faces(img, bboxes, points) if save_img: save_name = osp.join(save_dir, osp.basename(imgpath)) 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.close() fp.close() if show_img: cv2.destroyAllWindows()