def cmcTimeHeuristic(gallery, query, topNum, raceRank, windowSize=defaultWindowSize, shiftProp=defaultShiftProp,
                     shiftStep=defaultShiftStep):
    """
    Calculates the CMC curve using time heuristic.

    Arguments:
        gallery -- the gallery set of embeddings.
        query -- the query set of embeddings.
        topNum -- the top size.
        raceRank -- the previous checkpoint rank.
        windowSize -- the size of the window for time heuristic.
        shiftProp -- the position difference proportion to consider shifting for time heuristic.
        shiftStep -- the size of the step for time heuristic.

    Returns:
        The CMC curve in a list.
    """

    printv("Calculating CMC curve with time heuristic...")
    embeddingCount = 0
    windowStart = 0

    shiftSize = shiftProp * windowSize

    matches = np.zeros(topNum)

    # For each query identity, we iterate
    # for each embedding.
    for id in query:
        for embedding in query[id]:
            # Obtains the ranking position that will be
            # used in this iteration
            windowPositions = list(range(
                max(windowStart - windowSize, 0),
                min(windowStart + windowSize + 1, len(raceRank))
            ))

            # Obtains the ids of each positions
            possibleIds = [raceRank[position] for position in windowPositions]

            # Obtains the ranking
            ranking = rankIds(gallery, embedding, possibleIds)

            # If the previous id rank was lower than the window
            # position plus the shift size, the window shifts
            if raceRank.index(ranking[0][0]) > (windowStart + shiftSize):
                windowStart += shiftStep

            # Evaluates the obtained rank
            for rank in range(0, topNum):
                if ranking[rank][0] == id:
                    matches[rank] += 1
                    break
            embeddingCount += 1

    # Obtains the accumulated probability
    results = np.cumsum(matches) / embeddingCount
    print(results)
    printDone()
    return matches, results
def plotCMC(result):
    """
    Plots the given CMC curve result.
    """
    printv("Plotting the CMC curve...", False)
    plt.figure(figsize=(9, 9))
    top = range(1, len(result)+1)
    plt.plot(top, result)
    plt.yticks(np.arange(0, 1.05, 0.05))
    plt.xticks(top)
    plt.xlabel("Ranks")
    plt.ylabel("Probability")
    plt.title("CMC curve")
    plt.grid()
    plt.show()
    printDone()
def cmc(gallery, query, topNum):
    """
    Calculates the CMC curve.

    Arguments:
        gallery -- the gallery set of embeddings.
        query -- the query set of embeddings.
        topNum -- the top size.

    Returns:
        The CMC curve in a list.
    """

    printv("Calculating CMC curve...")
    embeddingCount = 0
    matches = np.zeros(topNum)

    # For each query identity, we iterate
    # for each embedding.
    for id in query:
        for embedding in query[id]:
            # We obtain the ranking
            ranking = rankIds(gallery, embedding, gallery.keys())

            # Evaluates the given ranking
            for rank in range(0, topNum):
                if ranking[rank][0] == id:
                    matches[rank] += 1
                    break
            embeddingCount += 1

    # Obtains the accumulated probability
    results = np.cumsum(matches) / embeddingCount
    print(results)
    printDone()
    return matches, results
Exemple #4
0
 def startIdentificationByCam(self):
     printv("Starting identification using webcam...")
     identify = self.detectFacesInFrame if self.detector is Detector.mtcnn else self.identifyBodyInFrame
     cap = cv.VideoCapture(0)
     if self.detector is Detector.mtcnn:
         printv("WARNING: You are using MTCNN. Identity will not be computed. Use a body detector if you want to compute the identity.")
     printv("⚡️ Identification using webcam started, use key 'q' to quit the process.")
     while True:
         _, frame = cap.read()
         newFrame = identify(deinterlaceImages([frame])[0])
         cv.imshow(self.frameLabel, newFrame)
         if cv.waitKey(1) & 0xFF == ord('q'):
             break
     cap.release()
     cv.destroyAllWindows()
     printDone()
Exemple #5
0
 def startIdentificationByVideo(self, videoPath):
     printv("Starting identification by video...")
     if not path.exists(videoPath):
         raise FileNotFoundError("Video path does not exist")
     identify = self.detectFacesInFrame if self.detector is Detector.mtcnn else self.identifyBodyInFrame
     if self.detector is Detector.mtcnn:
         printv("WARNING: You are using MTCNN. Identity will not be computed. Use a body detector if you want to compute the identity.")
     cap = cv.VideoCapture(videoPath)
     printv("⚡️ Identification by video started, use key 'q' to quit the process.")
     while (cap.isOpened()):
         _, frame = cap.read()
         newFrame = identify(deinterlaceImages([frame])[0])
         cv.imshow(self.frameLabel, newFrame)
         if cv.waitKey(1) & 0xFF == ord('q'):
             break
     cap.release()
     cv.destroyAllWindows()
     printDone()
