Object and Animal Recognition With Raspberry Pi and OpenCV

WOW!
That is one of thee most thorough and clear cut reply’s I have ever gotten!!!
This gives me the confidence to give it a shot!!!
Thanks for taking the time to reply! @Pixmusix

2 Likes

Awesome. :+1:
Let me know how you get on and happy making! :slight_smile:
Pix :heavy_heart_exclamation:

Does anyone know how to fix this Error:
pi@raspberrypi:~/opencv-4.4.0/build $ cmake -D CMAKE_BUILD_TYPE=RELEASE \

CMake Warning:
No source or binary directory provided. Both will be assumed to be the
same as the current working directory, but note that this warning will
become a fatal error in future CMake releases.

CMake Error: The source directory “/home/pi/opencv-4.4.0/build” does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.
pi@raspberrypi:~/opencv-4.4.0/build $

Im facing a lot of trouble with this

1 Like

Hi @Ishanth263430
Welcome to the forums.

With opencv there is this finiky step where you need to rememeber to make a build folder.

cd ~/opencv_build/opencv
mkdir build && cd build

That’s where PATH finds CMakeLists.txt.

I always forget this step.
Every single time I try to install openCV I have to google it and I always find this exact same stackoverflow thread. Every… single… time… :man_facepalming:

Even answering this question, I had to google it to remind myself of the solution.
Pix :heavy_heart_exclamation:

Ohhh, thanks so much

1 Like

He everyone,

I installed everything and now I want to run Python, unfortunately the code doesn’t work for me. I’ve tried different solutions to debug reading on the forums, when I took a simple snapshot of myself with the camera, Python opened a window for me. It was loaded unfortunately, when I load all the devices that are like a video, so I want to try them there, I switch them in python. I’ll rewrite it. Unfortunately this doesn’t work either. Can you please help me a lot my configuration is: RP4B + RP camera 3.

Test camera:
a.) libcamera-still -o image.jpg ------- it is OK, I take a photo
b.) test in python from the image is OK, code:
import cv2

image = cv2.imread(‘image.jpg’)

