### Uninstall and reinstall boxdetect Source: https://github.com/karolzak/boxdetect/blob/master/notebooks/get-started-autoconfig.ipynb This code snippet first uninstalls the boxdetect library and then reinstalls it. This is often done to ensure a clean installation or to get the latest version. ```python import sys ! {sys.executable} -m pip uninstall boxdetect -y ``` -------------------------------- ### Configure pipelines Source: https://github.com/karolzak/boxdetect/blob/master/notebooks/get-started-pipelines.ipynb Sets up the configuration for the box detection pipelines, including width, height, scaling factors, and aspect ratio ranges. ```python from boxdetect import config cfg = config.PipelinesConfig() # important to adjust these values to match the size of boxes on your image cfg.width_range = (30, 50) cfg.height_range = (30, 35) # the more scaling factors the more accurate the results but also it takes more time to processing # too small scaling factor may cause false positives # too big scaling factor will take a lot of processing time cfg.scaling_factors = [1.0] # w/h ratio range for boxes/rectangles filtering cfg.wh_ratio_range = (0.5, 1.8) # range of groups sizes to be returned cfg.group_size_range = (1, 100) # for this image we will use rectangles as a kernel for morphological transformations cfg.morph_kernels_type = 'rectangles' # 'lines' # num of iterations when running dilation tranformation (to engance the image) cfg.dilation_iterations = 0 ``` -------------------------------- ### Get boxes using the pipeline Source: https://github.com/karolzak/boxdetect/blob/master/notebooks/get-started-pipelines.ipynb Uses the `get_boxes` pipeline to detect rectangular boxes and group them. ```python from boxdetect.pipelines import get_boxes rects, grouping_rects, image, output_image = get_boxes( file_path, cfg=cfg, plot=False) ``` -------------------------------- ### Load image and display Source: https://github.com/karolzak/boxdetect/blob/master/notebooks/get-started-pipelines.ipynb Loads an image using OpenCV and displays it using Matplotlib. ```python import cv2 import matplotlib.pyplot as plt %matplotlib inline file_path = '../images/form_example1.png' input_image = cv2.imread(file_path) plt.figure(figsize=(15, 15)) plt.axis('off') plt.imshow(input_image) plt.show() ``` -------------------------------- ### Initialize PipelinesConfig and autoconfigure from VoTT Source: https://github.com/karolzak/boxdetect/blob/master/notebooks/get-started-autoconfig.ipynb This code snippet initializes the PipelinesConfig object and configures it by reading annotations from a specified VoTT directory. It targets annotations with the class tag 'box'. ```python from boxdetect import config cfg = config.PipelinesConfig() cfg.autoconfigure_from_vott( vott_dir="../tests/data/autoconfig_simple", class_tags=["box"]) ``` -------------------------------- ### Load image and display it Source: https://github.com/karolzak/boxdetect/blob/master/notebooks/get-started-autoconfig.ipynb This code snippet loads an image using OpenCV and displays it using Matplotlib. It sets up the plot to be a specific size and turns off the axes for a cleaner display. ```python import cv2 import matplotlib.pyplot as plt %matplotlib inline file_path = '../tests/data/autoconfig_simple/dummy_example.png' input_image = cv2.imread(file_path) plt.figure(figsize=(15, 15)) plt.axis('off') plt.imshow(input_image) plt.show() ``` -------------------------------- ### Autoconfigure pipelines with specific box sizes Source: https://github.com/karolzak/boxdetect/blob/master/notebooks/get-started-autoconfig.ipynb This code snippet demonstrates how to use the `autoconfigure` method from `boxdetect.config` to set up the configuration for detecting checkboxes. It takes a list of tuples, where each tuple represents the height and width of the checkboxes to search for. ```python from boxdetect import config cfg = config.PipelinesConfig() # The values I'm providing below is a list of box sizes I'm interested in and want to focus on # [(h, w), (h, w), ...] cfg.autoconfigure([(46, 46), (44, 43)]) ``` -------------------------------- ### Get checkboxes using the configured pipeline Source: https://github.com/karolzak/boxdetect/blob/master/notebooks/get-started-autoconfig.ipynb This code snippet uses the `get_checkboxes` function from the `boxdetect.pipelines` module to detect checkboxes in a given file path, using the previously configured `cfg` object. It includes parameters for pixel threshold, plotting, and verbosity. ```python from boxdetect.pipelines import get_checkboxes checkboxes = get_checkboxes( file_path, cfg=cfg, px_threshold=0.1, plot=False, verbose=True) ``` -------------------------------- ### Install BoxDetect from GitHub Source: https://github.com/karolzak/boxdetect/blob/master/README.md Installs the BoxDetect package directly from its GitHub repository. ```bash pip install git+https://github.com/karolzak/boxdetect ``` -------------------------------- ### Process image and get checkboxes with adjusted config Source: https://github.com/karolzak/boxdetect/blob/master/notebooks/get-started-pipelines.ipynb This code snippet uses the `get_checkboxes` function with the previously adjusted configuration and a `px_threshold` to detect checkboxes and determine if they are checked. ```python from boxdetect.pipelines import get_checkboxes # px_threshold set to 0.1 is basically saying that every checkbox # with more than 10% of white pixels inside should be considered as checked checkboxes = get_checkboxes( file_path, cfg=cfg, px_threshold=0.1, plot=False, verbose=True) ``` -------------------------------- ### Get checkboxes using the get_checkboxes pipeline Source: https://github.com/karolzak/boxdetect/blob/master/notebooks/get-started-autoconfig.ipynb This code snippet uses the `get_checkboxes` pipeline from `boxdetect.pipelines` to detect checkboxes in an image based on the provided configuration (`cfg`). The `plot=False` argument indicates that the results should not be plotted directly. ```python from boxdetect.pipelines import get_checkboxes checkboxes = get_checkboxes(file_path, cfg=cfg, plot=False) ``` -------------------------------- ### Install BoxDetect from PyPI Source: https://github.com/karolzak/boxdetect/blob/master/README.md Installs the BoxDetect package from the Python Package Index (PyPI). ```bash pip install boxdetect ``` -------------------------------- ### Adjust configuration for a new image Source: https://github.com/karolzak/boxdetect/blob/master/notebooks/get-started-pipelines.ipynb This code demonstrates how to adjust the `PipelinesConfig` parameters such as `width_range`, `height_range`, `scaling_factors`, and `wh_ratio_range` to optimize checkbox detection for a specific image. ```python from boxdetect import config cfg = config.PipelinesConfig() # important to adjust these values to match the size of boxes on your image cfg.width_range = (40,60) cfg.height_range = (40,60) # the more scaling factors the more accurate the results but also it takes more time to processing # too small scaling factor may cause false positives # too big scaling factor will take a lot of processing time cfg.scaling_factors = [0.5] # w/h ratio range for boxes/rectangles filtering cfg.wh_ratio_range = (0.5, 1.5) # num of iterations when running dilation tranformation (to engance the image) cfg.dilation_iterations = 0 ``` -------------------------------- ### Display individual grouped boxes Source: https://github.com/karolzak/boxdetect/blob/master/notebooks/get-started-pipelines.ipynb Iterates through the grouped boxes and displays each cropped box individually. ```python import cv2 org_img = cv2.imread(file_path) for rect in grouping_rects: x1 = rect[0] x2 = x1 + rect[2] y1 = rect[1] y2 = y1 + rect[3] plt.figure(figsize=(10,1)) plt.imshow(org_img[y1:y2, x1:x2]) plt.show() ``` -------------------------------- ### Display image with detected boxes Source: https://github.com/karolzak/boxdetect/blob/master/notebooks/get-started-pipelines.ipynb Draws the detected boxes (green) and grouped boxes (red) on the image and displays it. ```python from boxdetect.img_proc import draw_rects, get_image import matplotlib.pyplot as plt %matplotlib inline out_img = draw_rects(get_image(file_path), rects, thickness=3) plt.figure(figsize=(15,20)) plt.imshow(out_img) plt.show() ``` -------------------------------- ### Display image with detected boxes Source: https://github.com/karolzak/boxdetect/blob/master/notebooks/get-started-autoconfig.ipynb This code snippet draws rectangles around the detected checkboxes on the original image and then displays the resulting image using Matplotlib. ```python from boxdetect.img_proc import draw_rects, get_image import matplotlib.pyplot as plt %matplotlib inline out_img = draw_rects(get_image(file_path), checkboxes[:,0], thickness=3) plt.figure(figsize=(15,20)) plt.imshow(out_img) plt.show() ``` -------------------------------- ### Load autoreload extension and set up path Source: https://github.com/karolzak/boxdetect/blob/master/notebooks/get-started-autoconfig.ipynb This code snippet loads the autoreload extension for Jupyter notebooks, which automatically reloads modules before executing code. It also appends the parent directory to sys.path to allow importing local modules. ```python %load_ext autoreload %autoreload 2 import sys sys.path.append("../.") ``` -------------------------------- ### Load and display an image for checkbox detection Source: https://github.com/karolzak/boxdetect/blob/master/notebooks/get-started-pipelines.ipynb This code snippet loads an image using OpenCV and displays it using Matplotlib, preparing for subsequent checkbox detection. ```python import cv2 import matplotlib.pyplot as plt %matplotlib inline file_path = '../tests/data/dummy_example2.png' input_image = cv2.imread(file_path) plt.figure(figsize=(7, 7)) plt.axis('off') plt.imshow(input_image) plt.show() ``` -------------------------------- ### Print checkbox details and display crops Source: https://github.com/karolzak/boxdetect/blob/master/notebooks/get-started-autoconfig.ipynb This code snippet iterates through the detected checkboxes, printing their bounding rectangle coordinates, the result of the `contains_pixels` check, and displaying a cropped image of each checkbox. ```python print("Output object type: ", type(checkboxes)) for checkbox in checkboxes: print("Checkbox bounding rectangle (x,y,width,height): ", checkbox[0]) print("Result of `contains_pixels` for the checkbox: ", checkbox[1]) print("Display the cropout of checkbox:") plt.figure(figsize=(1,1)) plt.imshow(checkbox[2]) plt.show() ``` -------------------------------- ### Detect checkboxes and recognize if marked Source: https://github.com/karolzak/boxdetect/blob/master/notebooks/get-started-pipelines.ipynb This code snippet shows how to use the `get_checkboxes` function to process an image and identify checkboxes, determining if they are checked based on pixel density. ```python from boxdetect.pipelines import get_checkboxes file_path = '../images/form_example1.png' checkboxes = get_checkboxes( file_path, cfg=cfg, px_threshold=0.1, plot=False, verbose=True) ``` -------------------------------- ### Inspect the results of checkbox detection Source: https://github.com/karolzak/boxdetect/blob/master/notebooks/get-started-pipelines.ipynb This code iterates through the detected checkboxes, printing their bounding box coordinates, whether they contain pixels (indicating they are checked), and displaying a cropout of each checkbox. ```python print("Output object type: ", type(checkboxes)) for checkbox in checkboxes: print("Checkbox bounding rectangle (x,y,width,height): ", checkbox[0]) print("Result of `contains_pixels` for the checkbox: ", checkbox[1]) print("Display the cropout of checkbox:") plt.figure(figsize=(1,1)) plt.imshow(checkbox[2]) plt.show() ``` -------------------------------- ### Using `boxdetect.pipelines.get_checkboxes` to retrieve and recognize just the checkboxes Source: https://github.com/karolzak/boxdetect/blob/master/README.md This code example shows how to use the `get_checkboxes` function to retrieve and recognize only checkboxes from an image. It assumes the config is already adjusted and demonstrates setting `px_threshold` and `verbose` parameters. ```python from boxdetect.pipelines import get_checkboxes checkboxes = get_checkboxes( file_path, cfg=cfg, px_threshold=0.1, plot=False, verbose=True) ``` ```python print("Output object type: ", type(checkboxes)) for checkbox in checkboxes: print("Checkbox bounding rectangle (x,y,width,height): ", checkbox[0]) print("Result of `contains_pixels` for the checkbox: ", checkbox[1]) print("Display the cropout of checkbox:") plt.figure(figsize=(1,1)) plt.imshow(checkbox[2]) plt.show() ``` -------------------------------- ### Saving and loading configuration to/from YAML Source: https://github.com/karolzak/boxdetect/blob/master/README.md Demonstrates how to save a `PipelinesConfig` object to a YAML file and load it back. ```python from boxdetect import config cfg = config.PipelinesConfig() cfg.morph_kernels_thickness = 10 cfg.save_yaml('test_cfg.yaml') cfg2.load_yaml('test_cfg.yaml') ``` -------------------------------- ### Autoconfiguring from VoTT annotations Source: https://github.com/karolzak/boxdetect/blob/master/README.md This snippet shows how to use `autoconfigure_from_vott` to set up configuration parameters based on ground truth annotations from VoTT. ```python from boxdetect import config cfg = config.PipelinesConfig() cfg.autoconfigure_from_vott( vott_dir="../tests/data/autoconfig_simple", class_tags=["box"]) ``` ```python from boxdetect.pipelines import get_checkboxes checkboxes = get_checkboxes(file_path, cfg=cfg, plot=False) ``` -------------------------------- ### Using `boxdetect.config.PipelinesConfig.autoconfigure` Source: https://github.com/karolzak/boxdetect/blob/master/README.md This snippet illustrates how to use the `autoconfigure` method of `PipelinesConfig` to automatically set configuration parameters based on a provided list of box sizes. ```python from boxdetect import config cfg = config.PipelinesConfig() # The values I'm providing below is a list of box sizes I'm interested in and want to focus on # [(h, w), (h, w), ...] cfg.autoconfigure([(46, 46), (44, 43)]) ``` ```python from boxdetect.pipelines import get_checkboxes checkboxes = get_checkboxes(file_path, cfg=cfg, plot=False) ``` -------------------------------- ### Detect character boxes and group them together Source: https://github.com/karolzak/boxdetect/blob/master/README.md This snippet shows how to configure and use the `get_boxes` pipeline to detect and group character boxes from an image. It includes setting up `PipelinesConfig` with parameters like `width_range`, `height_range`, `scaling_factors`, `wh_ratio_range`, `group_size_range`, and `dilation_iterations`. ```python from boxdetect import config file_name = 'form_example1.png' cfg = config.PipelinesConfig() # important to adjust these values to match the size of boxes on your image cfg.width_range = (30,55) cfg.height_range = (25,40) # the more scaling factors the more accurate the results but also it takes more time to processing # too small scaling factor may cause false positives # too big scaling factor will take a lot of processing time cfg.scaling_factors = [0.7] # w/h ratio range for boxes/rectangles filtering cfg.wh_ratio_range = (0.5, 1.7) # group_size_range starting from 2 will skip all the groups # with a single box detected inside (like checkboxes) cfg.group_size_range = (2, 100) # num of iterations when running dilation tranformation (to engance the image) cfg.dilation_iterations = 0 ``` ```python from boxdetect.pipelines import get_boxes rects, grouping_rects, image, output_image = get_boxes( file_name, cfg=cfg, plot=False) ``` ```python print(grouping_rects) OUT: # (x, y, w, h) [(276, 276, 1221, 33), (324, 466, 430, 33), (384, 884, 442, 33), (985, 952, 410, 32), (779, 1052, 156, 33), (253, 1256, 445, 33)] ``` ```python import matplotlib.pyplot as plt plt.figure(figsize=(20,20)) plt.imshow(output_image) plt.show() ``` -------------------------------- ### Highlighting just the checkboxes Source: https://github.com/karolzak/boxdetect/blob/master/README.md This snippet demonstrates how to modify the `PipelinesConfig` to specifically highlight only checkboxes by adjusting the `group_size_range` parameter. ```python # limit down the grouping algorithm to just singular boxes (e.g. checkboxes) cfg.group_size_range = (1, 1) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.