コード例 #1
0
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
コード例 #2
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()
コード例 #3
0
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()
コード例 #4
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()
コード例 #5
0
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
コード例 #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
コード例 #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
コード例 #8
0
 def getEmbeddingFromBody(self, body):
     printv("Getting embedding from body image...")
     embedding = self.bodyEmbeddingGenerator.getEmbedding(body)
     printDone()
     return embedding
コード例 #9
0
def cmcTimeSpaceHeuristic(gallery, query, topNum, raceRank,
                          windowSize=defaultWindowSize, shiftProp=defaultShiftProp, shiftStep=defaultShiftStep,
                          fps=defaultFps, timeout=defaultTimeout):
    """
    Calculates the CMC curve using time and space 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.
        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.
    """

    print("Calculating CMC curve with time and space heuristic...")
    embeddingCount = 0
    matches = np.zeros(topNum)

    windowStart = 0
    shiftSize = shiftProp * windowSize

    idCounter = 0
    seenDict = {}

    # 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(
                windowStart,
                min(windowStart + windowSize, len(gallery))
            ))

            # Obtains the ids of each ranking positions, removing
            # the ones that have been seen with timeout 0
            possibleIds = set(
                [raceRank[position] for position in windowPositions]
            ).union(
                dict(filter(lambda item: item[1] > 0, seenDict.items())).keys()
            )

            ranking = rankIds(gallery, embedding, possibleIds)

            # If the previous id rank was lower than the window
            # position plus the shift size, the window shifts
            if ranking[0][0] not in seenDict.keys():
                if raceRank.index(ranking[0][0]) > (windowStart + shiftSize):
                    windowStart += shiftStep
                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 i in seenDict.keys():
                if i != ranking[0][0] and seenDict[i] != 0:
                    seenDict[i] = max(seenDict[i] - 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
コード例 #10
0
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