Ejemplo n.º 1
0
    def predict_emotions(self, faces, gray_image):
        player_data = []
        emotion_idx_lookup = get_class_to_arg()
        for face_coordinates in faces:
            x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets)
            gray_face = gray_image[y1:y2, x1:x2]
            gray_face = cv2.resize(gray_face, emotion_target_size)
            gray_face = preprocess_input(gray_face, True)
            gray_face = np.expand_dims(gray_face, 0)
            gray_face = np.expand_dims(gray_face, -1)
            emotion_prediction = emotion_classifier.predict(gray_face)
            emotion_index = emotion_idx_lookup[self.current_emotion]
            print("EMOTION INDEX: ", emotion_index, emotion_prediction)
            emotion_score = emotion_prediction[0][emotion_index]
            self.current_emotion_score = emotion_score

            if self.debug:  # Show all emotions for each face
                self.show_all_emotions(emotion_prediction, face_coordinates)

            x, y, w, h = face_coordinates
            face_dict = {'left': x, 'top': y, 'right': x + w, 'bottom': y + h}
            player_data.append({
                'faceRectangle': face_dict,
                'scores': emotion_prediction[0]
            })
        return player_data
Ejemplo n.º 2
0
def predict_emotions(faces, gray_image, current_emotion='happy'):
    global graph
    player_data = []
    # Hyperparameters for bounding box
    emotion_offsets = (20, 40)
    emotion_idx_lookup = get_class_to_arg()
    for face_coordinates in faces:
        x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets)
        gray_face = gray_image[y1:y2, x1:x2]
        try:
            gray_face = cv2.resize(gray_face, emotion_target_size)
        except Exception as e:
            print("Exception:", e)
            return player_data
        gray_face = preprocess_input(gray_face, True)
        gray_face = np.expand_dims(gray_face, 0)
        gray_face = np.expand_dims(gray_face, -1)
        with graph.as_default():
            emotion_prediction = emotion_classifier.predict(gray_face)
        emotion_index = emotion_idx_lookup[current_emotion]
        # app.logger.debug("EMOTION INDEX: ", emotion_index)
        emotion_score = emotion_prediction[0][emotion_index]
        x, y, w, h = face_coordinates
        face_dict = {'left': x, 'top': y, 'right': x + w, 'bottom': y + h}
        player_data.append({
            'faceRectangle': face_dict,
            'scores': emotion_prediction[0]
        })
    return player_data
Ejemplo n.º 3
0
    def rank_players(self, player_data):
        """ Rank players and display.

        Args:
            player_data : list of dicts
        """
        scores = []
        max_first_emo = None
        max_second_emo = None
        first_emotion = None
        emotion_idx_lookup = get_class_to_arg()
        # Get lists of player points.
        first_emotion_idx = emotion_idx_lookup[self.current_emotion]
        second_emotion_idx = emotion_idx_lookup[self.second_current_emotion]
        first_emotion_scores = [(round(x['scores'][first_emotion_idx] * 100))
                                for x in player_data]
        second_emotion_scores = [(round(x['scores'][second_emotion_idx] * 100))
                                 for x in player_data]

        # Collect scores into `scores_list`.
        scores_list = []
        if self.easy_mode:  # rank players by one emotion
            scores_list = first_emotion_scores
        else:  # hard mode scores are a product of percentage of both emotions
            for i in range(len(first_emotion_scores)):
                scores_list.append((first_emotion_scores[i] + 1) *
                                   (second_emotion_scores[i] + 1))
        text_size = 0.5 if self.raspberry else 0.8
        # Draw the scores for the faces.
        for i, currFace in enumerate(player_data):
            faceRectangle = currFace['faceRectangle']

            # Get points for first emotion.
            first_emotion = first_emotion_scores[i]
            second_emotion = second_emotion_scores[i]

            # Format points.
            if first_emotion == 1:  # singular 'point'
                first_emotion_caption = "%i point: %s" % (first_emotion,
                                                          self.current_emotion)
            else:
                first_emotion_caption = "%i points: %s" % (
                    first_emotion, self.current_emotion)
            if second_emotion == 1:  # singular 'point'
                second_emotion_caption = "%i point: %s" % (
                    second_emotion, self.second_current_emotion)
            else:
                second_emotion_caption = "%i points: %s" % (
                    second_emotion, self.second_current_emotion)

            # Display points.
            score_height_offset = 10 if self.easy_mode else 40
            first_emotion_coord = (faceRectangle['left'],
                                   faceRectangle['top'] - score_height_offset)
            draw_text(first_emotion_coord,
                      self.photo,
                      first_emotion_caption,
                      font_scale=text_size,
                      color=YELLOW)

            if not self.easy_mode:  # second line
                second_emotion_coord = (faceRectangle['left'],
                                        faceRectangle['top'] - 10)
                draw_text(second_emotion_coord,
                          self.photo,
                          second_emotion_caption,
                          color=YELLOW,
                          font_scale=text_size)

            # Display 'Winner: ' above player with highest score.
            one_winner = True
            final_scores = scores_list
            winner = final_scores.index(max(final_scores))
            max_score = max(final_scores)

            # Multiple winners - tie breaker.
            if final_scores.count(max_score) > 1:
                print("Multiple winners!")
                one_winner = False
                tied_winners = []
                for ind, i in enumerate(final_scores):
                    if i == max_score:
                        tied_winners.append(ind)

            # Identify winner's face.
            first_rect_left = player_data[winner]['faceRectangle']['left']
            first_rect_top = player_data[winner]['faceRectangle']['top']
            self.crown_over_faces = []
            if one_winner:
                tied_text_height_offset = 40 if self.easy_mode else 70
                draw_text((first_rect_left,
                           first_rect_top - tied_text_height_offset),
                          self.photo,
                          "Winner: ",
                          color=YELLOW,
                          font_scale=text_size)
                self.crown_over_faces = [winner]
            else:
                tied_text_height_offset = 40 if self.easy_mode else 70
                print("tied_winners:", tied_winners)
                for winner in tied_winners:
                    # FIXME: show both
                    first_rect_left = player_data[winner]['faceRectangle'][
                        'left']
                    first_rect_top = player_data[winner]['faceRectangle'][
                        'top']
                    tied_coord = (first_rect_left,
                                  first_rect_top - tied_text_height_offset)
                    draw_text(tied_coord,
                              self.photo,
                              "Tied: ",
                              color=YELLOW,
                              font_scale=text_size)
                self.crown_over_faces = tied_winners
