Real Time Face Detection Using OpenCV & python - By ShadBox

Total Blog Views: 50

Blog Status: publish

Created By: swazahmad Created at: 07-15-2021

Tags: python basicprogramming web development shadbox face detection software engineering

In this post i wanted to share face detection using  using Haar cascades in OpenCV and Python
 
Haar Cascade is a machine learning object detection algorithm used to identify objects in an image or video and based on the concept of ​​ features.
 
-First install OpenCV library using command 
 
pip install opencv-python

- You need to download Haa cascade XML (haarcascade_frontalface_default.xml ) which is available on OpenCv’s GitHub repository

- we can detect faces in videos. As you know videos are basically made up of frames, which are still images. So we perform the face detection for each frame in a video. Here is the code:

 
  import cv2
  # Load the cascade
  face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
   
  # To capture video from webcam.
  cap = cv2.VideoCapture(0)
  # To use a video file as input
  # cap = cv2.VideoCapture('filename.mp4')
   
  while True:
  # Read the frame
  _, img = cap.read()
  # Convert to grayscale
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  # Detect the faces
  faces = face_cascade.detectMultiScale(gray, 1.1, 4)
  # Draw the rectangle around each face
  for (x, y, w, h) in faces:
  cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
  # Display
  cv2.imshow('img', img)
  # Stop if escape key is pressed
  k = cv2.waitKey(30) & 0xff
  if k==27:
  break
  # Release the VideoCapture object
  cap.release()


swazahmad

Swaz is Practice Lead of ROR Developer. he's a Blogger, Mentor, and Rubyst. He has expertise in Javascript also . he's highly skilled in designing, developing and testing web applications. He enjoys being challenged and interesting with projects that need him to



Comments



  • swazahmad | almost 4 years ago
    Awesome Information Thanks For Sharing
Buy traffic for your website

About Shadbox

we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.

Services

Downloads