SafeZoned: IoT based monitoring System for child safety with Kafka and MindsDB

SafeZoned: IoT based monitoring System for child safety with Kafka and MindsDB

One step closer to worrying less about your child going out!

My first hashnode.com blog post!

Backstory

I had a childhood friend called Vignesh, He didn’t come to school one day and all of us thought he might be sick. Three days went by and the shocking news of his death spread across the school and our class teacher informed us about the same. We didn’t know how it happened or how but he was found dead in a well. I have a vivid memory of this moment to date and it will be there forever. Just imagine you’re in your 3rd grade and you hear one of your friend is dead. How do we solve this, the problem here is no one knew where he went or what he did that ended him in the well. Let’s look at how we can solve this.

Inspiration

I did some research and this is what I found. In the world of 8 Billion people every 40 seconds a child goes missing in the USA alone, an alarming number. Children are the backbone of one's nation, if the future of children was affected, it would impact the entire growth of that nation. Due to the abusements, the emotional and mental stability of the children gets affected which in turn ruins their careers and future. These innocent children are not responsible for what happens to them. So, parents are responsible for taking care of their own children. But, due to economic conditions and aims to focus on their child's future and career, parents are forced to crave money. Hence, it becomes difficult to cling on to their children all the time. In this approach, we provide an environment where this problem can be resolved in an efficient manner. It makes parents easily monitor their children in real-time just like staying beside them as well as focusing on their own careers without any manual intervention.
Source: FBI and the National Center for Missing & Exploited Children — FBI

Solution

SafeZoned is an application that is targeted to address this problem once it for all.

GitHub Repo: Link - Child Missing Kafka MindsDB
Demo Link: Link - Streamlit Web App on Azure

Architecture Diagram

Built with

  • Python – General programming language

  • Streamlit – Building the consumer dashboard with Python

  • Kafka – Queue for processing data from IoT devices

  • MySQL – Database to store initial data

  • MindsDB – Model training and inferencing

  • Docker - containerizing the Streamlit web app

  • Azure App Service - Deploying the Docker container

  • Twilio - Trigger an emergency call

  • MySQL - Store IoT data

What MindsDB offers

MindsDB brings machine learning into databases by employing the concept of AI Tables.

MindsDB might have just made all Data Scientist's life easier by bringing ML models to the database level and with cloud.mindsdb.com which is a user-friendly SQL interface to train ML models, connect various data sources, and execute SQL queries. I'll talk more about how to connect your existing MySQL database to MindsDB. I was worried about the speed of the prediction since it has to be real-time or near real-time but to my surprise, it's blisteringly fast less than ~600 ms per prediction. (includes network latency - tested on my local machine - 8thGen intel Core - i7-8750H, 16 GIG RAM, GTX 1060 6 GIG VRAM). It's an elegant solution to solve the pain of Data Engineers, where they've to serve AdHoc requests from Data Scientists. But with MindsDB, the data is already in the consumable format and it's available via a SQL interface so it's easy for a Data Scientist to build their models on top of it.

Deployment

MindsDB

Connecting to MySQL from MindsDB

Note: Whitelist all the three IPs in your MySQL instance
Important: Test Connection before continuing!

Once you click on Save and Continue, all the tables in your MySQL instance should be visible on MindsDB UI for you to use.

View Sample Data

SELECT 
    *
FROM 
    mysql_kafka.ml_iot_logs
LIMIT 
    10
;

Training

CREATE MODEL 
    mindsdb.child_missing_model
FROM 
    mysql_kafka
        (SELECT 
            *
        FROM 
            ml_iot_logs
        )
PREDICT 
    missing
;

Predict

SELECT 
    missing, missing_explain
FROM 
    mindsdb.child_missing_model
WHERE 
    latitude = 12.9699
    AND longitude=77.96288
    AND vibration=4.96288
    AND acceleration=77.96288;

# using sqlalchemy to connect to MindsDBs MySQL server     
user = os.environ.get("MINDSDB_USERNAME")
password = os.environ.get("MINDSDB_PASSWORD")
host = "cloud.mindsdb.com"
port = 3306
database = "mindsdb"
logging.info("Attempting to create MindsDB connection")

# connect to MindsDB database - faster than SDKs
conn = create_engine(
    url=f"mysql+pymysql://{user}:{password}@{host}:{port}/{database}").connect()

