Catch object detection

Hi, i wonder if there is a way to catch the event of detecting something to implement an if statement like this:

If “object is detected”:
do something
else:
do something

Yes, it’s possible. What method are you using for object detection?
Normally OpenCV processes frame by frame and the processing chain is linear; so you can put your code in the chain without catching events.
For example, using the face detection cascade classifier:

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv.VideoCapture(0)
while(True):
    ret, frame = cap.read() # get a frame from the webcam
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.1, 4) # detect the faces
    if len(faces)>0: 
        print("detected "+str(len(faces))+" faces")
   else:
        print("no faces detected")
1 Like