Breaking News

Build a Face Detection App in Python (OpenCV)


1 - Install OpenCV Libraries: pip install opencv-python
2 - Create a new file
3 - Write the code:

import cv2

face_cascade = cv2.CascadeClassifier('venv/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml')


eye_cascade = cv2.CascadeClassifier('venv/Lib/site-packages/cv2/data/haarcascade_eye.xml')


img = cv2.imread('images/face3.jpg')


gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


faces = face_cascade.detectMultiScale(gray, 1.3, 5)


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

    img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
    roi_gray = gray[y:y + h, x:x + w]
    roi_color = img[y:y + h, x:x + w]

    eyes = eye_cascade.detectMultiScale(roi_gray)


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

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

cv2.imshow('img', img)

cv2.waitKey(0)
cv2.destroyAllWindows()


4 - Full Video:

4 comments:

  1. can you make for indentify current face while using camera

    ReplyDelete
  2. I haven't started to investigate the genuine degree of AI in this article, however perhaps I've aroused your advantage. artificial intelligence course

    ReplyDelete
  3. i have this problem when run code
    C:\Users\user\PycharmProjects\Face_Recognition\venv\Scripts\python.exe C:/Users/user/PycharmProjects/Face_Recognition/Face_Rec_OpenCV.py
    Traceback (most recent call last):
    File "C:/Users/user/PycharmProjects/Face_Recognition/Face_Rec_OpenCV.py", line 16, in
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cv2.error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

    ReplyDelete
  4. The content that I normally go through nowadays is not at all in parallel to what you have written. It has concurrently raised many questions that most readers have not yet considered.
    Data Science training in Mumbai
    Data Science course in Mumbai
    SAP training in Mumbai

    ReplyDelete