str_query = """ 
SELECT 
    missing, missing_explain
FROM 
    mindsdb.child_missing_model
WHERE 
    latitude = 12.9699
    AND longitude=77.96288
    AND vibration=4.96288
    AND acceleration=77.96288
"""

try:
    logging.info("Attempting to execute the query")
    missing, json_missing_explain = conn.execute(text(str_query)).fetchone()
    logging.info("Successfully executed the query")
except Exception as e:
    logging.error(f"Error in send_data_to_mysql - \n{e}")
finally:
    conn.close()

Kafka

Dashboard

It gives us an overall view of our producers, consumers, and topic metrics.

We've two topics, iot_logs and call_initiate

Sample Data from iot_logs topic

Sample Data from call_initiate topic

Streamlit - Docker

Dockerfile

FROM python:3.8.16-slim

RUN mkdir -p /opt/streamlit/child_missing_model

COPY . /opt/streamlit/child_missing_model

WORKDIR /opt/streamlit/child_missing_model

RUN pip install -r requirements.txt

EXPOSE 8501

CMD streamlit run child_missing_model.py

.dockerignore

# exclude all files
*

# include only required files
!child_missing_model.py
!constants.py
!handler.py
!utils.py
!requirements.txt
# build the streamlit image
docker build -t santhoshkdhana/child_missing_mindsdb_hack .
# run the strealit image build in the previous step
docker run -d -p 8501:8501 \
-e MINDSDB_USERNAME="<your_mindsdb_email>" \
-e MINDSDB_PASSWORD="<your_mindsdb_password>" \
-e MYSQL_USERNAME="<MySQL_username>" \
-e MYSQL_HOST="<MySQL_server_URL>" \
-e MYSQL_PASSWORD="<MySQL_server_password>" \
-e TWILIO_ACCOUNT_SID="<Twilio_account_sid>" \
-e TWILIO_AUTH_TOKEN="<Twilio_auth_token>" \
-e TWILIO_TWIML_BIN_URL="<Twilio_twiml_bin_url>" \
--name mindsdb_hack santhoshkdhana/child_missing_mindsdb_hack
# push the image to Docker Hub
docker login
docker push santhoshkdhana/child_missing_mindsdb_hack

UI - Streamlit Web APP

Click here to view the Streamlit web application deployed on Azure.

Docker Hub

Click here to view the image in the Docker hub!

docker pull santhoshkdhana/child_missing_mindsdb_hack

Twilio

Twilio TwiML Bin

Call Logs

Run locally

### Requires Python3.8+

- If you're on Windows, `pip install PyWin`
- Setup `client.properties` file inside kafka before running this commands - Used Confluent Kafka

### Env Variables
- `MINDSDB_USERNAME`
- `MINDSDB_PASSWORD`
- `MYSQL_USERNAME`
- `MYSQL_HOST`
- `TWILIO_ACCOUNT_SID`
- `TWILIO_AUTH_TOKEN`
- `TWILIO_TWIML_BIN_URL`

```bash
python -m venv mindsdb_venv

source mindsdb_venv/bin/activate

python -m pip install -r requirements.txt

#go inside kafka dir and run this

# will simulate five data points and pus to iot_logs Kafka topic
python producer.py -n 5 -t iot_logs

python consumer.py -t iot_logs

python call_consumer.py -t call_initiate
```

Conclusion

This method can help prevent such incidents and enable a safe world for every child out there. Technology is mature enough and not using it to solve critical issues can be bad for society. The cost of IoT devices is cheaper than ever and it’s just a one-time purchase.

Future of SafeZoned

  1. Test it with a few real-world users and gather feedback

  2. Deploy both consumers in the cloud for auto-scaling

  3. Build a custom dashboard map experience for parents to track their children

  4. Create a retraining pipeline for the model

Reference Links

Few of the docs I've used to develop this entire application in just three days, great work MindsDB on docs. Kudos to the team!

Introduction to Machine Learning - MindsDB

MindsDB and SQL Alchemy - MindsDB

MindsDB and Kafka - MindsDB

Feature Engineering in MindsDB - MindsDB

Tutorials by Our Community - MindsDB

Editor - MindsDB

MindsDB - GitHub

TwiML for Programmable Voice | Twilio

Join MindsDB Community on Slack | Slack