Object and Animal Recognition With Raspberry Pi and OpenCV

how can i add more objects for recognition it the project?

1 Like

Hey Arham,

To solve your problem, come take a look at this - Edge Impulse . This is free for a maker and will let you customise an already built Machine Learned system (like the Coco library used in this guide).

1 Like

Hey Harold

You will be able to get significantly higher FPS if you turn off the preview window. That will safe the Raspberry Pi a lot of processing power. Should be easy to locate the | Create Preview Window | section in the script and to comment them out.

Hopefully, that helps. I am exploring a AI camera right now (the Oak-D Lite) which will run machine learning scripts at a much higher FPS when attached to a Raspberry Pi. So stay tuned and give it a little looksie.

2 Likes

I think I’ve found the problem. When the rasp pi is detecting an object it’s lagging and will have a 3 seconds delay. How can I fix this?

2 Likes

Hi Harold,

Given the Pi is trying to compute everything in almost real-time there will be some delay(due to hardware-software interactions).

What use case do you have where you need an almost real-time detection?

A unit with a TPU(Tensor processing unit) assisting or doing the bulk of the work, will significantly decrease the delay.

FYI: Behind the scenes the Pi is capturing the image and turning the signal coming in on the CSI cable into a neat human-viewable video stream. Then crunching that stream through a complex neural-net (fancy statistics) then drawing that one your screen.

Quite an amazing process - to optimise this it will take a bit of work/money. some ideas: disable the viewing window on the Pi and cut back resources, you could tighten and train your own neural-net with less ‘neurons’, try other models/ methods of crunching those numbers.

3 Likes

Hi there. I have been implementing your tutorial on my R.PI 3B+ with Buster OS… following the make -j $(nproc) command, it reaches 100% but always completes with the make: *** [Makefile:163: all] Error 2 message… the problem is not resolved even after repeating the make command as highlighted in your tutorial. Excerpt is shown below, any ideas are much appreciated:

/home/pi/opencv-4.4.0/modules/python/src2/cv2.cpp: In instantiation of ‘PyObject* pyopencv_from(const T&) [with T = cv::line_descriptor::KeyLine; PyObject = _object]’:
/home/pi/opencv-4.4.0/modules/python/src2/cv2.cpp:1476:39: required from ‘PyObject* pyopencv_from_generic_vec(const std::vector<_Tp>&) [with _Tp = cv::line_descriptor::KeyLine; PyObject = _object]’
/home/pi/opencv_contrib-4.4.0/modules/line_descriptor/misc/python/pyopencv_LSDDetector.hpp:12:47: required from here
/home/pi/opencv-4.4.0/modules/python/src2/cv2.cpp:74:75: error: ‘from’ is not a member of ‘PyOpenCV_Converter<cv::line_descriptor::KeyLine, void>’
PyObject* pyopencv_from(const T& src) { return PyOpenCV_Converter::from(src); }
~~~~~~~~~~~~~~~~~~~~~~~^
make[2]: *** [modules/python2/CMakeFiles/opencv_python2.dir/build.make:63: modules/python2/CMakeFiles/opencv_python2.dir/__/src2/cv2.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:16693: modules/python2/CMakeFiles/opencv_python2.dir/all] Error 2
make: *** [Makefile:163: all] Error 2

3 Likes

Hi Omar,

Welcome :slight_smile:

Have you run all of the previous commands?
Its sounds like OpenCV might not have been built or a new version has been released.

3 Likes

Hi Liam,

Yep, I have run all the above commands in the given order. thanks…

2 Likes

Heyya Omar,

Sound like something weird has happened (like a Make file has been created corrupted). Since you are using Buster and going through each step carefully I’d recommend focusing on that folder structure where you are running the | make -j $(nproc) | command. Then type and enter a new line | make clean |. That will remove any created files from previous | make | commands. This gives the Raspberry Pi a clean slate to try again. From there run that same | make -j $(nproc) | command.

Also, make sure you have altered the Configure Swapsize setting like so CONF_SWAPSIZE=2048.

Kind regards,
Tim

1 Like

Can we use the code with Raspberry Pi 3 B+ model ?

2 Likes

Heyya mate,

The code is absolutely transferable. That generation of Pi can run Object and Animal Recognition however, you will want to lower the pixel density of the output preview video. Otherwise, it will run significantly slower than a Raspberry Pi 4 Model B.

