def on_frame(output_pipe, frame, video_file):
    compression_factor = 0.2

    frame_copy = resize_image(frame, compression_factor)
    quadriliterals = detect_quadrilaterals(frame_copy)

    for quadriliteral in quadriliterals:
        for point in quadriliteral:
            point[0][0] *= 1 / compression_factor
            point[0][1] *= 1 / compression_factor

    if output_pipe.writable:
        output_pipe.send((quadriliterals, frame, video_file))
    else:
        exit(0)
Ejemplo n.º 2
0
def run_room_calibration():
  filenames = glob('./datasets/images/dataset_pictures_msk/zaal_*/*.jpg')

  probability_per_room = {} # key -> room, value -> count & average probability for the room

  for f in filenames:
    original_image = cv2.imread(f, 1)
    resized_image = resize_image(original_image, 0.2)
    image = resized_image.copy()
    detected_paintings = detect_quadrilaterals(image)

    probabilities = predict_room(resized_image, detected_paintings, 0.5)

    match = re.search('(z|Z)aal_(.+)$', os.path.dirname(f))
    room = match.group(2)

    if room not in probability_per_room:
      probability_per_room[room] = (0, 0)

    for painting_probabilities in probabilities:
      index = 0

      while index < len(painting_probabilities) and painting_probabilities[index][2] != room: index+=1

      probability = 0
      if index < len(painting_probabilities):
        probability = painting_probabilities[index][0]

      new_count = probability_per_room[room][0] + 1
      new_avg_probability = ((probability_per_room[room][0] * probability_per_room[room][1]) + probability) / new_count
      probability_per_room[room] = (new_count, new_avg_probability)

      logger.info("room: " + room + ", probability: " + str(probability))

  for room in probability_per_room:
    actual_number_of_paintings_in_room = get_painting_count_by_room(room)
    new_prob = probability_per_room[room][1] * probability_per_room[room][0] / actual_number_of_paintings_in_room
    if 1 < new_prob:
      new_prob = 1

    probability_per_room[room] = (actual_number_of_paintings_in_room, new_prob)

  with open('room_calibrations.pickle', 'wb') as handle:
    pickle.dump(probability_per_room, handle, protocol=pickle.HIGHEST_PROTOCOL)

  logger.info("Room calibrations: " + str(json.dumps(probability_per_room)))
def show_next_image(graph, filenames, file_number):
    """
    Display the image in the canvas (graph) at position file_counter in filenames.

    Parameters
    ----------
    - graph -- The canvas element to display the image in.
    - filenames -- All filenames.
    - file_number -- The file to display.

    Returns: The detected contours in the image with its coordinates relative to the current image size.
    """

    if len(filenames) <= file_number:
        return None

    filepath = filenames[file_number]

    logger.info('Loading next image; {}'.format(filepath))

    img = cv2.imread(filepath)

    factor = get_image_resize_factor(img)
    img = resize_image(img, factor)

    detected_contours = detect_contours(img)
    graph.erase()

    loc = get_image_location_in_graph(img)

    # reset the coordinate system of the graph to display the image
    graph.change_coordinates((0, graph_size[1]), (graph_size[0], 0))
    byte_string = image_to_byte_string(img)
    graph.DrawImage(data=byte_string, location=loc)

    (graph_width, graph_height) = graph_size[:2]
    # change the coordinate system of the canvas (graph) to be according to the displayed (centered) image
    graph.change_coordinates((-loc[0], graph_size[1] - loc[1]),
                             (graph_size[0] - loc[0], -loc[1]))

    return (detected_contours, filepath, img.shape[:2], img)
