0

I have started to learn OpenCV lib in Python. I want to write a code that will detect faces and eyes on the photo but also count number of detections. I have wrote a picture that has 6 people on it. You can download the image form here:

https://www.sendspace.com/file/cznbqa

I wrote a code that detect faces and eyes, but I do not know how to count number of detections. Also, my code detect 4/8 faces and 1/12 eyes on the photo so...I probably have some issues in the code, If you can help me with that also, I would be very thankful.

 # Standard imports

import cv2
import numpy as np

# Read image
my_image = cv2.imread("ljudi.jpg", 1)

# data for detecting faces and eyes
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')


img = cv2.imread("ljudi.jpg", 1)
gray = cv2.cvtColor(img, 0)

faces = face_cascade.detectMultiScale(gray, 1.2, 4)

for (x,y,w,h) in faces:

    #detect face with bule rectangle
    #rec. start point (x,y), rec. end point (x+w, y+h), blue color(255,0,0), line width 2
    face_rec = cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 1)        

    roi_gray = gray[y:y+h, x:x+w]
    roi_color = img[y:y+h, x:x+w]

    eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 2)

    for (ex,ey,ew,eh) in eyes:

        eye_rec = cv2.rectangle(roi_color, (ex,ey), (ex+ew, ey+eh), (0,255,0), )

cv2.imshow('img', img)
cv2.waitKey(0)
3
  • 1
    len(faces) (and len(eyes) respectively) does not do what you need? Commented Oct 11, 2019 at 12:00
  • lol... yeah! Thanks my friend Commented Oct 11, 2019 at 12:09
  • I've added this trivial info as answer just so this problem can be marked as solved.. Commented Oct 12, 2019 at 8:54

1 Answer 1

1

From the OpenCV documentation:

https://www.docs.opencv.org/2.4/modules/objdetect/doc/cascade_classification.html#cascadeclassifier-detectmultiscale

Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.

where a rectangle for the Python bindings simply seems to be of type list [x,y,w,h] (https://www.docs.opencv.org/2.4/modules/core/doc/basic_structures.html#rect).

To the number of faces / rectangles returned inside the list can easily be retrieved by getting the length of the list (i.e. number of items inside), which is done with the Python builtin function len(): https://docs.python.org/3/library/functions.html#len.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, can you maybe help me with this question also? stackoverflow.com/questions/58349314/…
@taga Sorry, have not used those libraries yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.