Created
August 9, 2020 03:03
-
-
Save danthemango/7abe4b6284f40b881e063613c05ce3ed to your computer and use it in GitHub Desktop.
opens a webcam, and zooms-in on a detectable face
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
import cv2 | |
def getFaceCrop(image): | |
# get image classifier | |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | |
image_copy = np.copy(image) | |
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) | |
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') | |
# Detect faces in the image using pre-trained face dectector | |
faces = face_cascade.detectMultiScale(gray_image, 1.25, 6) | |
# we only care about the first face | |
for f in faces: | |
# Get the bounding box for the face | |
x, y, w, h = [v for v in f] | |
cv2.rectangle(image_copy, (x,y), (x+w, y+h), (255,0,0), 3) | |
# Define the region of interest in the image | |
face_crop = gray_image[y:y+h, x:x+w] | |
return face_crop | |
print("Error: no face detected") | |
return None | |
cap = cv2.VideoCapture(0) | |
while(True): | |
# load video capture image | |
ret, image = cap.read() | |
if ret != True: | |
print("there was an error fetching video capture data") | |
else: | |
face_crop = getFaceCrop(image) | |
if not face_crop is None: | |
resized_face = cv2.resize(face_crop, (256, 256)) | |
cv2.imshow('frame', resized_face) | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment