openCV and raspberrypi cmera module 3 on Rpi4b

Which OS version should be used with this configuration for object and facial recognition please?
I am unsure as most of the threads seem to be from quite a few years ago!
Thank you

1 Like

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 worked for me last September.

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 using a slightly modified code, originally written by Tim in the original post 'Object and Animal Recognition With Raspberry Pi and OpenCV

#Import the Open-CV extra functionalities
import cv2

from picamera2 import Picamera2
picam2 = Picamera2()
picam2.configure(picam2.create_preview_configuration(main={"format": 'RGB888', "size": (640, 480)}))

#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 ="\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))

#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     
    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: 
                if (draw):

    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)


Hi Mark,
that’s brilliant.
Thank you very much for taking the time to answer my question.
Much appreciated.
All the best,

1 Like