FalconF16
Posts: 16
Joined: Thu May 28, 2020 1:42 am

Cost-effective device for Image Recognition?

Thu May 28, 2020 1:58 am

I'm brand new to Raspberry Pi (and microcomputers in general) and I'm working on a project that will require me to use a computer such as those produced by Raspberry Pi. I've been reading around this forum as well as other sites, but I can't seem to find a definitive answer that a super beginner like me can understand, which is what brings me here! I hope I'm asking in the right place!

Anyway, my goal is to use a Raspberry Pi computer to take a photo, and then analyze that photo to detect if there is a car in the photo. So I should be able to attach a camera module to it.

Now, I know - this is a common question and there are a lot of resources explaining how I can do this already. But my confusion lies here: I'm trying to find the least expensive way to do this. What is the cheapest device that will be able to accomplish this goal?

I've read some things about Raspberry Pi Zero for this, but I can't seem to find definitive answers about if it can do what I need, or not. Some websites show it being used alongside one of the larger computers, and other sites show it being used alone.

I appreciate any help! Thanks!

P.S: I've already created a model using Tensorflow (it's about 200MB). I know Raspberry computers run Tensorflow Lite, so I will make sure to use a model compatible with it!

blimpyway
Posts: 584
Joined: Mon Mar 19, 2018 1:18 pm

Re: Cost-effective device for Image Recognition?

Thu May 28, 2020 8:14 am

Can't have a generic answer, depends on what's camera looking at.

If it watches a parking lot, simple movement detection and blob size estimation would work on a pi zero too, since in a parking lot you can't have many things which are as big as a car but are not cars.

If you have wider images on a moving video footage with varying distances and perspective, then you probably need proper image recognition. How well that works depends again on... context. What is the camera looking at and how quick/often you need an answer. 10 fps? one frame per minute? Differences of "contex" can make a 3 order magnitude difference in processing power.

FalconF16
Posts: 16
Joined: Thu May 28, 2020 1:42 am

Re: Cost-effective device for Image Recognition?

Thu May 28, 2020 3:16 pm

@blimpyway Thanks for your response!

I think what I want to do is actually simpler than that.

I will attach the camera/computer to my garage, and I just want the computer to take a single photo every 10 minutes, and then process that photo.

There are multiple different cars. When it detects and classifies one certain car, it will just light up an LED. That's all!

Although movement detection and then just checking colors could be an easy solution, my problem is that, for example, I'm trying to detect a silver sedan, but there is also a silver SUV and a silver truck. So I can't just detect a blob and check its color.

Does that help provide more context? Please let me know if I should elaborate. Thanks!

blimpyway
Posts: 584
Joined: Mon Mar 19, 2018 1:18 pm

Re: Cost-effective device for Image Recognition?

Fri May 29, 2020 8:27 am

Well, 10 minutes is an awful long time.

Can you post a sample image? That or share details like:
- are there many cars in the frame?
- is only a fixed frame spot/area and distance from camera which needs to be focused on?
- does plate number usually shows in the image?

A sample image would be much more clear. Maybe erase plate numbers if they-re visible

blimpyway
Posts: 584
Joined: Mon Mar 19, 2018 1:18 pm

Re: Cost-effective device for Image Recognition?

Fri May 29, 2020 9:11 am

Just being curious I installed OpenALPR (license plate recognition stuff) on pi zero w

It needed up to 20 seconds to run on a couple demo images. That is cool, enough to check who stays on a parking spot.
If that is ok check this tutorial . Obviously, the car's plate has to be obvious on the picture.
Installing on pi zero w takes quite long. Skip PIP python libraries / script from the tutorial, if you don't have numpy already installed it takes forever to compile.

It is sufficient to get the openalpr libraries

Code: Select all

sudo apt install openalpr openalpr-daemon openalpr-utils libopenalpr-dev
and use the command line alpr program

https://magpi.raspberrypi.org/articles/ ... spberry-pi

