ads_header

Face Detection Using Python and OpenCV


Hello friend, in this post we will create a "Face Detection" program using the OpenCV library using the Python programming language. "Face Detection" is a program that can recognize a face in a frame, be it in the form of images or videos. One use of this technique is the attendance process carried out in an agency so that there is no forgery of attendance.





First, open your editor, here I use PyCharm then create a file named "index.py". When done, make sure you already have the file "haarcascade_frontalface_default.xml" if you don't have it, please download here and put it in the same folder as "index.py"

Open index.py and enter the following code

import cv2

faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

cam = cv2.VideoCapture(0)

First, we import the OpenCV library, then we load the haarcascade_frontalface_default.xml file into the "faceCascade" variable, then activate the camera loaded in the "cam" variable

Note : If you use an external camera, please change cv2.VideoCapture (0) to cv2.VideoCapture (1)

Next, enter the code below

while True:

    ret, im = cam.read()

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

    faces = faceCascade.detectMultiScale(gray, 1.2,5)

The above code serves to do continuous looping on the code inside
  • Every frame taken using an external webcam or camera is entered into the variable "im"
  • Converting to Gray mode variable "im"
  • Detect all faces in the frame





Next, enter the following code

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

         cv2.rectangle(im, (x-20,y-20), (x+w+20, y+h+20), (0,255,0), 4)

    cv2.imshow('Face Detection', im)

The code above serves to make a green box in the face area that is detected in the frame. Then display in a new window with the name "Face Detection". When finished, enter the following closing code so that the window still appears.

    if cv2.waitKey(10) & 0xFF == ord('q'):
        break
cam.release()
cv2.destroyAllWindows()

At this point, the program is ready to run.

Full Source Code :


import cv2

faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

cam = cv2.VideoCapture(0)

while True:

    ret, im = cam.read()

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

    faces = faceCascade.detectMultiScale(gray, 1.2,5)

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

        cv2.rectangle(im, (x-20,y-20), (x+w+20, y+h+20), (0,255,0), 4)

    cv2.imshow('Face Detection', im)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break
cam.release()
cv2.destroyAllWindows()


14 comments:

  1. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
    you can visit us if you are looking for PEB Manufacturers

    ReplyDelete
  2. Thank you so much for this blog...

    If you are looking for the best blockchain game development company. Do visit Webllisto, it is one of the best companies that provide smart contract development services, metaverse development services, Defi development services, Defi wallet development services, and internet marketing services for various industries. Visit the website - https://webllisto.com/

    ReplyDelete

  3. Value assignment help act as a tuition teacher and guides students looking for online academic assistance for assignments, homework, and essay writing services. contact valueassignmenthelp.com they provide plagiarism free content. Value assignment help is the best solution for you. They are best known for delivering assignment help services to students without having to break the bank. whatsapp no.: +91-9872003804, +61-413228507.

    ReplyDelete
  4. I find it useful is the ability to call Quickbooks phone numbers for help. This can be done by dialing Quickbooks Customer Service Support Phone Number +16023251557 When you do, you will reach the customer service team, who will be able to provide assistance with any question or issue that you may have.

    ReplyDelete
  5. I read your article and all the information in the article is very good and I am glad to read this useful information thank you for sharing the information.
    Quickbooks Payroll Support.+1 866-669-5068

    ReplyDelete
  6. Thanks for sharing your blog. In this Blog you have given an Informative Information which are very important for users.

    Best Shared Web Hosting

    Managed Server Hosting

    Unmanaged Dedicated Server Hosting

    Best Minecraft Server Hosting

    ReplyDelete
  7. When you or your company need help with QuickBooks or any other aspect of your business, dial Quickbooks Support Phone Number +1 888-210-4052.

    ReplyDelete
  8. Learn and discover classes on MS Office Computer Course and do practicals on Excel, Word, PowerPoint, Outlook. Path Creator designed an online Microsoft Course in Kolkata where you will get advanced training for MS- Excel and Learn MS Office in Kolkata. Our Course is designed to Learn to write documents, take notes, create spreadsheets, and more.

    Learn MS Office in Kolkata

    ReplyDelete
  9. Hi - Nice Article thanks for sharing with me.

    forex investor leads

    ReplyDelete
  10. Hey great post
    https://www.wamatechnology.com/top-ios-app-development-company-in-mumbai

    ReplyDelete
  11. If you are looking for contact information for QuickBooks, then be sure to give them a call at Quickbooks Customer Service +1 888-210-4052.

    ReplyDelete
  12. I read your blog its nice i like content its very useful for me
    I am Amara William I am always ready to learn new things and resolve problem regarding.
    I am a help you to resolve your quick book error, I am independent expert last 5 year to solve all error of quick book QuickBooks , working dedicatedly with the reliable QuickBooks support provider for resolving QuickBooks errors efficiently.


    https://www.qberrorsupport.com/convert-quickbooks-online-company-file-to-desktop/

    ReplyDelete
  13. QuickBooks is the most recommended and widely used accounting software which helps you get your work done easily and efficiently. This software comes with a customer service team that is active 24/7, so you can get answers for any sort of Quickbooks Customer Service question. You can contact them by dialling +1 615-510-6154.

    ReplyDelete
  14. Quickbooks is amazing software is used for small companies. if you are looking growth of their company then contact for any issue at
    QuickBooks Support Phone Number: +1 855-675-3194.

    ReplyDelete

Powered by Blogger.