Developer stories to your inbox.

Subscribe to the Developer Digest, a monthly dose of all things code.

You may unsubscribe at any time using the unsubscribe link in the digest email. See our privacy policy for more information.

alwaysAI Ad

Speed Up Development with a JSON Configuration File

By Jason Koo Jul 07, 2020

JSON Main application into a configuration imagen.

Separating certain variables from the main application into a configuration file can improve your development time by reducing the need to recompile apps for minor changes. In this tutorial, we’ll cover how to setup a very basic method for leveraging a JSON file for runtime configuration options.

This tutorial will have three parts:

  1. The configuration file
  2. Loading the configuration file in
  3. Initializing the variables

Creating the Configuration File

The first part of the tutorial is creating the configuration file, which we’ll call config.json.  We've gone with the JSON file format, which is a ubiquitous and human readable standard that's already being delivered by countless REST services. Create this file using the file browser, or if developing on a Mac, type the following into the Terminal app:

touch config.json

Or in Window's Powershell:

type nul > config.json

Open this file and declare any data your app will need to run. In our example we'll put in an entry for specifying a model name and what camera index to use, two common things changed when using our starter apps:

 "model" : "alwaysai/squeezenet_ssd",
 "var_2" : 0

Loading the Configuration File

To make use of the config.json file use the following convenience function:

import os
import json

def load_json(filepath):
   """Returns a JSON file as a Python object"""
    if os.path.exists(filepath) == False:
        raise Exception('File at {} does not exist'.format(filepath))
    with open(filepath) as data:
        return json.load(data)

I often put this into a separate file, something like, where I can include additional file access functions (like saving back to JSON) when needed, but you can place it directly in any .py file.

Using the Configuration

To make use of the config.json data :

import edgeiq

def main():
config = file_manager.load_json("./config.json")
model = config.get("model", None)
obj_detect = edgeiq.ObjectDetection(model)
camera_index = config.get("camera", None)
with edgeiq.WebcamVideoStream(cam=camera_index)

Now, if you want to change the model your app is referencing or to toggle between multiple cameras attached to your system, just edit the config.json instead of rebuilding* with aai app install.

*NOTE: This is just for changing which model to use, however, you'll still need to run 'aai app models add/remove' to actually update the CLI for downloading the models during an 'aai app install'. See our docs for more info. 

Get Started Now
We are providing professional developers with a simple and easy-to-use platform to build and deploy computer vision applications on edge devices.
Get Started Now
By Jason Koo Jul 07, 2020