def run_task_03(dataset_folder='dataset_pictures_msk', show=True, save=True):
    if save:
        filename = create_file('./results/task3/' + dataset_folder + '_prediction')
        plot_filename_base = './results/task3/dataset_pictures_msk_'

    filenames = glob('./datasets/images/' + dataset_folder + '/zaal_*/*.jpg')

    correct_image_predictions = 0
    correct_room_predictions = 0
    total_predictions = 0

    correct_predictions_per_room = {}  # key -> room, value -> count
    incorrect_predictions_per_room = {}  # key -> room, value -> count

    for f in filenames:
        original_image = cv2.imread(f, 1)
        resized_image = resize_image(original_image, 0.2)
        image = resized_image.copy()
        detected_paintings = detect_quadrilaterals(image)

        probabilities = predict_room(resized_image, detected_paintings)

        match = re.search('(z|Z)aal_(.+)$', os.path.dirname(f))
        room = match.group(2)
        room_name = basename(dirname(f)).replace('_', ' ')
        image_filename = os.path.basename(f)

        for painting_probabilities in probabilities:
            total_predictions += 1

            # ignore if no probabilities for a detected painting (possibly no match above given threshold)
            if 0 == len(painting_probabilities): continue

            max_probability = painting_probabilities[0]

            # only sure about the correctness of the predicted image with the dataset_pictures_msk set
            if dataset_folder == 'dataset_pictures_msk':
                if image_filename == max_probability[1]:
                    correct_image_predictions += 1

            # always sure about the correctness of the predicted room due to the folder structure
            if room == max_probability[2]:
                correct_room_predictions += 1

                if room_name in correct_predictions_per_room:
                    correct_predictions_per_room[room_name] += 1
                else:
                    correct_predictions_per_room[room_name] = 1
            else:
                if room_name in incorrect_predictions_per_room:
                    incorrect_predictions_per_room[room_name] += 1
                else:
                    incorrect_predictions_per_room[room_name] = 1

            if save:
                append_file(filename, f + '\n')

            log = "probability: {}".format(max_probability[0])
            logger.info(log)
            if save:
                append_file(filename, log + '\n')

            log = "predicted image: {}".format(max_probability[1])
            logger.info(log)
            if save:
                append_file(filename, log + '\n')

            log = "actual image: {}".format(image_filename)
            logger.info(log)
            if save:
                append_file(filename, log + '\n')

            log = "predicted room: {}".format(max_probability[2])
            logger.info(log)
            if save:
                append_file(filename, log + '\n')

            log = "correct room: {}".format(room)
            logger.info(log)
            if save:
                append_file(filename, log + '\n\n')

        detection_image = draw_quadrilaterals_opencv(image, detected_paintings)

        if show:
            show_image(f, detection_image)

    log = "SUMMARY"
    logger.info(log)
    if save:
        append_file(filename, log + '\n')

    if dataset_folder == 'dataset_pictures_msk':
        log = "total # of correct painting predictions: {}/{}".format(correct_image_predictions, total_predictions)
        logger.info(log)
        if save:
            append_file(filename, log + '\n')

            save_plot(
                {'correct predictions': correct_predictions_per_room,
                 'incorrect predictions': incorrect_predictions_per_room},
                'Correct and incorrect painting predictions per room',
                plot_filename_base + 'correect_and_incorrect_predictions_per_room'
            )

    log = "total # of correct room predictions: {}/{}".format(correct_room_predictions, total_predictions)
    logger.info(log)
    if save:
        append_file(filename, log)
def run_task_03_uniqueness(show=True, save=True):
    imagesFromDB = get_all_images({
        'filename': 1,
        'room': 1,
        'corners': 1
    })

    correct_matchings = 0
    total_matchings = 0
    average_probability = 0

    if save:
        filename = create_file('./results/task3/dataset_pictures_msk_uniqueness')

    dataset_folder = './datasets/images/dataset_pictures_msk'

    for imageFromDB in imagesFromDB:
        path = dataset_folder + '/zaal_' + imageFromDB['room'] + '/' + imageFromDB['filename']
        original_image = cv2.imread(path, 1)
        resized_image = resize_image(original_image, 0.2)
        (height, width) = resized_image.shape[:2]

        corners = imageFromDB.get('corners')
        for point in corners:
            point[0] = point[0] * width
            point[1] = point[1] * height

        detected_paintings = [np.asarray(corners).astype(np.int32)]

        if show:
            detected_image = draw_quadrilaterals_opencv(resized_image, detected_paintings)
            show_image(imageFromDB['filename'], detected_image)

        probabilities = predict_room(resized_image, detected_paintings, 0)

        total_matchings += 1
        if 0 == len(probabilities): continue

        max_probability = probabilities[0]

        if max_probability[0] is not None and max_probability[0][1] == imageFromDB['filename']:
            correct_matchings += 1
            average_probability += max_probability[0][0]

            log = "image: {}".format(imageFromDB['filename'])
            logger.info(log)
            if save:
                append_file(filename, log + '\n')

            log = "probability: {}".format(max_probability[0][0])
            logger.info(log)
            if save:
                append_file(filename, log + '\n\n')

    log = "SUMMARY"
    logger.info(log)
    if save:
        append_file(filename, log + '\n')

    log = "total # of correct painting matchings: {}/{}".format(correct_matchings, total_matchings)
    logger.info(log)
    if save:
        append_file(filename, log + '\n')

    log = "average probability: {}".format(average_probability / total_matchings)
    logger.info(log)
    if save:
        append_file(filename, log)