Hope that helps,
Tim

1 Like

Dear Sir
I am getting error " net = cv2.dnn_DetectionModel(weightsPath,configPath)

AttributeError: module ‘cv2’ has no attribute ‘dnn_DetectionModel’"

ANY ADVICE

1 Like

It is solved , i just updated my opencv from 4.1 to 4.6

2 Likes

Hey guys and Tim! I wanted to train my toy car to detect humanoid-like fighting robots. I got a full database of them, but I don’t know how to add the model to the script from the video and make it work. Is there any tutorial on how to train a model with all of these images? (they’re all in a folder)

1 Like

Hey Nitro,

It’s no longer necessary to be a machine learning expert in order to identify objects in your environment (it’s a proper exciting time to be alive :blush:). To solve your problem, come take a look at this - Edge Impulse . This is free for a maker and will let you customise an already-built Machine Learned system (like the Coco library used in this guide).

With your stack of fighting robot pictures, you will be able to feed them into Edge Impulse, and work through the flow to customise the Coco Library so that they will be included as new identifiable objects. You can also start a new machine-learned object detection system from scratch. Once you have finished you’ll be able to upload/transfer the machine-learned system to your Raspberry Pi and run it completely on the Edge (no internet access required).

Here is a very swell lady, Jenny, figuring creating a Machine Learnt system from scratch on a Jetson Nano using Edge Impulse. I’d recommend using a Desktop Computer to customise/build your Machine Learnt system and then porting it across to your Raspberry Pi Single Board Computer. It is a very computationally intensive process to create/modify machine-learned systems.

Kind regards,
Tim

2 Likes

I have all the components needed for this project. My question is for example if I identify the object, face etc. can I take a photo and save it ? Any tutorials on that that I can be pointed to? Thank you.

1 Like

Hey FlyingMonkey,

You absolutely can do that. Come take a look at this project which used this guide as a springboard - Backyard Birdcam - Tutorial Australia. Very simply, that project identifies birds, takes and stores HQ photos of the identified bird, and then continues normal operation. That has all the Python Scripts free to be observed along with a build process :blush: its proper amazing.

So check that out, if you need more help come post again.
Kind regards,
Tim

1 Like

Hey Tim,

I have read through all the various comments and still have the following question:

-Can we still not use Bullseye? ( I have the same cv2 not installed and errors)
-If Bullseye does work, do you have to activate the legacy Pi Camera feature?
-Will this work fine with CM4 and Pi HQ camera?
-Any special settings for HQ camera to work properly?

Thanks and love the project,
Rick

2 Likes

I have the following problem when I try to run the object-indent.py:

[ WARN:0@1.039] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (2401) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Failed to allocate required memory.
[ WARN:0@1.040] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (1356) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0@1.040] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (862) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
Traceback (most recent call last):
File “/home/pi/Desktop/Object_Detection_Files/object-ident.py”, line 53, in
result, objectInfo = getObjects(img,0.45,0.2)
File “/home/pi/Desktop/Object_Detection_Files/object-ident.py”, line 24, in getObjects
classIds, confs, bbox = net.detect(img,confThreshold=thres,nmsThreshold=nms)
cv2.error: OpenCV(4.6.0) /home/pi/opencv/modules/imgproc/src/resize.cpp:4052: error: (-215:Assertion failed) !ssize.empty(

1 Like

Hey mate,

Last time I set up this system with the attempt to use Bullseye (even after activating legacy Pi Camera features) it wasn’t playing ball. That was ~4 Months ago. It may have been improved since but not sure.

This system will work perfectly with a Raspberry Pi HQ Camera, no special settings need to be changed. I haven’t tried this system with a CM4 however it would be able to do it just fine. Just be aware of the differences in connecting a CM4 to a HQ camera —> CM4 on CM4 IO Board <-> HQ Camera - Raspberry Pi Forums

In regards to your errors above are you running the Bullseye or Buster Version of Raspberry Pi? From my preliminary readings it looks like those errors come up when you use the newer Bullseye Version (and wouldn’t if you used the Older Buster OS) —> GStreamer: pipeline have not been created · Issue #27 · leswright1977/PySpectrometer · GitHub

Kind regards,
Tim

1 Like