Ejemplo n.º 4
0
def rank_players(player_data,
                 photo,
                 current_emotion='happy',
                 one_player=False):
    """ Rank players and display.

    Args:
        player_data : list of dicts
        photo : numpy nd array
    """

    text_size = 0.5
    if len(player_data) < 1:
        draw_text((0.2 * photo.shape[0], 0.2 * photo.shape[1]),
                  photo,
                  "No faces found - try again!",
                  font_scale=text_size,
                  color=YELLOW)
        if one_player:
            return photo, [], 1
        else:
            return photo, []
    scores = []
    first_emotion = None
    easy_mode = True
    emotion_idx_lookup = get_class_to_arg()
    # Get lists of player points.
    first_emotion_idx = emotion_idx_lookup[current_emotion]
    # second_emotion_idx = emotion_idx_lookup[second_current_emotion]
    first_emotion_scores = [(round(x['scores'][first_emotion_idx] * 100))
                            for x in player_data]

    # Collect scores into `scores_list`.
    scores_list = []
    # if easy_mode:  # rank players by one emotion
    scores_list = first_emotion_scores

    emotion_offsets = (20, 40)

    largest_face = 0
    player_index = None

    faces_with_scores = []

    # Draw the scores for the faces.
    for i, currFace in enumerate(player_data):
        faceRectangle = currFace['faceRectangle']
        x1, x2 = faceRectangle['left'], faceRectangle['right']
        y1, y2 = faceRectangle['top'], faceRectangle['bottom']

        if one_player:  # mode
            if (x2 - x1) * (y2 - y1) > largest_face:
                largest_face = largest_face
                player_index = i

        # Convert back to coordinates to get offset
        face_coordinates = (x1, y1, x2 - x1, y2 - y1)
        # Get points for first emotion.
        first_emotion = first_emotion_scores[i]
        face_photo_path = 'static/images/face_{}.jpg'.format(str(uuid.uuid4()))
        x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets)
        face_image = photo[y1:y2, x1:x2]
        cv2.imwrite(face_photo_path, face_image)
        app.logger.info("Saved face to {}".format(face_photo_path))
        faces_with_scores.append((face_photo_path, first_emotion))
        # second_emotion = second_emotion_scores[i]

        # Format points.
        if first_emotion == 1:  # singular 'point'
            first_emotion_caption = "%i point: %s" % (first_emotion,
                                                      current_emotion)
        else:
            first_emotion_caption = "%i points: %s" % (first_emotion,
                                                       current_emotion)
        #
        # Display points.
        score_height_offset = 10
        first_emotion_coord = (faceRectangle['left'],
                               faceRectangle['top'] - score_height_offset)
        draw_text(first_emotion_coord,
                  photo,
                  first_emotion_caption,
                  font_scale=text_size,
                  color=YELLOW)

        # Display 'Winner: ' above player with highest score.
        one_winner = True
        final_scores = scores_list
        winner = final_scores.index(max(final_scores))
        max_score = max(final_scores)

        # Multiple winners - tie breaker.
        if final_scores.count(max_score) > 1:
            app.logger.info("Multiple winners!")
            one_winner = False
            tied_winners = []
            for ind, i in enumerate(final_scores):
                if i == max_score:
                    tied_winners.append(ind)

        # Identify winner's face.
        first_rect_left = player_data[winner]['faceRectangle']['left']
        first_rect_top = player_data[winner]['faceRectangle']['top']
        crown_over_faces = []
        if one_winner:
            tied_text_height_offset = 40 if easy_mode else 70
            draw_text(
                (first_rect_left, first_rect_top - tied_text_height_offset),
                photo,
                "Winner: ",
                color=YELLOW,
                font_scale=text_size)
            crown_over_faces = [winner]
        else:
            tied_text_height_offset = 40 if easy_mode else 70
            app.logger.info("tied_winners:", tied_winners)
            for winner in tied_winners:
                # FIXME: show both
                first_rect_left = player_data[winner]['faceRectangle']['left']
                first_rect_top = player_data[winner]['faceRectangle']['top']
                tied_coord = (first_rect_left,
                              first_rect_top - tied_text_height_offset)
                draw_text(tied_coord,
                          photo,
                          "Tied: ",
                          color=YELLOW,
                          font_scale=text_size)
            # crown_over_faces
    if one_player:
        return photo, faces_with_scores, player_index
    return photo, faces_with_scores
Ejemplo n.º 5
0
def rank_players(player_data, photo, current_emotion='happy'):
    """ Rank players and display.

    Args:
        player_data : list of dicts
        photo : numpy nd array
    """

    text_size = 0.5
    if len(player_data) < 1:
        draw_text(
            (0.2 * photo.shape[0], 0.2 * photo.shape[1]),
            photo,
            "No faces found - try again!",
            font_scale=text_size,
            color=YELLOW)
        return photo
    scores = []
    first_emotion = None
    easy_mode = True
    emotion_idx_lookup = get_class_to_arg()
    # Get lists of player points.
    first_emotion_idx = emotion_idx_lookup[current_emotion]
    # second_emotion_idx = emotion_idx_lookup[second_current_emotion]
    first_emotion_scores = [(round(x['scores'][first_emotion_idx] * 100))
                            for x in player_data]

    # Collect scores into `scores_list`.
    scores_list = []
    # if easy_mode:  # rank players by one emotion
    scores_list = first_emotion_scores

    # Draw the scores for the faces.
    for i, currFace in enumerate(player_data):
        faceRectangle = currFace['faceRectangle']

        # Get points for first emotion.
        first_emotion = first_emotion_scores[i]
        # second_emotion = second_emotion_scores[i]

        # Format points.
        if first_emotion == 1:  # singular 'point'
            first_emotion_caption = "%i point: %s" % (first_emotion,
                                                      current_emotion)
        else:
            first_emotion_caption = "%i points: %s" % (first_emotion,
                                                       current_emotion)
        #
        # Display points.
        score_height_offset = 10
        first_emotion_coord = (faceRectangle['left'],
                               faceRectangle['top'] - score_height_offset)
        draw_text(
            first_emotion_coord,
            photo,
            first_emotion_caption,
            font_scale=text_size,
            color=YELLOW)

        # Display 'Winner: ' above player with highest score.
        one_winner = True
        final_scores = scores_list
        winner = final_scores.index(max(final_scores))
        max_score = max(final_scores)

        # Multiple winners - tie breaker.
        if final_scores.count(max_score) > 1:
            print("Multiple winners!")
            one_winner = False
            tied_winners = []
            for ind, i in enumerate(final_scores):
                if i == max_score:
                    tied_winners.append(ind)

        # Identify winner's face.
        first_rect_left = player_data[winner]['faceRectangle']['left']
        first_rect_top = player_data[winner]['faceRectangle']['top']
        crown_over_faces = []
        if one_winner:
            tied_text_height_offset = 40 if easy_mode else 70
            draw_text(
                (first_rect_left, first_rect_top - tied_text_height_offset),
                photo,
                "Winner: ",
                color=YELLOW,
                font_scale=text_size)
            crown_over_faces = [winner]
        else:
            tied_text_height_offset = 40 if easy_mode else 70
            print("tied_winners:", tied_winners)
            for winner in tied_winners:
                # FIXME: show both
                first_rect_left = player_data[winner]['faceRectangle']['left']
                first_rect_top = player_data[winner]['faceRectangle']['top']
                tied_coord = (first_rect_left,
                              first_rect_top - tied_text_height_offset)
                draw_text(
                    tied_coord,
                    photo,
                    "Tied: ",
                    color=YELLOW,
                    font_scale=text_size)
            # crown_over_faces
    return photo