if image is not None:
height, width = image.shape[:2]
resized_image = cv2.resize(image, (1920 // 2, 1080// 2))

cv2.imshow('Resized Image', resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

else:
print(“Obrázek nebyl nalezen nebo nelze načíst. Zkontrolujte cestu k souboru.”)

c.) I change cv2.VideoCapture(0) so many times in python … So I tried it with static image and a few upgrade, it function!
My code:
import cv2

Threshold to detect object

thres = 0.45
nms_threshold = 0.2

Load names of classes

classNames =
classFile = ‘coco.names’ # Update with the path to your coco.names file
with open(classFile, “rt”) as f:
classNames = f.read().rstrip(“\n”).split(“\n”)

Set up the configuration and weight files for the model

configPath = ‘ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt’ # Update with the path to your config file
weightsPath = ‘frozen_inference_graph.pb’ # Update with the path to your weights file

Load the model

net = cv2.dnn_DetectionModel(weightsPath, configPath)
net.setInputSize(320, 320)
net.setInputScale(1.0 / 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)

Function to get the detected objects and modify the image to highlight them

def getObjects(img, thres, nms, draw=True, objects=):
classIds, confs, bbox = net.detect(img, confThreshold=thres, nmsThreshold=nms)
objectInfo =
if len(objects) == 0: objects = classNames
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:
# Draw rectangle and label for each object
cv2.rectangle(img, box, color=(0, 255, 0), thickness=2)
cv2.putText(img, f’{className.upper()} {round(confidence * 100, 2)}%',
(box[0] + 10, box[1] + 30), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
return img, objectInfo

Main function to load the image and apply object detection

if name == “main”:
# Load image
img = cv2.imread(‘image.jpg’) # Update with the path to your image file

# Resize image to HD resolution before processing if it's larger
img_height, img_width = img.shape[:2]
if img_width > 1280 or img_height > 720:
    img = cv2.resize(img, (1280, 720))

# Get object detection results
result, objectInfo = getObjects(img, thres, nms_threshold)
print("Detected Objects:", objectInfo)  # Print information about detected objects

# Resize the output for display if needed
result = cv2.resize(result, (1280, 720))  # Resize to HD 720p

# Display the output
cv2.imshow("Output", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Please help …

It seems like you’re encountering issues with object detection using OpenCV in Python, particularly with a Raspberry Pi 4B and the Raspberry Pi Camera Module 3. There could be a few potential areas causing problems:

  1. Camera Initialization: Ensure that your Raspberry Pi Camera Module is properly connected and enabled. You might need to enable the camera via sudo raspi-config and navigate to Interfacing Options > Camera.
  2. Check Camera Access in Python: The camera might not be accessible in Python due to permission issues. Run Python scripts requiring camera access with administrative privileges using sudo python your_script.py.
  3. Correct File Paths: Make sure that the file paths you’re using (coco.names, ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt, frozen_inference_graph.pb, and 'image.jpg') are correct and pointing to the right locations in your filesystem.
  4. Correct Model and Configuration Files: Ensure that the model and configuration files you’re using for object detection are compatible with the OpenCV version and the hardware (Raspberry Pi in this case). Sometimes, certain models might not work optimally due to hardware limitations.
  5. Model Input Size: The input size for your model might be too large for the Raspberry Pi’s processing capabilities. Try adjusting the input size to smaller dimensions like 320x320 or 416x416 to see if it improves performance.
  6. Check Dependencies: Ensure you have all necessary libraries and dependencies installed for OpenCV and other required modules. Sometimes, missing dependencies can cause issues.
  7. Debugging Output: Add print statements at various stages of your code to see where the problem might be occurring. For instance, print the shapes of loaded images, check for errors during model loading, or print the results of class name loading.
  8. Check Camera Access: Test the camera access using simple scripts or commands (raspistill) to ensure the camera is working and accessible by the Raspberry Pi.
  9. Debug the getObjects Function: Verify that the getObjects function is correctly processing the detections. Print out the values inside the function to check if the detected objects are as expected.

Hope that helps!!
Ishanth

3 Likes

Hi,

thank you very much. I tried everything but with zero results. My friend have a idea, go and try web cam. So now it works!

Thank you very much a thanks for this tutorial!

1 Like

Thanks for your post Mark. I was able to get the project working following your updates. I appreciate it!

1 Like

Could this also work with a Raspberry Pi 5 64-bit? Additionally, I would like to use a USB webcam. Is this possible?

how should i add custom data model or object for detection, as i want to detect tiger using raspberry pi, therefore please help me

Hello, tried this on my raspberry pi 3b+. Im using camera module v2. I can’t see the camwra in the raspberry pi configuration. Any ideas how to fix it? Or i can use a webcam as alternative? Pls i need your reply.

Hy tim how to operate the Raspberry Pi to identifies a cup and it will activate the servo. Can you give a guidance for me.

1 Like

Hi
did you resolve your error ? how ? i try this project for my student and i have the same
thanks

Hi Tim, i’ve the same error than devanshu189371
i’m a teacher and i would teach ai to my student with this project to extend birds.
first i do it for myself and i’ve the same error
i have a pi4 8g0, raspbian. opencv works well (i’ve tested the version) but not object_detection_files.py
can you help me ?
sorry for my bad english, but i’m french
Pascal

Hi @BERNARD268259

Welcome! Good to have you with us.

Tricky one, but I’m sure we can find a solution.

Below is an excerpt from devanshu’s error message that really stuck out to me.

(-215:Assertion failed) !ssize.empty() in function ‘resize’

There are a lot of reasons this error might have surfaced, but the general theme here is that whatever the variable img is (which is the image your are trying to process), it’s either malformed or unfound.

For example, Opencv might fires this error above when the img variable contains something with 0 width or 0 height.

The first thing I’d want to know is which image caused this problem.
What I think we should implement is “Catching” this error and then printing out what caused our problem.
In python, we catch an error with something called a try block.

def getObjects(img, thres, nms, draw=True, objects=[]):
   try:
      classIds, confs, bbox = net.detect(img,confThreshold=thres,nmsThreshold=nms)
   except:
      if img is None:
         print("Looks like the image you're trying to process is... not a thing?")
      else:
         print(f"Something went wrong when trying to process the following image : {img}") 
      print("Error caught, exiting code")
      exit()
   
   # continue on with the rest of the code.
   if len(objects) == 0: objects = classNames
      objectInfo =[]
   # ... more code blah blah

So you can see above I’ve wrapped the offending line of code in this try except syntactic sugar.
Now IF that error pops up again, we can learn more about which image caused the problem.

If you like, give that a go and get back in touch. We’d love to know what image caused this error so we can inspect it. That should lead us towards a solution.

Let us know if you have any questions :slight_smile:
Pix :heavy_heart_exclamation:

Hi, I am new to pi and I am having trouble setting up CV2.
I cannot seem to complete the steps after mkdr build


Hi @Ellen268327

Welcome! Good to have you with us. :slight_smile:

I think your error is similar to a someone I helped on this thread last year.
My reply here seemed to help them; maybe it will help you?

Let me know if that works for you too :+1:
Pix :heavy_heart_exclamation:

hi

thanks for your reply but my error was not enough described.
I don’t understand because it seems that i did the procedure as described

here the error :
[ WARN:0@10.998] global cap_v4l.cpp:1134 tryIoctl VIDEOIO(V4L2:/dev/video0): select() timeout.
[ WARN:0@11.002] global dnn_utils.cpp:166 blobFromImagesWithParamsImpl Red/blue color swapping requires at least three image channels.
Traceback (most recent call last):
File “/home/pascal/Desktop/Object_Detection_Files/object-ident.py”, line 50, in
result, objectInfo = getObjects(img,0.45,0.2)
^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/pascal/Desktop/Object_Detection_Files/object-ident.py”, line 21, in getObjects
classIds, confs, bbox = net.detect(img,confThreshold=thres,nmsThreshold=nms)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cv2.error: OpenCV(4.9.0) /io/opencv/modules/imgproc/src/resize.cpp:4152: error: (-215:Assertion failed) !ssize.empty() in function ‘resize’


(program exited with code: 1)
Press return to continue