FalconF16
Posts: 16
Joined: Thu May 28, 2020 1:42 am

Re: Cost-effective device for Image Recognition?

Sat May 30, 2020 12:43 am

blimpyway wrote:
Fri May 29, 2020 8:27 am
Well, 10 minutes is an awful long time.

Can you post a sample image? That or share details like:
- are there many cars in the frame?
- is only a fixed frame spot/area and distance from camera which needs to be focused on?
- does plate number usually shows in the image?

A sample image would be much more clear. Maybe erase plate numbers if they-re visible
Thanks again for your help!

I'm actually planning to use this for more than one thing - the garage example is only one of them. Rather, let me give you a better example that will make more sense. It's another thing I'm planning to work on, although the process will be the same.

Let's say I have a birdfeeder, and I want to be notified anytime a bird comes in for a meal. However, squirrels like to come to the birdfeeder uninvited, and I don't want to be notified when they do.

So, I'd create a tensorflow model to be able to detect a bird. It'll be a simple model; I don't need to classify different types of birds or anything. And in this case, the Pi Zero should take photos every 1 minute, analyze that image onboard, and flash an LED if there's a bird. If there's a squirrel or other animal, it shouldn't do anything (since the model doesn't detect squirrels).

So I guess what I'm trying to really ask is: "Can the Pi Zero run basic image detection using Tensorflow?"

There may be other ways to detect birds and such (without relying Tensorflow), just like the example you provided for reading license plates (which I assume doesn't utilize Tensorflow). However, my main concern is with running very basic Tensorflow models on the Pi Zero. I don't plan to use it for resource-intensive stuff like real-time vision. I just want to be able to process/analyze a single image every minute (or 10).

I hope that clears it up. Unfortunately, I can't try for myself because I don't have a Raspberry Pi. I'm hoping to use input from this forum before purchasing. But I plan to purchase many (for other similar uses), so cost is the most limiting factor (I don't want to go above $10 per Pi), and performance isn't my highest priority. Thanks!

Here's a random picture of a birdfeedr I found on the web. The Pi's camera will probably see a birds this far away (although the photo won't be as clear since I'll probably use a 5MP camera).
https://i1.wp.com/www.gardenexperiments ... .jpg?ssl=1

blimpyway
Posts: 584
Joined: Mon Mar 19, 2018 1:18 pm

Re: Cost-effective device for Image Recognition?

Sat May 30, 2020 1:10 pm

Hmm I doubt pi zero would do that.
I tried myself to compile darknet and run yolo tiny... didn't work. Compilation doesn't fail but program execution does. From what I could search, it has something with darknet making 64bit assumptions while Pi's os is 32

You are on your own.
-----------
Regarding squirrel vs birds - that's easy with motion detection - just "mask" region of the feeder itself and the pole it is standing on (or rope it is hanging from) so motion will trigger when birds come in flight, not when anything... crawls towards the feeder.

Most motion detectors have this feature to mask out a region (or more) from the viewing frame you don't want to trigger a motion event.
------------

Other option is to use a pi zero only to capture picture and send it to aws/cloud to compute recognition there. There are free tiers on aws and other hosting platforms.

FalconF16
Posts: 16
Joined: Thu May 28, 2020 1:42 am

Re: Cost-effective device for Image Recognition?

Sat May 30, 2020 2:46 pm

@blimpyway

Hmm, thanks for trying it out!

I think Tensorflow Lite is supported by 32 bit systems, according to:
https://www.tensorflow.org/lite/microcontrollers

Therefore, I think it may still work since it sounded like you didn't run into any other problems, except that Darknet wasn't supported by 32 bit systems. However, I'm in no way an expert (in either Tensorflow, Darknet, or RPi), so I'd love to hear what you think about this.

Also, I don't want to bother you, but if you are able to try testing Tensorflow on RPi Zero, I hope you can let me know what happens!

blimpyway
Posts: 584
Joined: Mon Mar 19, 2018 1:18 pm

Re: Cost-effective device for Image Recognition?

Sun May 31, 2020 7:12 am

That was a test I tried a while ago.

This feedback is enough to convince me not to install tensorflow on pi zero: https://pablotron.org/2019/04/06/tensor ... -idea.html

Pi zero w is cheap and fun, just spend the money and run your own test. Even if it doesn't fit this particular project, the learning adventure and perspective for other projects changes.

What I can say even it looks very slow compared with other Pis, the camera GPU is almost as fast as on a pi3. Image capture goes real time, with less than a couple cpu % wasted.
What you do with the image is another story but that also depends what is the particular problem and how much you can optimise it.
Motion detection in particular is quite good.

blimpyway
Posts: 584
Joined: Mon Mar 19, 2018 1:18 pm

Re: Cost-effective device for Image Recognition?

Sun May 31, 2020 7:50 am

See also this https://www.youtube.com/watch?v=aimSGOAUI8Y

So it should work provided models could be loaded in Zero's low memory

Even if it is 50 times slower than Pi 4 it should recognize a squirrel from a bird in under 20 seconds.

That is just theory. When camera is active, pi zero in headless mode has ~260 MBytes available memory.

blimpyway
Posts: 584
Joined: Mon Mar 19, 2018 1:18 pm

Re: Cost-effective device for Image Recognition?

Sun May 31, 2020 9:54 am

Well... the curiosity killed the cat. What a way to spend a Sunday morning.

After some pain I could manage to run tf lite on pi zero w, roughly as explained in the video above and its github pages.

The detection on image attached took 55 seconds, but after I measured times on various parts of the script, most of them was spend on start up - loading libraries including tensorflow and model.

The actual time spent in image detection was ~15-16 seconds.
cucusmall.jpg
cucusmall.jpg (94.28 KiB) Viewed 860 times

Heater
Posts: 17355
Joined: Tue Jul 17, 2012 3:02 pm

Re: Cost-effective device for Image Recognition?

Sun May 31, 2020 11:19 am

blimpyway wrote:
Sun May 31, 2020 7:12 am
That was a test I tried a while ago.

This feedback is enough to convince me not to install tensorflow on pi zero: https://pablotron.org/2019/04/06/tensor ... -idea.html
That example seem to be a very poor demonstration with timing done badly.

Experience shows that when starting up such a program a lot of time is spent just loading the libraries. That demo includes all that time.

Better to have the program startup and load what it needs. which might take a while, then loop around analyzing images.
Memory in C++ is a leaky abstraction .

Heater
Posts: 17355
Joined: Tue Jul 17, 2012 3:02 pm

Re: Cost-effective device for Image Recognition?

Sun May 31, 2020 11:22 am

blimpyway wrote:
Sun May 31, 2020 9:54 am
Well... the curiosity killed the cat. What a way to spend a Sunday morning.
Could you fill in some details for us?

Which link above did you follow?
Which code exactly?
Did you have a Coral accelerator attached?

I notice one of the examples linked to above uses a Pi 4 and a Coral accelerator, at which point it would be more economical and simpler to use a Nvidia Jetson Nano board.
Memory in C++ is a leaky abstraction .

blimpyway
Posts: 584
Joined: Mon Mar 19, 2018 1:18 pm

Re: Cost-effective device for Image Recognition?

Sun May 31, 2020 11:46 am

I mean the guy with this video has github links in description https://www.youtube.com/watch?v=aimSGOAUI8Y


However the example doesn't work on pi zero since the existing compiled tf_lite libraries are for Pi3

For pi zero I installed regular tensorflow with PIP in a virtual env using python 3.7... I used this "Zero failure" article to actually install tensorflow https://pablotron.org/2019/04/06/tensor ... -idea.html


After opencv failed I had to install some other library, I don't recall which one since the terminal history window now is busy for a while. It could be libglib2.0-dev but I'm not sure. (NOPE it was libgstreamer1.0-0)

Then I had to edit the script TFLite_detection_image.py since I ran in headless mode.. commented out resulting cv2 image display by saving it in a file instead. The script itself already detected whether is tf_lite library or full tensorflow available and does its imports accordingly. This was great

The github link is: https://github.com/EdjeElectronics/Tens ... spberry-Pi

PS install time of tensorflow didn't took one hour, more like 15 minutes. Either I already had some dependencies already installed or the author of that article had a lousy internet connection. In order to run the github demo you also need to install opencv in the same virtual venv as tensorflow
I did that by just entering by hand commands from get_pi_requirements.sh

blimpyway
Posts: 584
Joined: Mon Mar 19, 2018 1:18 pm

Re: Cost-effective device for Image Recognition?

Sun May 31, 2020 12:06 pm

so in short, this script will work if:

1. you uncomment pip install tensorflow
2. remove everything tf_lite install lines which follow.
3. add libgstreamer1.0-0 package at the beginning with the whole bunch of other apt-get commands

Damn, I always get the wrong link:
https://github.com/EdjeElectronics/Tens ... rements.sh

Here is what it should be... I did not tested it for obvious reasons.

Before running it create a new virtualenv for python 3.7 and source its bin/activate

Code: Select all

#!/bin/bash
# !!!!! Blimpyway: Run this in a new venv python 3.7, not directly 

# Get packages required for OpenCV

sudo apt-get -y install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get -y install libxvidcore-dev libx264-dev
sudo apt-get -y install qt4-dev-tools libatlas-base-dev

# Blimpyway: this was needed too:
sudo apt-get -y install libgstreamer1.0-0

# Need to get an older version of OpenCV because version 4 has errors
pip3 install opencv-python==3.4.6.27

# blimpyway - install tensorflow and done. 
pip3 install tensorflow



FalconF16
Posts: 16
Joined: Thu May 28, 2020 1:42 am

Re: Cost-effective device for Image Recognition?

Sun May 31, 2020 4:07 pm

blimpyway wrote:
Sun May 31, 2020 9:54 am
Well... the curiosity killed the cat. What a way to spend a Sunday morning.

After some pain I could manage to run tf lite on pi zero w, roughly as explained in the video above and its github pages.

The detection on image attached took 55 seconds, but after I measured times on various parts of the script, most of them was spend on start up - loading libraries including tensorflow and model.

The actual time spent in image detection was ~15-16 seconds. cucusmall.jpg
This is awesome!! And now it definitely seems possible! Did you not run into any problems with RAM?

blimpyway
Posts: 584
Joined: Mon Mar 19, 2018 1:18 pm

Re: Cost-effective device for Image Recognition?

Sun May 31, 2020 8:28 pm

No the python process was a bit over 100MBytes. There were about 200 left free.

Beware it was running headless, no GUI just ssh terminal.

Also I'd recommend using a Pi 3A+ instead, it isn't much more expensive but at least twice as fast and you can install precompiled tensorflow lite libraries.

Consider the camera itself has a price higher than pi zero, you have to pay for sd, power supply and other accessories so the overall equation isnt 10 vs 20 but more like 30 vs 40 - for doubling performance.

FalconF16
Posts: 16
Joined: Thu May 28, 2020 1:42 am

Re: Cost-effective device for Image Recognition?

Wed Jun 03, 2020 1:57 am

blimpyway wrote:
Sun May 31, 2020 8:28 pm
No the python process was a bit over 100MBytes. There were about 200 left free.

Beware it was running headless, no GUI just ssh terminal.

Also I'd recommend using a Pi 3A+ instead, it isn't much more expensive but at least twice as fast and you can install precompiled tensorflow lite libraries.

Consider the camera itself has a price higher than pi zero, you have to pay for sd, power supply and other accessories so the overall equation isnt 10 vs 20 but more like 30 vs 40 - for doubling performance.
I got a Raspberry Pi Zero! I'll try the example out now and let you know what happens. Thanks!

Return to “Python”