def displayFaceCompareResult(sourceFaceId): faceDB = FaceData.getFaceData() compareFace = FaceUtils.compareFaceByOthers(sourceFaceId) faceData = faceDB.findFaceById(sourceFaceId) faceFilePath = faceData['imagePath'] faceId = faceData['faceId'] faceImage = cv.imread(faceFilePath) namedWindowName = 'test_' + str(faceId) cv.namedWindow(namedWindowName) cv.imshow(namedWindowName, faceImage) for valueObj in compareFace: faceId = valueObj[0] similarValue = valueObj[1] if (similarValue <= 0.42): faceData = faceDB.findFaceById(faceId) faceFilePath = faceData['imagePath'] faceId = faceData['faceId'] faceImage = cv.imread(faceFilePath) namedWindowName = 'test_' + str(faceId) cv.namedWindow(namedWindowName) cv.imshow(namedWindowName, faceImage) else: break cv.waitKey(0) cv.destroyAllWindows()
def loadAllFaceFeatures(): faceDB = FaceData.getNewFaceData() faceDataList = faceDB.getAllFaces() allFaceList = {} for faceData in faceDataList: featurePath = faceData['featurePath'] faceId = faceData['faceId'] if (len(featurePath) > 0): face_array = np.load(featurePath) allFaceList[faceId] = face_array return allFaceList
def compareFaceByOthers(faceId): faceDB = FaceData.getNewFaceData() faceData = faceDB.findFaceById(faceId) if (faceData != None): featureFilePath = faceData['featurePath'] print("开始load feature:" + featureFilePath) currentFeature = np.load(featureFilePath) faceCompareResult = {} allFaceFeature = loadAllFaceFeatures() for key, otherFaceFeature in allFaceFeature.items(): if (key != faceId): distance = (np.linalg.norm(currentFeature - otherFaceFeature)) faceCompareResult[key] = distance sortedFaces = sorted(faceCompareResult.items(), key=lambda d: d[1]) print(sortedFaces) return sortedFaces
def updateMostSimilarPerson(faceId): faceDB = FaceData.getFaceData() compareFace = FaceUtils.compareFaceByOthers(faceId) for valueObj in compareFace: compareFaceId = valueObj[0] similarValue = valueObj[1] compareFaceObj = faceDB.findFaceById(compareFaceId) compareFaceAssignStatus = compareFaceObj['assignedStatus'] if (compareFaceAssignStatus == 'U' or compareFaceAssignStatus == 'A'): if (similarValue <= 0.4): compareFaceData = faceDB.findFaceById(compareFaceId) comparePersonId = compareFaceData['personId'] faceDB.changeFacePerson(faceId, comparePersonId, 'A') print('找到相似的脸' + str(comparePersonId)) else: faceDB.changeFacePerson(faceId, Constants.PERSON_ID_UNNAMED, 'U') print('没有相似的脸, 更新为 匿名') else: print('这张脸手动改过') break
def getFaceInDBByRawImage(rawImageName): faceDB = FaceData.getFaceData() return faceDB.searchFaceByRawImageFileName(rawImageName)
def updateFaceFeatureFile(faceId, featurePath): faceDB = FaceData.getFaceData() return faceDB.updateFaceFeature(faceId, featurePath)
def createNewFaceForPerson(facePath, rawImagePath, personId): faceDB = FaceData.getFaceData() return faceDB.newFace(rawImagePath, facePath, personId)
def createNewPersonFace(facePath, rawImagePath): faceDB = FaceData.getFaceData() personId = faceDB.newPerson(None) return faceDB.newFace(rawImagePath, facePath, personId)