Image Processors

Image processors are modules for determining information from images, e.g. roof shape, occupancy. All image processors have a method predict(ImageSet) which will return the information predicted for the ImageSet provided. The following example can be used to test the different processors that utilize street level images of buildings, e.g. NFloorDetector.

 1#written: fmk
 2#copyright: BSD-2
 3
 4"""
 5brails_street_processors.py
 6===========================
 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 using HouseView
14                  5) determine occupancy
15"""
16
17import os
18import argparse
19import ast
20
21from pathlib import Path
22
23from brails import Importer
24
25API_KEY_DIR = '../api_key.txt'
26if os.path.exists(API_KEY_DIR):
27    with open(API_KEY_DIR, 'r', encoding='utf-8') as file:
28        api_key = file.readline().strip()  # Read first line & strip whitespace
29else:
30    raise FileNotFoundError('API key file not found. Please ensure the file'
31                            f' exists at: {API_KEY_DIR}')
32
33def main():
34    
35    # Create the argument parser
36    parser = argparse.ArgumentParser(description="Demonstrate Importer.")
37    parser.add_argument('scraper', type=str, help="Footprint Scraper")
38    parser.add_argument('location', type=str, help="Location")
39    parser.add_argument('classifiers', type=str, help="List classifiers")        
40
41    # Parse the arguments
42    args = parser.parse_args()
43
44    importer = Importer()
45
46    region_boundary_class = importer.get_class("RegionBoundary")
47    region_boundary_object = region_boundary_class({"type": "locationName", "data": args.location})
48    scraper_class = importer.get_class(args.scraper)
49    scraper = scraper_class({"length": "ft"})
50    inventory = scraper.get_footprints(region_boundary_object)
51    print(f"num assets found: {len(inventory.inventory)} for {args.location} using {args.scraper}")    
52
53    # Subsample from the extracted assets to keep the image downloading step quick.
54    # Here, we are randomly sampling 10 buildings using a random seed value of 100:
55    small_inventory = inventory.get_random_sample(10, 100)
56
57    # Get street level imagery for the selected subset using GoogleStreetview:
58    google_street_class = importer.get_class('GoogleStreetview')
59    google_street = google_street_class({'apiKey': api_key})
60    images_street = google_street.get_images(small_inventory, 'tmp/street/')
61
62    images_street.print_info()
63
64    # Crop the obtained imagery such that they include just the buildings of
65    # interest:
66    filter_house = importer.get_class('HouseView')
67    image_filter = filter_house({})
68    filtered_images_street = image_filter.filter(images_street, 'tmp/filtered_images')
69    print('\nCropped images are available in ',
70          Path(filtered_images_street.dir_path).resolve())
71
72    filtered_images_street.print_info()    
73
74    classifiers = ast.literal_eval(args.classifiers)
75    #classifiers = args.classifiers
76
77    for classifier in classifiers:
78        print(f"**************** Running {classifier} *************")
79        classifier_class = importer.get_class(classifier)
80        classifier = classifier_class()
81        predictions = classifier.predict(filtered_images_street)
82        print(f"Predictions: {predictions}")
83
84# Run the main function if this script is executed directly
85if __name__ == "__main__":
86    main()        

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

python3 brails_street_processors.py USA_FootprintScraper "Larkspur, CA" NSFloorDetector

and the application would produce two predictions:

Processor Colab Link
NFloorDetector
NFloorsGPT
NFloorsVLM
VLM_Segmenter
ChimneyDetector
RoofShapeClassifier
RoofShapeClassifier
OccupancyClassifier
YearBuiltClassifier