Tracking Raspberry Pi Internal Temperatures in Computer Vision Apps

by

Lila Mullany

Running computer vision applications (or AI applications in general) can put a strain on an edge device, especially when combining models. Using a heat sink in conjunction with a fan is probably sufficient to keep the device from throttling, but it would be helpful to be notified before your device becomes this hot. Being alerted to potentially unsafe temperature enables you to stop your app before the device burns out, and to continue to monitor the temperature — giving you the option to restart the program when the device has sufficiently cooled off.

To that end, we have created a utility class that enables you to track and record temperature readings, and which provides a summary of the collected data.

This tutorial uses a Raspberry Pi 4 and the package ‘gpiozero’, which comes installed by default on the Raspbian OS image. For more information on this package, see the following link.

To complete the tutorial, you must have:

  1. An alwaysAI account (it’s free!)
  2. alwaysAI set up on your machine (also free)
  3. A text editor such as sublime or an IDE such as PyCharm, or whatever else you prefer to code in

Please see the rest of the alwaysAI tutorials to learn more about computer visiondeveloping models, how to change models, etc.

All of the code from this tutorial is available on GitHub.

There will be two main parts to this tutorial:

  1. Adding in the GPIO Zero requirements
  2. Initializing the temperature tracker in the app.py file

Let’s get started!

After you have your free account and have set up your developer environment, you’ll need to download all of the starter apps; do so using this link before proceeding with the rest of the tutorial.

The app that was modified for this tutorial was the ‘Object Detector’ app, so cd into the starter apps folder and then into the ‘realtime_object_detector’ folder.

  1. Adding the GPIO Zero requirements:
  • First, add a requirements.txt file to your folder.
  • Add the content ‘gpiozero’ to requirements.txt. This will be installed when you deploy your app. See the alwaysAI documentation for more information on dependencies.

NOTE: if your Pi is not connected to wifi, it will not be able to download the requirements in requirements.txt

2. Updating the app.py file to use the TemperatureTracker:

  • Import the TemperatureTracker code by adding the following line to the very top of ‘app.py’:

from temperature_tracker import TemperatureTracker

  • After the print statements describing the model, engine, accelerator and labels, we’ll create the TemperatureTracker instance. Add the following line of code:

temperature_tracker = TemperatureTracker()

  • Now, we need to mark the time that we started recording temperature data. Inside the try/with block, but before the ‘while’ loop, add the following code right after you start the FPS instance:

temperature_tracker.start()

  • To actually capture temperature data, call the ‘update’ method. Place the following code inside the while loop:

temperature_tracker.update()

NOTE: I added a blank line for readability after the ‘for prediction in predictions’ loop and before the temperature statements. This can be done any time you want more readability in the streamer text output with ‘text.append(“”)’.

  • To get a current reading only, and not modify the stored data, you can use the ‘now’ method. This will return the temperature and corresponding timestamp. Do this by adding the following line right after the call to update that you added in the previous step:

now = temperature_tracker.now()

  • We can append the text sent to the streamer with the current temperature and also check whether the temperature that was returned was safe (at least for a Raspberry Pi 4). Add the following code under the changes you made in the previous two steps:

# log block showing current temperature
text.append(“{:1.2f}C/{:1.2f}F at time {}\n”.format(now[0], ((now[0]*(9 / 5)) + 32),time.strftime(‘%Y-%m-%d %H:%M:%S’, now[1])))# details whether the temperature is safe for a Raspberry Pi 4
if now[0] < temperature_tracker.MAX_TEMP_RASP4:
    text.append(“Temperature is safe”)
else:
    text.append(“TEMPERATURE IS NO LONGER SAFE”)

NOTE: If you do not see the warning message on the streamer before the application stops, this means that the browser feed did not refresh before the application stopped. You will still see this warning message on the console once the program stops.

  • After the call to fps.update(), add the following check:

if now[0] >= temperature_tracker.MAX_TEMP_RASP4:
    print(“Maximum safe temperature reached, stopping program”)
    break

  • Lastly, in the final block, mark the time you stopped the tracker after you stop the FPS instance by adding:

temperature_tracker.stop()

  • Capture and print the temperature summary data by adding the following lines just before the ‘Program Ending’ print statement:

summary = temperature_tracker.summary()
print(summary)

That’s it! Now you can build and start your app to see it in action. You may want to configure the app first, especially if you changed edge devices or created a new folder from scratch. You can do this with the following command, and enter the desired configuration input when prompted:

aai app configure

Now, to see your app in action, first build the app by typing into the command line:

aai app install

And once it is done building, start the app with the command:

aai app start

Open the browser to localhost:5000 to see the app in action, and watch the console after terminating the program for a summary of your temperature data, as shown below.

Tracking Raspberry Pi Internal Temperatures in Computer Vision Apps Image

Drive More Real-Time Intelligence and Higher ROI in Your Business