def createWebfaceRawData(imgSize, saveFilePath): start = 0 peopleNum = 2500 singleSubNum = 20 datax = [] datay = [] dirlist = sorted(os.listdir(webfaceRoot))[start:start + peopleNum] for index, dir in enumerate(dirlist): print("%d people running..." % index) for root, d, files in os.walk(webfaceRoot + dir): print("img num:", len(files)) for file in files[:singleSubNum]: imgPath = webfaceRoot + dir + "/" + file img = cv2.imread(imgPath) try: faceImg = findAlignFace_dlib(img, imgSize) except: continue datax.append(faceImg) datay.append(index) datax = np.array(datax) datay = np.array(datay) f = h5py.File(saveFilePath, 'w') f['data'] = datax f['labels'] = datay f.close()
def runLFW(model, trainLabel, imgSize): acc = 0 posScore = [] negScore = [] count = 0 posGen = getPosPairsImg() for img1, img2 in posGen: count += 1 if count > 10: break try: faceImg1 = findAlignFace_dlib(img1, imgSize) faceImg2 = findAlignFace_dlib(img2, imgSize) except: continue faceImg1 = (faceImg1[np.newaxis, :, :, :]-127.5)/128.0 faceImg2 = (faceImg2[np.newaxis, :, :, :]-127.5)/128.0 rep1 = model.getRepVec(faceImg1, trainLabel) rep2 = model.getRepVec(faceImg2, trainLabel) score = calcCosSimilarityPairs(rep1, rep2) print(score) #posScore.append(score) #posCsv = pd.DataFrame(posScore) #posCsv.to_csv("./data/pos_score_"+modelName+".csv", index=False) count = 0 negGen = getNegPairsImg() for img1, img2 in negGen: count += 1 if count > 10: break try: faceImg1 = findAlignFace_dlib(img1, imgSize) faceImg2 = findAlignFace_dlib(img2, imgSize) except: continue faceImg1 = (faceImg1[np.newaxis, :, :, :] - 127.5) / 128.0 faceImg2 = (faceImg2[np.newaxis, :, :, :] - 127.5) / 128.0 rep1 = model.getRepVec(faceImg1, trainLabel) rep2 = model.getRepVec(faceImg2, trainLabel) score = calcCosSimilarityPairs(rep1, rep2) print(score)
def getRep_VGGface(rgbImg, version=2): alignedFace = findAlignFace_dlib(rgbImg, 224) alignedFace = alignedFace.astype(np.float64) x = np.expand_dims(alignedFace, axis=0) x = utils.preprocess_input(x, version=version) # or version=2 rep = model.predict(x) rep = np.reshape(rep, (2048, )) return rep
def gerRep_lightCNN_pytorch(rgbImg): faceImg = findAlignFace_dlib(rgbImg, 128) inputImg = torch.zeros(1, 1, 128, 128) grayImg = cv2.cvtColor(faceImg, cv2.COLOR_RGB2GRAY) img = np.reshape(grayImg, (128, 128, 1)) img = transform(img) inputImg[0, :, :, :] = img input = inputImg.cuda() input_var = torch.autograd.Variable(input, volatile=True) _, features = model(input_var) rep = features.data.cpu().numpy() rep = np.reshape(rep, (256, )) return rep
def createPairsWebface(saveFileName): posNum = 2 negNum = 2 imgSize = 128 trainPair1 = [] trainPair2 = [] label = [] peopleList = sorted(os.listdir(webfaceRoot)) # 获取所有人名列表 for index, dir in enumerate(peopleList): print("%d people running..." % index) # 选取anchor样本 posImgList = os.listdir(webfaceRoot + dir) anchorIndex = np.random.randint(0, len(posImgList)) anchor = posImgList[anchorIndex] anchorImgPath = webfaceRoot + dir + "/" + anchor print("anchor img path:", anchorImgPath) anchorImg = cv2.imread(anchorImgPath) try: anchorImg = findAlignFace_dlib(anchorImg, imgSize) except: continue anchorImg = cv2.cvtColor(anchorImg, cv2.COLOR_RGB2GRAY) anchorImg = anchorImg[:, :, np.newaxis] for i in range(negNum): # 选取负样本人 tmpPeopleList = copy.deepcopy(peopleList) del tmpPeopleList[index] # 除去本人的图片集 # 随机选取一个人作为负样本集 negPeople = tmpPeopleList[np.random.randint(0, len(tmpPeopleList))] negPeopleImgList = os.listdir(webfaceRoot + negPeople) # 随机选一张图片作为负样本 neg = negPeopleImgList[np.random.randint(0, len(negPeopleImgList))] negImgPath = webfaceRoot + negPeople + "/" + neg print("neg img path:", negImgPath) negImg = cv2.imread(negImgPath) try: negImg = findAlignFace_dlib(negImg, imgSize) except: continue negImg = cv2.cvtColor(negImg, cv2.COLOR_RGB2GRAY) negImg = negImg[:, :, np.newaxis] trainPair1.append(anchorImg) trainPair2.append(negImg) label.append(-1) for i in range(posNum): # 随机选取正样本 tmpPosImgList = copy.deepcopy(posImgList) del tmpPosImgList[anchorIndex] # 除去anchor图片 pos = posImgList[np.random.randint(0, len(posImgList))] posImgPath = webfaceRoot + dir + "/" + pos print("pos img path:", posImgPath) posImg = cv2.imread(posImgPath) try: posImg = findAlignFace_dlib(posImg, imgSize) except: continue posImg = cv2.cvtColor(posImg, cv2.COLOR_RGB2GRAY) posImg = posImg[:, :, np.newaxis] trainPair1.append(anchorImg) trainPair2.append(posImg) label.append(1) f = h5py.File(saveFileName, 'w') f['train_pair1'] = np.array(trainPair1) f['train_pair2'] = np.array(trainPair2) f['label'] = np.array(label) f.close()
def getRep_openface(rgbImg): alignedFace = findAlignFace_dlib(rgbImg, 96) rep = net.forward(alignedFace) return rep