Exemple #1
0
def process_1_img(im_path, model):
    resized = False
    frame = cv2.imread(im_path)
    if frame is None:
        return 2
    if frame.shape[0] > 1000:
        frame = my_resize(frame, width=900, height=900)
        resized = True
    im_grids_final, points_grids, list_transform_matrix = main_grid_detector_img(
        frame, resized=resized)
    if im_grids_final is None:
        return 3
    grids_matrix = process_extract_digits(im_grids_final,
                                          model,
                                          save_images_digit=False)
    if all(elem is None for elem in grids_matrix):
        return 4
    grids_solved = main_solve_grids(grids_matrix)

    if grids_solved is None:
        return 5

    ims_filled_grid = write_solved_grids(im_grids_final, grids_matrix,
                                         grids_solved)
    im_final = recreate_img_filled(frame, ims_filled_grid, points_grids,
                                   list_transform_matrix)

    cv2.imwrite(
        os.path.dirname(im_path) + "/solved/" +
        os.path.splitext(os.path.basename(im_path))[0] + "_solved.jpg",
        im_final)

    return 1
Exemple #2
0
def process_single_img(frame, model, save=False):
    # Resizing image
    if frame.shape[0] > 1000 or frame.shape[0] < 800:
        old_shape = frame.shape
        frame = my_resize(frame,
                          width=param_resize_width,
                          height=param_resize_height)
    else:
        old_shape = None

    # Extracting grids
    im_grids_final, points_grids, list_transform_matrix = main_grid_detector_img(
        frame)
    if im_grids_final is None:
        return frame

    # Generate matrix representing digits in grids
    grids_matrix = process_extract_digits(im_grids_final, model)
    if all(elem is None for elem in grids_matrix):
        return frame

    # Solving grids
    grids_solved = main_solve_grids(grids_matrix)

    if grids_solved is None:
        return frame

    ims_filled_grid = write_solved_grids(im_grids_final, grids_matrix,
                                         grids_solved)
    im_final = recreate_img_filled(frame, ims_filled_grid, points_grids,
                                   list_transform_matrix)

    if old_shape is not None:
        im_final = cv2.resize(im_final, old_shape[:2][::-1])

    if save:
        if not os.path.isdir(save_folder):
            os.makedirs(save_folder)
        cv2.imwrite(
            save_folder + os.path.splitext(os.path.basename(im_path))[0] +
            "_solved.jpg", im_final)

    return im_final
Exemple #3
0
def main_process_img(im_path,
                     model,
                     save=False,
                     display=False,
                     use_hough=True,
                     save_images_digit=False):
    init = time.time()
    frame = cv2.imread(
        im_path)  # TODO Check if image not well oriented - EXIF data
    init0 = time.time()
    if frame is None:
        logger.error("This path doesn't lead to a frame")
        sys.exit(3)
    if frame.shape[0] > 1000 or frame.shape[0] < 800:
        frame = my_resize(frame,
                          width=param_resize_width,
                          height=param_resize_height)
    im_grids_final, points_grids, list_transform_matrix = main_grid_detector_img(
        frame, display=display, use_hough=use_hough)
    found_grid_time = time.time()
    if im_grids_final is None:
        logger.error("No grid found")
        sys.exit(3)
    logger.info("Grid(s) found")
    grids_matrix = process_extract_digits(im_grids_final,
                                          model,
                                          display=display,
                                          display_digit=False,
                                          save_images_digit=save_images_digit)
    if all(elem is None for elem in grids_matrix):
        logger.error("Failed during digits extraction")
        sys.exit(3)
    logger.info("Extraction done")
    extract_time = time.time()
    grids_solved = main_solve_grids(grids_matrix)
    logger.info("Solving done")

    if grids_solved is None:
        print(grids_matrix)
        cv2.imshow('grid_extract', im_grids_final[0])
        cv2.imwrite(
            save_folder + os.path.splitext(os.path.basename(im_path))[0] +
            "_failed.jpg", im_grids_final[0])
        cv2.waitKey()
        sys.exit(3)

    solve_time = time.time()

    ims_filled_grid = write_solved_grids(im_grids_final, grids_matrix,
                                         grids_solved)
    im_final = recreate_img_filled(frame, ims_filled_grid, points_grids,
                                   list_transform_matrix)
    final_time = time.time()

    if save:
        if not os.path.isdir(save_folder):
            os.makedirs(save_folder)
        cv2.imwrite(
            save_folder + os.path.splitext(os.path.basename(im_path))[0] +
            "_solved.jpg", im_final)

    total_time = final_time - init

    load_time = init0 - init
    logger.info("Load Image\t\t\t{:03.1f}% - {:05.2f}ms".format(
        100 * load_time / total_time, 1000 * load_time))
    founding_time = found_grid_time - init0
    logger.info("Grid Research \t\t{:03.1f}% - {:05.2f}ms".format(
        100 * founding_time / total_time, 1000 * founding_time))
    extraction_duration = extract_time - found_grid_time
    logger.info("Digits Extraction \t{:03.1f}% - {:05.2f}ms".format(
        100 * extraction_duration / total_time, 1000 * extraction_duration))
    solving_duration = solve_time - extract_time
    logger.info("Grid Solving \t\t{:03.1f}% - {:05.2f}ms".format(
        100 * solving_duration / total_time, 1000 * solving_duration))
    recreation_duration = final_time - solve_time
    logger.info("Image recreation \t{:03.1f}% - {:05.2f}ms".format(
        100 * recreation_duration / total_time, 1000 * recreation_duration))
    logger.info("PROCESS DURATION \t{:.2f}s".format(final_time - init0))
    logger.info("EVERYTHING DONE \t{:.2f}s".format(total_time))
    # print(grid)
    # print(grid_solved)

    if len(ims_filled_grid) == 1:
        cv2.imshow('img', frame)
        cv2.imshow('grid_extract', im_grids_final[0])
        cv2.imshow('grid_filled', ims_filled_grid[0])
    cv2.imshow('im_final', im_final)
    cv2.waitKey()