Skip to content

Commit

Permalink
Face Detection using python
Browse files Browse the repository at this point in the history
  • Loading branch information
DhanushNehru committed Sep 19, 2020
1 parent 3c50151 commit b14f305
Show file tree
Hide file tree
Showing 5 changed files with 33,379 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions Day 21 - Face Recognition using python/detect_face_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# !/usr/bin/python3
import cv2

# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# Read the input image
img = cv2.imread('SamplePic.jpg')

# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)

# Draw rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)

# Display the output
cv2.imshow('img', img)
cv2.waitKey()
35 changes: 35 additions & 0 deletions Day 21 - Face Recognition using python/detect_face_video.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# !/usr/bin/python3
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()
Loading

0 comments on commit b14f305

Please sign in to comment.