def prepareImage(imagePath): """ Prepare images before use it. """ print("> Prepare image "+imagePath + ":") imname = ntpath.basename(imagePath) imname = imname.split(imname.split('.')[-1])[0][0:-1] # 记录图片basename img = cv2.imread( imagePath ) # 加载图片 if needCrop: dlib_img = io.imread(imagePath) img2 = cv2.copyMakeBorder(img,0,0,0,0,cv2.BORDER_REPLICATE) detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(FLAGS.predictor_path) # 加载 dlib 检测 dets = detector(img, 1) print("> Number of faces detected: {}".format(len(dets))) if len(dets) == 0: print '> Could not detect the face, skipping the image...' + image_path return None if len(dets) > 1: print "> Process only the first detected face!" # 标记第一个人脸 detected_face = dets[0] cv2.rectangle(img2, (detected_face.left(),detected_face.top()), \ (detected_face.right(),detected_face.bottom()), (0,0,255),2) fileout = open(os.path.join(FLAGS.tmp_detect , imname + ".bbox"),"w") fileout.write("%f %f %f %f\n" % (detected_face.left(),detected_face.top(), \ detected_face.right(),detected_face.bottom())) fileout.close() ## If we are using landmarks to crop if useLM: shape = predictor(dlib_img, detected_face) nLM = shape.num_parts fileout = open(os.path.join(FLAGS.tmp_detect, imname + ".pts" ), "w") for i in range(0, nLM): cv2.circle( img2, (shape.part(i).x, shape.part(i).y), 5, (255,0,0)) fileout.write("%f %f\n" % (shape.part(i).x, shape.part(i).y)) fileout.close() img = utils.cropByLM(img, shape, img2) else: print "> cropByFaceDet " img = utils.cropByFaceDet(img, detected_face, img2) cv2.imwrite(os.path.join( FLAGS.tmp_detect, imname+"_detect.png"), img2) img = cv2.resize(img, (trg_size, trg_size)) cv2.imwrite(os.path.join(FLAGS.tmp_ims, imname + '.png'), img) return img
cv2.rectangle(img2, (detected_face.left(),detected_face.top()), \ (detected_face.right(),detected_face.bottom()), (0,0,255),2) fileout = open("tmp_detect/"+imname+".bbox","w") fileout.write("%f %f %f %f\n" % (detected_face.left(),detected_face.top(), \ detected_face.right(),detected_face.bottom())) fileout.close() ## If we are using landmarks to crop if useLM: shape = predictor(dlib_img, detected_face) nLM = shape.num_parts fileout = open("tmp_detect/"+imname+".pts","w") for i in range(0,nLM): cv2.circle(img2, (shape.part(i).x, shape.part(i).y), 5, (255,0,0)) fileout.write("%f %f\n" % (shape.part(i).x, shape.part(i).y)) fileout.close() img = utils.cropByLM(img, shape, img2) else: print "> cropByFaceDet " img = utils.cropByFaceDet(img, detected_face, img2) cv2.imwrite("tmp_detect/"+imname+"_detect.png",img2) img = cv2.resize(img,(trg_size, trg_size)) cv2.imwrite("tmp_ims/" + imname + ".png",img) #####CNN fitting ############################## start_time = time.time() # load net try: caffe.set_mode_gpu() caffe.set_device(GPU_ID) except Exception as ex:
dlib_img = io.imread(image_path) img2 = cv2.copyMakeBorder(img,0,0,0,0,cv2.BORDER_REPLICATE) dets = detector(img, 1) print("> Number of faces detected: {}".format(len(dets))) if len(dets) == 0: print('> Could not detect the face, skipping the image...' + image_path) continue if len(dets) > 1: print("> Process only the first detected face!") detected_face = dets[0] ## If we are using landmarks to crop shape = predictor(dlib_img, detected_face) nLM = shape.num_parts for i in range(0,nLM): cv2.circle(img2, (shape.part(i).x, shape.part(i).y), 5, (255,0,0)) img, lms = utils.cropByLM(img, shape, img2) cv2.imwrite(data_out + "/imgs/"+imname+"_detect.png",img2) lms = lms * 500.0/img.shape[0] fileout = open(data_out + "/imgs/"+imname + ".pts","w") for i in range(0,lms.shape[0]): fileout.write("%f %f\n" % (lms[i,0], lms[i,1])) fileout.close() img = cv2.resize(img,(500, 500)) cv2.imwrite(data_out + "/imgs/"+imname+ ".png",img) outs.write("%s\n" % (data_out + "/imgs/"+imname+ ".png")) countIms = countIms + 1 ################################################## ##### Shape fitting ############################## # load net
def run(): ################################################### ##### Prepare images ############################## countIms = 0 with open(fileList, "r") as ins, open(data_out + "/imList.txt","w") as outs: for image_path in ins: # image_path = image_path[:-1] print("> Prepare image "+image_path + ":") imname = ntpath.basename(image_path) #imname = imname[:-4] imname = imname.split(imname.split('.')[-1])[0][0:-1] img = cv2.imread(image_path) ## If we have input landmarks if len(landmarkDir) > 0: lms = np.loadtxt(landmarkDir + '/' + imname + '.pts') img2 = cv2.copyMakeBorder(img,0,0,0,0,cv2.BORDER_REPLICATE) nLM = lms.shape[0] for i in range(0,nLM): cv2.circle(img2, (lms[i,0], lms[i,1]), 5, (255,0,0)) img, lms = utils.cropByInputLM(img, lms, img2) else: dlib_img = io.imread(image_path) img2 = cv2.copyMakeBorder(img,0,0,0,0,cv2.BORDER_REPLICATE) dets = detector(img, 1) print("> Number of faces detected: {}".format(len(dets))) if len(dets) == 0: print('> Could not detect the face, skipping the image...' + image_path) continue if len(dets) > 1: print("> Process only the first detected face!") detected_face = dets[0] ## If we are using landmarks to crop shape = predictor(dlib_img, detected_face) nLM = shape.num_parts for i in range(0,nLM): cv2.circle(img2, (shape.part(i).x, shape.part(i).y), 5, (255,0,0)) img, lms = utils.cropByLM(img, shape, img2) cv2.imwrite(data_out + "/imgs/"+imname+"_detect.png",img2) lms = lms * 500.0/img.shape[0] fileout = open(data_out + "/imgs/"+imname + ".pts","w") for i in range(0,lms.shape[0]): fileout.write("%f %f\n" % (lms[i,0], lms[i,1])) fileout.close() img = cv2.resize(img,(500, 500)) cv2.imwrite(data_out + "/imgs/"+imname+ ".png",img) outs.write("%s\n" % (data_out + "/imgs/"+imname+ ".png")) countIms = countIms + 1 ################################################### ##### Shape fitting ############################## # load net MainModel = imp.load_source('MainModel', "../CNN/shape_model.py") net = torch.load(model_path) net.eval() mean0 = np.load(mean_path, encoding='latin1') mean = mean0['arr_0'] net.cuda() print('> CNN Model loaded to regress 3D Shape and Texture!') model = scipy.io.loadmat(BFM_path,squeeze_me=True,struct_as_record=False) model = model["BFM"] faces = model.faces-1 print('> Loaded the Basel Face Model to write the 3D output!') ## For loop over the input images count = 0 with open(data_out + "/imList.txt", "r") as ins: for image_path in ins: if len(image_path) < 3: continue image_path = image_path[:-1] count = count + 1 fig_name = ntpath.basename(image_path) outFile = data_out + "/shape/" + fig_name[:-4] print('> Processing image: ' + image_path) im = cv2.imread(image_path) im = cv2.resize(im, (224, 224)).astype(float).transpose((2,0,1)) im = im - mean #im = im/255 im = Variable(torch.from_numpy(im).unsqueeze(0).float().cuda()) features = net(im).data.cpu().numpy() ## Writing the regressed 3DMM parameters np.savetxt(outFile + '.ply.alpha', features[0,0:99]) S,T = utils.projectBackBFM(model,features[0,:]) print('> Writing 3D file in: ', outFile + '.ply') utils.write_ply(outFile + '.ply', S, T, faces) ################################################## ##### Bump map regression ######################## print("Regress bump maps") bumpMapRegressor.estimateBump(bumpModel_path, data_out + "/imList.txt", data_out + "/bump/") ################################################## ##### Recover the 3D models ################## print("Recover the 3D models") print("./TestBump -batch " + data_out + "/imList.txt " + data_out + "/3D/ " + data_out + "/shape " + data_out + "/bump " + data_out + "/bump ../3DMM_model/BaselFaceModel_mod.h5 ../dlib_model/shape_predictor_68_face_landmarks.dat " + data_out + "/imgs " + data_out + "/imgs/ 1"); os.system("./TestBump -batch " + data_out + "/imList.txt " + data_out + "/3D/ " + data_out + "/shape " + data_out + "/bump " + data_out + "/bump ../3DMM_model/BaselFaceModel_mod.h5 ../dlib_model/shape_predictor_68_face_landmarks.dat " + data_out + "/imgs " + data_out + "/imgs/ 1");