Exemple #6
0
 def getBodiesFromImage(self, image):
     printv("Getting body images from image...")
     boxes = self.bodyDetector.getBoundingBoxes(image)[1]
     bodies = [box.getImageFromBox(image) for box in boxes]
     printDone()
     return bodies
Exemple #7
0
 def getFacesFromImage(self, image):
     printv("Getting faces images from image...")
     boxes = [face.boundingBox for face in self.faceDetector.getFaces(image)]
     faces = [box.getImageFromBox(image) for box in boxes]
     printDone()
     return faces
Exemple #8
0
 def getEmbeddingFromBody(self, body):
     printv("Getting embedding from body image...")
     embedding = self.bodyEmbeddingGenerator.getEmbedding(body)
     printDone()
     return embedding
def cmcSpaceHeuristic(gallery, query, topNum, raceRank, fps=defaultFps, timeout=defaultTimeout):
    """
    Calculates the CMC curve using space heuristic.

    Arguments:
        gallery -- the gallery set of embeddings.
        query -- the query set of embeddings.
        topNum -- the top size.
        raceRank -- the previous checkpoint rank.
        fps -- the image feeder frame per second value for space heuristic.
        timeout -- the amount time elapsed to consider an identity as not possible for space heuristic.

    Returns:
        The CMC curve in a list.
    """

    printv("Calculating CMC curve with space heuristic...")
    embeddingCount = 0
    matches = np.zeros(topNum)
    ids = list(gallery.keys())

    seenDict = {}
    idCounter = 0

    # For each query identity, we iterate
    # for each embedding.
    for id in query:
        for embedding in query[id]:
            # Seen identities with 0-value timeout
            # get filtered.
            possibleIds = filter(
                lambda id: id not in list(seenDict.keys()) or seenDict[id] > 0.,
                ids
            )

            ranking = rankIds(gallery, embedding, possibleIds)

            if ranking[0][0] not in seenDict.keys():
                idCounter += 1

            # Winner timeout is reset
            seenDict[ranking[0][0]] = timeout + (
                    timeout * max(raceRank.index(ranking[0][0]) - idCounter, 0)
            )

            # Seen identities counter gets updated
            for seenId in seenDict.keys():
                if seenId != ranking[0][0]:
                    seenDict[seenId] = max(seenDict[seenId] - (1. / fps), 0)

            # Evaluates the obtained rank
            for rank in range(0, min(topNum, len(ranking))):
                if ranking[rank][0] == id:
                    matches[rank] += 1
                    break
            embeddingCount += 1

    # Obtains the accumulated probability
    results = np.cumsum(matches) / embeddingCount
    print(results)
    printDone()
    return matches, results
Exemple #10
0
 else:
     identifier = buildPersonIdentifier(args)
     if args.video is not None:
         identifier.startIdentificationByVideo(args.video)
     elif args.camera:
         identifier.startIdentificationByCam()
     elif args.embedding is not None:
         checkPath(args.embedding, "Body image", True)
         if args.mtcnn:
             raise ValueError(
                 "You can't generate an embedding using MTCNN. Please, use a body detector and a body embedding generator.")
         body = loadImage(args.embedding)
         embedding = identifier.getEmbeddingFromBody(body)
         if args.savePath is None:
             raise FileNotFoundError("Save path is missing. You can set it using --savePath option.")
         printv("Saving embedding at %s" % (args.savePath))
         persistEmbedding(args.savePath, embedding)
     elif args.detection is not None:
         checkPath(args.saveFolder, "Detection save folder", False)
         checkPath(args.detection, "Image", True)
         basename = path.splitext(path.basename(args.detection))[0]
         image = loadImage(args.detection)
         if args.boundingBox:
             detect = identifier.detectFacesInFrame if args.mtcnn else identifier.detectBodiesInFrame
             persistImage(
                 args.saveFolder,
                 "%s.png" % (basename),
                 detect(image)
             )
         else:
             detect = identifier.getFacesFromImage if args.mtcnn else identifier.getBodiesFromImage