Skip to content

Trains a model with Tensorflow / Keras to detect digits from my electricity meter. Images are coming from an ESP32 camera webserver. It predicts the digits, trains itself with the new predicted images and writes the information to an Influx database. Counter status is then visualized with Grafana. Alerts are also managed by Grafana.

License

Notifications You must be signed in to change notification settings

zw978/Tensorflow_Energy_Monitoring

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Energy monitoring with Tensorflow

Warning: Currently it's not Pylint conform and there are no tests available.

Motivation:

I am a computer science student and currently live in my own apartment. My server, several sensors / microcontrollers / smart home devices and especially my electric heating in an old building, consumes quite a lot of electricity.

Since only an analog electricity meter is installed, I would like to automate the reading process and visualize the data. In addition, notifications would be nice.

I decided to recognize the numbers via "artificial intelligence". Due to my low cost setup, the images from the camera are pretty bad. Existing models from the internet were not reliable enough. I decided to create my own model. The classification and training process had begun...

Setup:

ESP32 CAM

The ESP32 CAM microprocessor, was basically flashed with the template of the camera web server from Espressif examples. Only in the file app_httpd.cpp changes were made in the capture function to switch on the LED (pin 4) before each image.

    pinMode(4,OUTPUT); // Defines pin 4 as output pin
    digitalWrite(4,HIGH); // turns LED on
    digitalWrite(4,LOW); // turns LED off

The microprocessor is connected to the IoT WiFi and firewall rules to access the web server from the Ubuntu server are in place.

How it works

1. Preparation

Based on Tensorflow, Python3.7 and Anaconda virtual environment.

  1. Clone repo.
  2. Install Anaconda.
  3. Make sure requirments for Tensorflow are fulfilled. In my case I had to install CUDA etc. for my NVIDIA GPU.

NVIDIA GPU requirements

  1. Create new virtual enviroment with prepared conda_requirements.txt from setup folder. Currently the Anaconda requirements are only adjusted to Windows 10 + NVIDIA GPU. If you want to use a Linux distribution or train and predict with CPU you have to install additionally packages manually.
conda create --name myNewEnv --file [Path to /setup/conda_requirements.txt]
  1. Every time you want to use the program, first activate the Anaconda enviroment.
conda activate myNewEnv

Unfortunately I didn't find a package for InfluxDB in Anaconda, install package via pip inside the Anaconda env.

pip install influxdb

In config.ini file in the Prepare section add the path where the default folder structure should be created. Afterwards run Main.py and enter the path to config.ini.

Enter prepare to select preparation mode.

Enter the root folder name for the default folder structure.

Enter y to update paths in config.ini. They'll point to the recently created folders.

Now you have to enter the few missing parameters in config.ini manually.

This process is only necesarry if you want to create a new folder structure or if you want to update all paths at once in the config. To run predictions as a service for example as cronjob, its possible to start Main.py with parameters. Four modes are available: prepare, classify, train, predict

python Main.py [pathToConfig] [mode]

2. Classification and training

Run Main.py with desired parameter. Additional Python modules may need to be installed.

python Main.py [pathToConfig] classify

Now the raw image will be cropped. Afterwards the cropped image will be divided into multiple files. Each time the program shows the picture and asks which digit is displayed. After entering the answer, the image will be moved to the specifiy category / digit folder.

After some pictures have been classified, preferably a few hundred, the training can be started.

Run Main.py with desired parameter. Additional Python modules may need to be installed.

python Main.py [pathToConfig] train

Now the previously classified images are stored in a trained model using Tenserflow / Keras.

3. Recognition and visualization

Run Main.py with desired parameter. Additional Python modules may need to be installed.

python Main.py [pathToConfig] predict

The program gets a current image from the web server.

Raw image

Then the image is cut into several parts, just like it is when classifying.

Cropped image Cropped image Cropped image Cropped image Cropped image

The trained model tries to recognize the number in each image. If the probability is more than 95%, the recognized image is moved to a folder of the specific category, just as it is when classifying. The new pictures can be used in the future for the autonomously training of the model.

Prediction / log file

If the probability is more than 95% and the predicted value is higher or equal than the last electricity meter value, it's saved to an InfluxDB database. The predicted values and some additional information like the the maximum electricity meter value per day according to the contract are combined in a Grafana dashboard. The red points display the maximum electricity meter value per day according to the contract and the green graph shows the current value.

Grafana

Coming soon

  • Train model by itself with new detected images
  • Check electricity meter via Telegram bot
  • Grafana alerts

About

Trains a model with Tensorflow / Keras to detect digits from my electricity meter. Images are coming from an ESP32 camera webserver. It predicts the digits, trains itself with the new predicted images and writes the information to an Influx database. Counter status is then visualized with Grafana. Alerts are also managed by Grafana.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 52.9%
  • C++ 24.7%
  • Python 22.4%