I just installed the object recognition program on my raspi 4b and am amazed by the solution. Could you be of help to me to rotate the image 90° clockwise due to the camera mounted on a tripod? Thanks in Advance, Robert
Hi Tim,
I was wondering if you could help me out with a plant disease pretrained library. I got everything about the OpenCV working but you had mentioned about using other librarys than the coco library. Can you give a set of instructions on how I could implement this pretrained plant disease library instead of the coco library.
Thanks,
James
cyber.dragon9087@gmail.com
How can I fuse this code with the face recognition one? I’ve tried to do it, but only the face recognition part seems to work.
this is awesome and it have done
i want to use this project of object recognition to create a project of taking a picture and saving as jpeg and counting the amount of fish in the taken picture and the length of the fish, which adjustment of the code should I do
Hi @Tim. I was following the steps, but I came across an error, while running it.
Hey Tim, Great guide, many thanks as it got me started on the right path.
I wanted to use your guide for my project using a Raspberry Pi 4B 4GB with a Camera Module 3 and the latest 32 bit Raspberry Pi OS w/ desktop (3/5/23), and I didn’t really want to go through a build process, or use an older OS or legacy drivers/bindings.
I’m sure this has probably been solved elsewhere, but I couldn’t find it, so I thought I would share a proven working solution that I found that works with your code with almost no changes and NO builds. I also find that this solution appears to detect objects much faster than that reported by others elsewhere in this thread. Anyway this works really well for me and took almost no time to install once the details were worked out…
Raspberry Pi 4B 4GB w/ 64 GB SD, flashed with most recent 32 bit Raspberry Pi OS w/ desktop (3/5/23) and using a Raspberry Pi Camera Module 3
To install OpenCV I followed these steps (not my own)
sudo apt install libjpeg-dev libtiff5-dev libjasper-dev libpng-dev
sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt install libxvidcore-dev libx264-dev
sudo apt install libgtk2.0-dev
sudo apt install libatlas-base-dev gfortran
sudo apt install python3-opencv
sudo apt install openvc-data
sudo apt install ffmpeg
And this is the very slightly modified code, originally written by Tim
#Import the Open-CV extra functionalities
import cv2
cv2.startWindowThread()
from picamera2 import Picamera2
picam2 = Picamera2()
picam2.configure(picam2.create_preview_configuration(main={"format": 'RGB888', "size": (640, 480)}))
picam2.start()
#This is to pull the information about what each object is called
classNames = []
classFile = "/home/rover/Desktop/Object_Detection_Files/coco.names"
with open(classFile,"rt") as f:
classNames = f.read().rstrip("\n").split("\n")
#This is to pull the information about what each object should look like
configPath = "/home/rover/Desktop/Object_Detection_Files/ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt"
weightsPath = "/home/rover/Desktop/Object_Detection_Files/frozen_inference_graph.pb"
#This is some set up values to get good results
net = cv2.dnn_DetectionModel(weightsPath,configPath)
net.setInputSize(640, 480)
net.setInputScale(1.0/ 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)
#This is to set up what the drawn box size/colour is and the font/size/colour of the name tag and confidence label
def getObjects(img, thres, nms, draw=True, objects=[]):
classIds, confs, bbox = net.detect(img,confThreshold=thres,nmsThreshold=nms)
#Below has been commented out, if you want to print each sighting of an object to the console you can uncomment below
#print(classIds,bbox)
if len(objects) == 0: objects = classNames
objectInfo =[]
if len(classIds) != 0:
for classId, confidence,box in zip(classIds.flatten(),confs.flatten(),bbox):
className = classNames[classId - 1]
if className in objects:
objectInfo.append([box,className])
if (draw):
cv2.rectangle(img,box,color=(0,255,0),thickness=2)
cv2.putText(img,classNames[classId-1].upper(),(box[0]+10,box[1]+30),
cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
cv2.putText(img,str(round(confidence*100,2)),(box[0]+200,box[1]+30),
cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
return img,objectInfo
#Below determines the size of the live feed window that will be displayed on the Raspberry Pi OS
#if __name__ == "__main__":
#
# cap = cv2.VideoCapture(0)
# cap.set(3,640)
# cap.set(4,480)
# #cap.set(10,70)
#Below is the never ending loop that determines what will happen when an object is identified.
while True:
img = picam2.capture_array()
#Below provides a huge amount of controll. the 0.45 number is the threshold number, the 0.2 number is the nms number)
result, objectInfo = getObjects(img,0.45,0.2)
#print(objectInfo)
cv2.imshow("Output",img)
cv2.waitKey(1)
First- I am finding issues in finding the zip direrctories when i execute the command cd ~/opencv-4.4.0/
secondly - the code says its wrong
Hello all! Not sure if @Tim observes this thread any more, so I’m hoping someone can help.
I have the opencv and the script running fine. I have this pi in field and need to reserve on power. Therefore I have set up a PIR.
The problem is, I’ve tried to place the opencv code in a PIR loop and running into a wall.
Can somone point me in the right direction to have a pir sensor trigger the opencv to detect a specfic animal, if the animal isn’t there, close the script until motion triggers the PIR again.
Any help is GREATLY appreciated
Thank you for the update and contribution! I’ve used your update and it works great.
Would you know how to introduce motion detection into your script rather than having the “never ending loop”?
Hello Tim, We are seting up Pi4 with Opencv but make -j $(nproc) giving continuous error since last 10 hours and not moving a bit ahead of 41%, it’s showing #include “boostdec.in” and #include"veg.h" not found even though it’s there in opencvcontrib/.cache/xfeatures2d/boostdec . Please give your valuable advice to resolve this and also since image classification an drecognition is working properly without resolving these errors , can we encounter any error in future if we do not resolve this now.
Regards and Thanks,
Hi
I have been trying to get this up and running for 2 days now but I keep getting this errorPython 3.7.3 (/usr/bin/python3)
%cd /home/pi/Desktop/Object_Detection_Files
%Run object-ident.py
Traceback (most recent call last):
File “/home/pi/Desktop/Object_Detection_Files/object-ident.py”, line 1, in
import cv2
ModuleNotFoundError: No module named ‘cv2’
%Run object-ident.py
Hi @Andy262337
This error message ModuleNotFoundError: No module named ‘cv2’
is communicating that python3 can’t find opencv.
The terminal command below prints information about your the opencv installation. Would you mind running it to establish you have it installed and is findable by the linux PATH?
pip show opencv-python
it dosnt show anything its as if opencv-python dosent exsit have I missed a commend somewhere? i ask chatgpt and it told me to do this
ChatGPT
ChatGPT
It looks like the installation process is in progress. Here’s what each part of the output means:
- Collecting opencv-python:
- The
pip3
command is attempting to download and install theopencv-python
package.
- Using cached:
- The package is already available in the local cache, so it doesn’t need to download it again. This is to save time and bandwidth.
- Installing build dependencies:
- Before installing OpenCV, it needs to install its build dependencies. These are tools and libraries necessary for compiling the source code.
- Collecting numpy>=1.17.0:
- OpenCV depends on the NumPy library, so it’s also installing the required version of NumPy.
- Running setup.py bdist_wheel for opencv-python:
- This step involves running the setup script (
setup.py
) to build the wheel distribution package for OpenCV. The wheel format is a binary distribution format that can be installed usingpip
.
It seems that the output got cut off, and it didn’t show the completion of the wheel build. Typically, you would see a progress indicator, and once the process completes, it will display a message indicating a successful installation.
If the process completes without any errors, you should be able to use OpenCV in your Python scripts. If there are any issues, error messages would usually be displayed in the terminal. If the installation fails, make sure to check the error messages for clues on what went wrong.
and the message i got back isAn error occurred while building with CMake.
Command:
/tmp/pip-build-env-kaag0f45/lib/python3.7/site-packages/cmake/data/bin/cmake --build . --target install --config Release –
Install target:
install
Source directory:
/tmp/pip-install-hy39svcg/opencv-python
Working directory:
/tmp/pip-install-hy39svcg/opencv-python/_skbuild/linux-armv7l-3.7/cmake-build
Please check the install target is valid and see CMake’s output for more information.
----------------------------------------
Command “/usr/bin/python3 -u -c “import setuptools, tokenize;file=‘/tmp/pip-install-hy39svcg/opencv-python/setup.py’;f=getattr(tokenize, ‘open’, open)(file);code=f.read().replace(‘\r\n’, ‘\n’);f.close();exec(compile(code, file, ‘exec’))” install --record /tmp/pip-record-okem0_5y/install-record.txt --single-version-externally-managed --compile --user --prefix=” failed with error code 1 in /tmp/pip-install-hy39svcg/opencv-python/
pi@raspberrypi:~ $
I have try starting a fresh 3 times im just not sure what im doing wrong
The only bit i can see that i could be mucking up is im not sure if im changing it as it doest let me edit it?
The change the number on CONF_SWAPSIZE = 100 to CONF_SWAPSIZE=2048 . Having done this press Ctrl-X, Y, and then Enter Key to save these changes. This change is only temporary and we will be changing it back. To have these changes affect anything we must restart the swapfile by entering the following command to the terminal. Then we will resume Terminal Commands as normal.
Hi Andy.
Let’s try installing it.
pip install opencv-python
thank you! i run a new install and realised part of the opencv-python wasnt there.
ended up using chatgpt to work out what was missing and its now up and running.
next question how do i add to the coco
Nice one.
I’m sorry I don’t have any personal experience with the coco library.
I’m sure someone else will assist
Is it possible to perform an animal recognition without using a camera? (eg. reading images or videos directly from an SD Card?)
Or does ML actually require a live video feed?
Hi @Jonathan263374
Good to have you with us.
Sure thing
Let’s look at the loop of @Tim’s code.
while True:
success, img = cap.read()
result, objectInfo = getObjects(img,0.45,0.2)
cv2.imshow("Output",img)
cv2.waitKey(1)
Let’s start at the top.
cap
is a cv2.VideoCapture() object and it’s creating a variable called img
.
img
is an image object. Under the hood it’s just an array of pixels created by our camera.
img
is then passed into this getObject()
function which is doing all our ML heavy lifting.
Note that getObject()
has no idea, and doesn’t care, where the image came from.
getObject()
just want’s an image; any image.
Instead of assigning img
to the output of our camera, we can ditch the VideoCapture object entirely and assign img
to an file read from disk.
To do that, let’s replace this line success, img = cap.read()
with something like:
img = cv2.imread('myPic_of_a_Swordbilled_Hummingbird.jpg')
success = bool(img)
img
is now a file from disk, not a camera output. We pass the file img
into getObject()
.
getObject()
is a happy function because it got an image, which is all it wanted.
while True: #you may not need this infinite loop if there is only one image
img = cv2.imread('myPic_of_a_Swordbilled_Hummingbird.jpg')
success = bool(img)
result, objectInfo = getObjects(img,0.45,0.2)
cv2.imshow("Output",img)
cv2.waitKey(0) #Wait for keypress
Hopefully that’s clear but don’t hesitate to ask for clarification.
Pix