> ## Documentation Index
> Fetch the complete documentation index at: https://docs.edgeimpulse.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Cyclist Blind Spot Detection - Himax WE-I Plus

Created By: Adam Taylor, Adam Fry

Public Project Link: [https://studio.edgeimpulse.com/public/108632/latest](https://studio.edgeimpulse.com/public/108632/latest)

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/intro.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=1935cc08a2f06538d3fb721d2d90a971" width="360" height="270" data-path=".assets/images/blind-spot-detection/intro.jpg" />
</Frame>

## Introduction

Like many countries, the UK encourages people to cycle, with many cycle paths and cycle-to-work programs. Ideally for safety, the cycle paths are isolated from the main flow of traffic, and my home town of Harlow does pretty well at this.

<Frame caption="Cycle Paths for my home town of Harlow, UK">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/harlow.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=60a1c7dc89c343b362d8eb29439ecb15" width="936" height="766" data-path=".assets/images/blind-spot-detection/harlow.jpg" />
</Frame>

However, Harlow is new town, and as such these paths could be easily planned and built. In the larger cities and older towns such as London, cyclists have to share the road with other users. Sadly each year this leads to fatalities and injuries on the roads, one case which is especially troublesome is when cyclists are on the inside of large vehicles such as trucks, vans, buses etc. When the cyclist is on the inside of the vehicle and the the vehicle is turning left (or right in the most other countries) there exists the conditions for the driver to not see the cyclist and turn into their path. Unfortunately, this leads to many injuries and deaths.

One of the major cause of these incidents is driver visibility. It is difficult for the driver to see down the side of the vehicle. There are attempts to prevent these events, one thing which is used in London on many large vehicles is a warning to Cyclists that they may be in the blind spot of the vehicle.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/blind-spot.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=abba6aaab8896981d80f1ed4357ba1c8" width="702" height="866" data-path=".assets/images/blind-spot-detection/blind-spot.jpg" />
</Frame>

Of course, it would be better for a system which would alert the driver that a cyclist was in their blind spot. This led to me thinking about how it could possibly be achieved, and retrofit into vehicles. Ideally the system would be low cost, small, capable of operating off a battery, and able to give a fast and timely warning to the driver. And, the system needs to be nearly self contained.

My idea was to use a little camera, which would be able to raise an indication or alert to the driver that there was a cyclist in the blind spot. Ideally, this would be audible such that the driver could not fail to see it. For this reason I chose the Himax WE-I Plus camera from SparkFun. This device includes a simple grey-scale VGA camera, and has the ability to break out GPIO such that a buzzer or other warning could be generated.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/himax-we-i-plus.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=b2d56bfee0cd9c426e6998f6d00017ba" width="936" height="740" data-path=".assets/images/blind-spot-detection/himax-we-i-plus.jpg" />
</Frame>

The USB connector can be used for powering the device, and it is small enough to be easily packaged and deployed.

The best way to be able to detect cyclists is to capture an image and analyse if a Cyclist is present. This is a perfect job for machine learning, specifically object detection. As we want to deploy at the edge on a small, power-constrained microprocessor, it is an ideal use case for Edge Impulse and their Faster Objects, More Objects (FOMO) algorithm.

## What is FOMO

Image classification, where we say if an item is present in a image, works well as long as there is only a single object in the image.

Alternatively, object detection is able to provide the class, number of objects and positions in the image. This is what we need for cyclist detection as real world conditions mean there will be many objects in the image and there may be several cyclists also in the same image. It is crucial when this occurs we do detect the cyclist, for this reason we need a object detection algorithm.

However, object detection algorithms are very computationally intensive and therefore struggle to be as responsive as necessary for this use case on a microcontroller.

This is where the FOMO algorithm developed by Edge Impulse comes into play, it provides a simplified version of object detection.

## Dataset

Like with all ML/AI projects, one of the largest challenges is in collecting a dataset. There is not a large, publicly available, dataset so we started to collect a small dataset to enable training and proof of concept. If the concept works we can create a larger dataset which addresses more conditions such as low light, weather, etc.

The initial dataset used consisted of 100 images of cyclists in different conditions tagged from around the world. These images were collected from open source images available across the web.

Once the dataset is collected we are able to create the Edge Impulse project.

## Edge Impulse Project

The first thing do is log into your [Edge Impulse account](https://www.edgeimpulse.com) and create a new project.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/dashboard-1.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=97b980eb34930b853f9442a917a2a08c" width="936" height="760" data-path=".assets/images/blind-spot-detection/dashboard-1.jpg" />
</Frame>

Once the project is created we need to get started working on it. I had a work experience student with me this week from the local high school. He helped me create the dataset and train the model, we were able to work collaboratively on the project due to Edge Impulse's new collaboration feature.

On the project Dashboard, select "Add collaborator" and in the dialog add in either the username or email of the individual.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/collaborator.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=427e01a1f82c318aee357fcae34d31aa" width="936" height="744" data-path=".assets/images/blind-spot-detection/collaborator.jpg" />
</Frame>

Once they are added you can then easily work together on the project. This enabled Adam F. to work on uploading and labelling the dataset, while I attended meetings.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/collaborators-2.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=9f7005311118b2b6ec1e2e546d164b60" width="642" height="492" data-path=".assets/images/blind-spot-detection/collaborators-2.jpg" />
</Frame>

Each of the images is uploaded and labeled with the location of the Cyclist.

As you upload the images you will notice the labeling queue in the data acquisition page displays the number of items to be labeled.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/data-aquisition.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=4c2133d424408e299974c65ca3617809" width="936" height="202" data-path=".assets/images/blind-spot-detection/data-aquisition.jpg" />
</Frame>

By clicking on the labelling queue you can label each image.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/RkSeGIwVMUIdPe7i/.assets/images/blind-spot-detection/labeling.jpg?fit=max&auto=format&n=RkSeGIwVMUIdPe7i&q=85&s=e015f98dd92b0e206c8a84042c74c317" width="936" height="630" data-path=".assets/images/blind-spot-detection/labeling.jpg" />
</Frame>

Once the bounding box is drawn around the object, the next step is to enter the label.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/labeling-2.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=00e310774f3a6649fe5424cdf5f93973" width="936" height="710" data-path=".assets/images/blind-spot-detection/labeling-2.jpg" />
</Frame>

Using this view we can work through each of the images which needs to be labeled.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/RkSeGIwVMUIdPe7i/.assets/images/blind-spot-detection/labeling-3.jpg?fit=max&auto=format&n=RkSeGIwVMUIdPe7i&q=85&s=dae044098fee7d61103e60ad22bfacea" width="936" height="680" data-path=".assets/images/blind-spot-detection/labeling-3.jpg" />
</Frame>

The next step with the data labeled is to create the impulse.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/RkSeGIwVMUIdPe7i/.assets/images/blind-spot-detection/processing.jpg?fit=max&auto=format&n=RkSeGIwVMUIdPe7i&q=85&s=58a63407b9b7aae508b2a56294605c8c" width="936" height="588" data-path=".assets/images/blind-spot-detection/processing.jpg" />
</Frame>

The first step is to add a processing block - Select "Image Processing" block.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/RkSeGIwVMUIdPe7i/.assets/images/blind-spot-detection/processing-2.jpg?fit=max&auto=format&n=RkSeGIwVMUIdPe7i&q=85&s=1ebf249ebc3c4cbd3939cc2a11910577" width="936" height="524" data-path=".assets/images/blind-spot-detection/processing-2.jpg" />
</Frame>

Then we can add the processing block.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/RkSeGIwVMUIdPe7i/.assets/images/blind-spot-detection/processing-3.jpg?fit=max&auto=format&n=RkSeGIwVMUIdPe7i&q=85&s=2bda923078797beebf8e7c4c13ab26f9" width="936" height="500" data-path=".assets/images/blind-spot-detection/processing-3.jpg" />
</Frame>

With the impulse created the next stage is to configure the image processing block. Change the color depth to Black and White.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/depth.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=678fce52e2f3afdd1b5d9fde43ed9d60" width="936" height="866" data-path=".assets/images/blind-spot-detection/depth.jpg" />
</Frame>

Select the "Generate Features" tab and click "Generate features".

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/generate-features-2.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=b468874aab642f6d85fbb8966e945213" width="936" height="878" data-path=".assets/images/blind-spot-detection/generate-features-2.jpg" />
</Frame>

These are the features which will be taken forward for training in the processing block.

The final stage is to train the model.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/RkSeGIwVMUIdPe7i/.assets/images/blind-spot-detection/training.jpg?fit=max&auto=format&n=RkSeGIwVMUIdPe7i&q=85&s=fc952668d84156bd23d9fbcfcc62af48" width="548" height="1000" data-path=".assets/images/blind-spot-detection/training.jpg" />
</Frame>

Once the model is trained we will see a confusion matrix which shows the performance. The initial training was good but the F1 score (which is the a key indication of the result) was too low.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/f1.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=ef59cb7a8bf9d44120ccdc58539d0b84" width="918" height="888" data-path=".assets/images/blind-spot-detection/f1.jpg" />
</Frame>

While it looks good on individual images like below.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/RkSeGIwVMUIdPe7i/.assets/images/blind-spot-detection/test.jpg?fit=max&auto=format&n=RkSeGIwVMUIdPe7i&q=85&s=859e5e574781d070e0b72ea37e14b64b" width="936" height="506" data-path=".assets/images/blind-spot-detection/test.jpg" />
</Frame>

When we test it on the entire validation set the accuracy score was very low, only 36%, which is not acceptable.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/RkSeGIwVMUIdPe7i/.assets/images/blind-spot-detection/test-2.jpg?fit=max&auto=format&n=RkSeGIwVMUIdPe7i&q=85&s=b7dcd6f514e2fdbe58f8bee1c33922cd" width="936" height="862" data-path=".assets/images/blind-spot-detection/test-2.jpg" />
</Frame>

We can get better performance than this. However, before we change the settings of the project, we will save a version of it. This will allow us to save the current state of the project, so we have a version we can revert to if necessary.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/RkSeGIwVMUIdPe7i/.assets/images/blind-spot-detection/save-version.jpg?fit=max&auto=format&n=RkSeGIwVMUIdPe7i&q=85&s=1217b60b0fcd187b14d04cafe983fac1" width="936" height="656" data-path=".assets/images/blind-spot-detection/save-version.jpg" />
</Frame>

With the version saved, the next step is to change some of the project settings. Investigating the project settings, the generated features are not closely clustered.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/feature-explorer.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=9df2074d16f657f9a73da5febfac7b0f" width="912" height="990" data-path=".assets/images/blind-spot-detection/feature-explorer.jpg" />
</Frame>

We can change the setting on the resize, to resize with respect to the longest side.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/RkSeGIwVMUIdPe7i/.assets/images/blind-spot-detection/resize.jpg?fit=max&auto=format&n=RkSeGIwVMUIdPe7i&q=85&s=3b9ba294e616543bfac50fcac440464d" width="526" height="926" data-path=".assets/images/blind-spot-detection/resize.jpg" />
</Frame>

Regenerating the features shows a much closer clustering in the Feature Explorer.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/feature-explorer-new.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=6deac2b7f421904fa2eb1d06a94f2cef" width="852" height="988" data-path=".assets/images/blind-spot-detection/feature-explorer-new.jpg" />
</Frame>

I also changed the number of training cycles, and the learning rate. This resulted in a better F1 score, though a slightly reduced accuracy compared to previously.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/RkSeGIwVMUIdPe7i/.assets/images/blind-spot-detection/revised.jpg?fit=max&auto=format&n=RkSeGIwVMUIdPe7i&q=85&s=5fde698e511c377e51404e0b104721bb" width="936" height="886" data-path=".assets/images/blind-spot-detection/revised.jpg" />
</Frame>

This resulted in much better performance when tested against the validation set.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/RkSeGIwVMUIdPe7i/.assets/images/blind-spot-detection/revised-2.jpg?fit=max&auto=format&n=RkSeGIwVMUIdPe7i&q=85&s=f88dc65778c2b3b0e588bc32b3580086" width="936" height="874" data-path=".assets/images/blind-spot-detection/revised-2.jpg" />
</Frame>

To deploy the algorithm on the target hardware we select "Deploy" and choose the Himax WE-i Plus camera from the options.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/RkSeGIwVMUIdPe7i/.assets/images/blind-spot-detection/target.jpg?fit=max&auto=format&n=RkSeGIwVMUIdPe7i&q=85&s=c3e620f6b348168227dab4442deca3b4" width="936" height="794" data-path=".assets/images/blind-spot-detection/target.jpg" />
</Frame>

This will generate an application directly for the target device.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/firmware.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=7894d7641b5db553e2c5a76145567f09" width="936" height="818" data-path=".assets/images/blind-spot-detection/firmware.jpg" />
</Frame>

To load the application onto the Himax WE-I Plus, extract the downloaded folder and run the batch file for your operating system, then follow the on-screen commands.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/flash-1.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=7910c9dc39cd76698942c26582774f8a" width="936" height="490" data-path=".assets/images/blind-spot-detection/flash-1.jpg" />
</Frame>

Press the Reset button on the device when instructed.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/flash-2.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=4c237d3526e7e709e9f56f5d42409f41" width="936" height="488" data-path=".assets/images/blind-spot-detection/flash-2.jpg" />
</Frame>

With the application flashed, we are able to run some tests using the camera against images, using live Classification.

Both images were correctly identified as a cyclists.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/classification-1.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=7b3db0568515d0633aebc3e30e11ae40" width="936" height="634" data-path=".assets/images/blind-spot-detection/classification-1.jpg" />
</Frame>

Second classification:

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/classification-2.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=aef953de304999cc897aebac643a3002" width="862" height="1000" data-path=".assets/images/blind-spot-detection/classification-2.jpg" />
</Frame>

The final step is run the application on the board, standalone. Testing this against a range of images shows cyclists detected.

<Frame caption="">
  <img src="https://mintcdn.com/edgeimpulse/cU2n98Ml68g1eiRr/.assets/images/blind-spot-detection/inferencing.jpg?fit=max&auto=format&n=cU2n98Ml68g1eiRr&q=85&s=fe3a58d5b5ee42f079749b1041126c50" width="936" height="490" data-path=".assets/images/blind-spot-detection/inferencing.jpg" />
</Frame>

## Wrap Up

This project shows that tinyML can be used in a small microcontroller-based image processing system to detect cyclists. This approach can be further developed to produce a system which can be used to increase road safety.
