# Opencv face recognition

**URL:** <https://forum.opencv.org/t/opencv-face-recognition/3812>\
**Category:** Python\
**Tags:** face\
**Created:** [June 13, 2021, 7:06am UTC](https://forum.opencv.org/t/opencv-face-recognition/3812 "2021-06-13T07:06:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jophin123](https://avatars.discourse-cdn.com/v4/letter/j/50afbb/32.png) [@jophin123](https://forum.opencv.org/u/jophin123)\
**Post date:** [June 13, 2021, 7:06am UTC](https://forum.opencv.org/t/opencv-face-recognition/3812/1 "2021-06-13T07:06:50Z")

</div>

I watched a YouTube video about face recognition and done same as that he told.But at the time of face recognition it recognise all the peoples as same.what is the reason.code is

```auto
import cv2

def draw_boundary(img, classifier, scaleFactor, minNeighbors, color, text, clf):
    # Converting image to gray-scale
    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # detecting features in gray-scale image, returns coordinates, width and height of features
    features = classifier.detectMultiScale(gray_img, scaleFactor, minNeighbors)
    coords = []
    # drawing rectangle around the feature and labeling it
    for (x, y, w, h) in features:
        cv2.rectangle(img, (x,y), (x+w, y+h), color, 2)
        # Predicting the id of the user
        id, _ = clf.predict(gray_img[y:y+h, x:x+w])
        # Check for id of user and label the rectangle accordingly
        if id==1:
            cv2.putText(img, "cat", (x, y-4), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 1, cv2.LINE_AA)
        coords = [x, y, w, h]

    return coords

# Method to recognize the person
def recognize(img, clf, faceCascade):
    color = {"blue": (255, 0, 0), "red": (0, 0, 255), "green": (0, 255, 0), "white": (255, 255, 255)}
    coords = draw_boundary(img, faceCascade, 1.1, 10, color["white"], "Face", clf)
    return img

# Loading classifier
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# Loading custom classifier to recognize
clf = cv2.face.LBPHFaceRecognizer_create()
clf.read("classifier.yml")

# Capturing real time video stream. 0 for built-in web-cams, 0 or -1 for external web-cams
video_capture = cv2.VideoCapture(-1)

while True:
    # Reading image from video stream
    _, img = video_capture.read()
    # Call method we defined above
    img = recognize(img, clf, faceCascade)
    # Writing processed image in a new window
    cv2.imshow("face detection", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# releasing web-cam
video_capture.release()
# Destroying output window
cv2.destroyAllWindows()

```

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [June 13, 2021, 8:03am UTC](https://forum.opencv.org/t/opencv-face-recognition/3812/2 "2021-06-13T08:03:01Z")

</div>

what is the purpose of your program ?  
what are you trying to achieve here ?  
how did you train this ?

---

<div class="post-metadata">

**Author:** ![jophin123](https://avatars.discourse-cdn.com/v4/letter/j/50afbb/32.png) [@jophin123](https://forum.opencv.org/u/jophin123)\
**Post date:** [June 13, 2021, 9:21am UTC](https://forum.opencv.org/t/opencv-face-recognition/3812/3 "2021-06-13T09:21:18Z")

</div>

mypurpose is to recognize people usingopencv.i trained data usingfacedetection by haarcascade

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [June 13, 2021, 12:15pm UTC](https://forum.opencv.org/t/opencv-face-recognition/3812/4 "2021-06-13T12:15:29Z")

</div>

expect this stuff to work for humans but not for cats. it might, but it’s not designed for that.

[https://docs.opencv.org/master/dd/d65/classcv\_1\_1face\_1\_1FaceRecognizer.html](https://docs.opencv.org/master/dd/d65/classcv_1_1face_1_1FaceRecognizer.html)
