# Catch object detection

**URL:** <https://forum.opencv.org/t/catch-object-detection/1470>\
**Category:** Python\
**Created:** [February 11, 2021, 3:18am UTC](https://forum.opencv.org/t/catch-object-detection/1470 "2021-02-11T03:18:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miguel\_Castillo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/miguel_castillo/32/980_2.png) [@Miguel\_Castillo](https://forum.opencv.org/u/Miguel_Castillo)\
**Post date:** [February 11, 2021, 3:18am UTC](https://forum.opencv.org/t/catch-object-detection/1470/1 "2021-02-11T03:18:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kbarni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/kbarni/32/26_2.png) [@kbarni](https://forum.opencv.org/u/kbarni)\
**Post date:** [February 11, 2021, 9:39am UTC](https://forum.opencv.org/t/catch-object-detection/1470/2 "2021-02-11T09:39:23Z")

</div>

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")
```
