How to detect Forest fires using Kinesis Video Streams and Amazon Rekognition
Tags: aws, kinesis, rekognition, globallogic
Published: 2024-06-05 22:01:55

#Introduction#
On a hot summer night, while we were enjoying our food and drinks, the dogs suddenly began barking and staring at a certain direction. We got outside to have a better look and noticed that the sky had started to turn orange. We immediately knew what it was happening, there was a huge fire at a beautiful forest a few miles away. This was happening almost every summer, at different places, wiping out forests and destroying homes, with a massive impact on the environment and people's lives. 

Having seen the aftermath and the years it took for the burnt areas and people to recover, I decided to build something to detect smoke and fire and help reduce the destructive impact. After all, early detection plays a crucial role when it comes to forest fires.

#Challenges#
Waiting for a Real-time scenario, like the one described above, was not an option or desirable for testing my solution. To overcome this challenge, i decided to simulate the required conditions. 

I used my laptop and played YouTube videos of forest fires as the source. This approach allowed me to consistently recreate the visual characteristics of forest fires, use specific scenes, thus ensuring that my solution was tested thoroughly under different conditions. This approach provided a reliable and efficient way to validate my solution and demonstrate how it could possibly handle similar real-time scenarios.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1xmombl8la6f0ozl3ca.jpg)



#Prerequisites#
Here is a brief overview of the AWS services and components used in the solution:

**RTSP Camera** 
An IP/CCTV camera 

**Raspberry Pi** 
This acts as a local gateway to connect the camera and manage the video stream up to Amazon Kinesis Video Streams. It is using certificates generated by AWS IoT Core to authenticate itself securely to AWS services.

**AWS IoT**
Set up an IoT Thing to represent my IP camera. This involved configuring the certificates and policies for secure communication between the IP camera and AWS IoT. It is an important component in creating a secure and manageable architecture for streaming video from an RTSP camera through a Raspberry Pi to Kinesis Video Streams. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvp7994v49wb4hyeanmd.png)

**Kinesis Video Stream KVS**
Kinesis Video Stream to ingest live video from the RTSP camera (with a matching name to the IoT Thing).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ubp1aukl4vbq49ks645s.png)

**Amazon Rekognition**
Trained a Rekognition Custom Labels model to detect smoke and fire in images. Training takes some time, depending on the size of the dataset. (The ARN is used in Lambda functions).
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7a4b7hk7obyzz6rcznsy.png)

**S3**
Created an S3 bucket to store the extracted images from the IP camera, with the appropriate bucket policies to allow read/write access from the AWS services used.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e28f30pnh0mhbv837qfj.png)

**Lambda**
Wrote a Lambda function to processes images stored in S3, detect smoke and fire using Rekognition, and trigger an SNS notification.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mq6v9zc0iilaoswgc6ye.png)

**SNS**
If smoke or fire is detected by the Rekognition Custom Labels model, the Lambda function triggers a notification using Amazon Simple Notification Service (SNS). SNS can then deliver the notification to subscribed endpoints, such as email, SMS, or mobile push notifications.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ihszhyt9rqe19q3biy9.png)

**IAM Roles**
Created the required  IAM roles and policies for Kinesis Video Streams, Rekognition, Lambda, IoT, S3, and SNS. As per best practices, least privilege principles were applied.

**Producer SDK - GStreamer plugin**
The GStreamer plugin for Kinesis Video Streams is a component that integrates GStreamer with Amazon Kinesis Video Streams.

#Solution Overview and walkthrough#

Here is a brief overview about how the solution works.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pmiibnj16jcy69qvjsac.png)
The first thing to do is to start the Amazon Rekognition Model that we trained.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/66mk8bqay8p0c3eb2gq1.png)

Next, we need to setup the RTSP camera and test the stream, using VLC. Then we move on and configure the GStreamer plugin in the Raspberry-Pi.

We have to transfer the certificates to the Raspberry Pi and place them in a specific directory.

Obtain the IoT credential endpoint using AWS CloudShell or awscli:
```
aws iot describe-endpoint --endpoint-type iot:CredentialProvider
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/72ekcyq9mcgetzdk9kmc.png)
The next step is to set the environment variables for the region, certificate paths, and role alias:
```
export AWS_DEFAULT_REGION=eu-west-1
export CERT_PATH=certs/certificate.pem.crt
export PRIVATE_KEY_PATH=certs/private.pem.key
export CA_CERT_PATH=certs/AmazonRootCA1.pem
export ROLE_ALIAS=CameraIoTRoleAlias
export IOT_GET_CREDENTIAL_ENDPOINT=cxxxxxxxxxxs.credentials.iot.eu-west-1.amazonaws.com
```

Now we can execute the GStreamer command and start streaming to Kinesis Video Streams:
```
./kvs_gstreamer_sample FireDetection rtsp://username:password@192.168.1.100/stream1
```
With the video feed successfully streaming to Kinesis Video Streams, it's time to start extracting the images from the stream.

Kinesis Video Streams simplifies this process by automatically transcoding and delivering images. It extracts images from video data in real-time based on tags and delivers them to a specified S3 bucket.
To use that feature, we need to create a JSON file named ***update-image-generation-input.json*** with the required config.

```
{
 "StreamName": "FireDetection",
 "ImageGenerationConfiguration":
 {
  "Status": "ENABLED",
  "DestinationConfig":
  {
   "DestinationRegion": "eu-west-1",
   "Uri": "s3://images-bucket-name"
  },
  "SamplingInterval": 200,
  "ImageSelectorType": "PRODUCER_TIMESTAMP",
  "Format": "JPEG",
  "FormatConfig": {
                "JPEGQuality": "80"
       },
  "WidthPixels": 1080,
  "HeightPixels": 720
 }
}
```

and run the following command in awscli
```
aws kinesisvideo update-image-generation-configuration \
--cli-input-json file://./update-image-generation-input.json \
```
If we check our S3 bucket we can see the extracted images
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1cp9fxg8umpx0j5vq3l7.png)

Our Lambda function is now going to be triggered and will start processing them using Amazon Rekognition. This allows for identifying smoke/fire objects within the images and triggering notifications based on detected objects.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/727rm0nd5ovf8wfm9xqt.png)

#Conclusion#
We now have a solution where our IP camera streams video to a Kinesis Video Stream. AWS Lambda processes frames from this stream, using Amazon Rekognition Custom Labels to detect smoke and fire. Detected events are then triggering SNS. 
By integrating Amazon Rekognition with custom labels, Kinesis Video Streams, S3, and AWS IoT, we can create a powerful image recognition system for many use cases.

For a more detailed walkthrough, feel free to contact me.