Image Filters

Image filters are modules for cleaning up images prior to processing them for information by a processor. Removing unwanted crap from an image can help the processors work better. The following example demonstrates this. It uses the users chosen footprin scraper to obtain start an assetInventory. It then obtains Google Streev view images for a subset of the inventory and then applies a House flter, which attempts to isolate the house in the center of the downloadded image. It returns a new image.

The following example shows a more typical python example.

 1#written: fmk
 2#copyright: BSD-2
 3
 4
 5"""
 6brails_filters.py
 7================
 8Example showing the use of BRAILS module that crops building images from panos.
 9
10 Purpose: Testing 1) from Importer get a scraper using arg provided
11                  2) get_footprints using scraper provided
12                  3) get_images using StreetView for subset of footprints
13                  4) filter method of HouseView
14"""
15
16import os
17import argparse
18from pathlib import Path
19from brails import Importer
20
21# This script needs a Google API Key to run.
22# We suggest placing your API key in file apiKey.txt in the same directory as
23# this script if you plan to commit changes to this example. This way, you do
24# not risk accidentally uploading your API key (apiKey.txt is in .gitignore,
25# so you have work to do to get it uploaded)
26
27API_KEY_DIR = '../api_key.txt'
28if os.path.exists(API_KEY_DIR):
29    with open(API_KEY_DIR, 'r', encoding='utf-8') as file:
30        api_key = file.readline().strip()  # Read first line & strip whitespace
31else:
32    raise FileNotFoundError('API key file not found. Please ensure the file'
33                            f' exists at: {API_KEY_DIR}')
34
35
36def main():
37    
38    # Create the argument parser
39    parser = argparse.ArgumentParser(description="Demonstrate Importer.")
40    parser.add_argument('scraper', type=str, help="Footprint Scraper")
41    parser.add_argument('location', type=str, help="Location")    
42
43    # Parse the arguments
44    args = parser.parse_args()
45
46    importer = Importer()
47
48    region_boundary_class = importer.get_class("RegionBoundary")
49    region_boundary_object = region_boundary_class({"type": "locationName", "data": args.location})
50    scraper_class = importer.get_class(args.scraper)
51    scraper = scraper_class({"length": "ft"})
52    inventory = scraper.get_footprints(region_boundary_object)
53    print(f"num assets found: {len(inventory.inventory)} for {args.location} using {args.scraper}")    
54
55    # Subsample from the extracted assets to keep the image downloading step quick.
56    # Here, we are randomly sampling 10 buildings using a random seed value of 100:
57    small_inventory = inventory.get_random_sample(10, 100)
58
59
60    # Get street level imagery for the selected subset using GoogleStreetview:
61    google_street_class = importer.get_class('GoogleStreetview')
62    google_street = google_street_class({'apiKey': api_key})
63    images_street = google_street.get_images(small_inventory, 'tmp/street/')
64
65    images_street.print_info()
66
67    # Crop the obtained imagery such that they include just the buildings of
68    # interest:
69    filter_house = importer.get_class('HouseView')
70    image_filter = filter_house({})
71    filtered_images_street = image_filter.filter(images_street, 'tmp/filtered_images')
72    print('\nCropped images are available in ',
73          Path(filtered_images_street.dir_path).resolve())
74
75    filtered_images_street.print_info()    
76
77# Run the main function if this script is executed directly
78if __name__ == "__main__":
79    main()    

To run for example the brails_filters.py script for Berkeley, CA the following would be issued frm a terminal window:

python3 brails_filter.py USA_FootprintScraper "Berkeley, CA"

and the application would produce two ImageSets:

Raw Images

../../_images/gstrt_37.84910645_-122.27443686.jpg ../../_images/gstrt_37.85885402_-122.25080419.jpg ../../_images/gstrt_37.86096388_-122.27096762.jpg
../../_images/gstrt_37.86240981_-122.29162218.jpg ../../_images/gstrt_37.86325404_-122.27462417.jpg ../../_images/gstrt_37.86369654_-122.26024302.jpg
../../_images/gstrt_37.87800845_-122.27387780.jpg ../../_images/gstrt_37.87979406_-122.27365989.jpg ../../_images/gstrt_37.88182298_-122.26462540.jpg
Filtered Images:
    • ../../_images/gstrt_37.84910645_-122.274436861.jpg
    • ../../_images/gstrt_37.85885402_-122.250804191.jpg
    • ../../_images/gstrt_37.86096388_-122.270967621.jpg
    • ../../_images/gstrt_37.86240981_-122.291622181.jpg
    • ../../_images/gstrt_37.86325404_-122.274624171.jpg
    • ../../_images/gstrt_37.86369654_-122.260243021.jpg
    • ../../_images/gstrt_37.87800845_-122.273877801.jpg
    • ../../_images/gstrt_37.87979406_-122.273659891.jpg
    • ../../_images/gstrt_37.88182298_-122.264625401.jpg