This blog post explains how to capture and analyse a picture with the help of a Raspberry Pi camera and Microsoft Computer Vision API and then display results based on contents of captured image on a Character LCD.
Hardware Requirements
- Raspberry Pi
- Camera for Raspberry Pi
- Character LCD
- Jumper wires
- Breadboard
- Potentiometer (If your LCD supports contrast adjustment)
- Button
Wiring
Depending upon LCD, you may have backlight.
Raspberry Pi | LCD |
---|---|
5V or 3.3V (Depends on LCD) | Backlight Anode (+ve) |
GND | Backlight Cathode (-ve) |
If your LCD supports changing of contrast, you can connect a potentiometer to adjust that.
Raspberry Pi | Potentiometer | LCD |
---|---|---|
5V or 3.3V (Depends on LCD) | +ve pin | |
GND | -ve pin | |
Data Pin | Contrast Pin (Usually 3rd pin on LCDs using Hitachi HD44780 LCD controller) |
Software Requirements
We are going to capture a photo when the button is pressed and then upload that image to Dropbox. We will then get a shareable link from Dropbox and make a Microsoft Computer Vision API call with that photo link. Results returned by API call will be displayed on Character LCD.
Installing the dependencies
Adafruit Python CharLCD library
Get the LCD library
here and cd
to the downloaded folder and run the following command to install it.
Requests
Run the following command in terminal to download and install requests.
Dropbox
Login to your dropbox account and create an app to store captured images. You will also need an Access Token. The following links may be of assistance. This blog post should help with that. Install dropbox python client library by typing the following command.
Getting an Computer Vision API (subscription) key to make API calls
You can get a free subscription key here. At the time of writing, Microsoft allows 5000 free transactions per month.
Python 3 Code
Importing the dependencies
Variables for making API call
Update the subscription_key variable with the key you got before.
Access Token for Dropbox
Raspberry Pi board
Button Setup
Variables for driving the LCD
Corresponding Raspberry Pi GPIO pins for register select, instruction and data registers on LCD. We will use these variables later to initialize the LCD.
Size of the LCD, If you have a character LCD of 16x2, then modify the lcd_columns variable to 16 and lcd_rows to 2. Since, I used 20x4 LCD, I will use the following values.
Initialise
Initialise Dropbox, LCD and camera by calling the constructor methods provided by Dropbox, Adafruit and picamera libraries. If your LCD supports backlight, then you could pass in an additional parameter of backlight = 1 or backlight = 0 to turn it on or off.
The main() function
The main() function is the first function that gets called when program starts. It waits for the button press and when that happens, it calls other functions to capture the image, upload it to dropbox, gets the uploaded image link and then finally calls the predict_contents() function which calls the Vision API and display results on LCD. The main() function is called in last step to perform same steps when user presses the button again.
Each function used here is described below in more detail.
The display_message() function
The display_function()
function takes advantage of the message() function provided by Adafruit LCD library.
You can pass in upto four strings while calling it and it will display each string on a new line.
However it will display the message on multiple lines if you only pass in line1 parameter.
lcd.clear()
will clear any existing text on LCD and lcd.home()
will bring the cursor to row 1 and column 1.
The upload_image() function
This piece of code gets the previously captured image in working directory named image.jpg
and uploads it to dropbox.
The get_image_url() function
This function makes a post request to dropbox to get a shareable link of image we uploaded at last step. The result is then converted to JSON.
image_url.replace('www', 'dl')
converts the https://www.rest-of-the-link to https://dl.rest-of-the-link so that it can be downloaded.
This step is necessary otherwise the image opens in the dropbox web app. The access token obtained from dropbox earlier is used here to authenticate the request.
The predict_contents(image_url) function
This function takes in the previously obtained image link as a parameter and post it to the computer vision API.
The subscription key is used to authenticate the request and visualFeatures
parameter contains details about what features do we want to analyse in the image.
Vision API applies machine learning to identify the image and returns the result.
Caption best summaries the image in a single sentence and tags contain information about what features are there in the image.
The top 8 most confident tags are displayed after caption on LCD.
Starting point of our application
This is where we call the main()
function and display "Program Stopped" when program is stopped.
Running the application
Finally, we are at a point to start the program and have some fun taking pictures. Download the source code and open up your command prompt or terminal and cd into the folder where you downloaded the file and run the following command.
Example pictures and API responses
Check on GithubThanks for reading.