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:

9 comments:

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

    ReplyDelete
    Replies
    1. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai

      Delete
  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