Skip to content

Instantly share code, notes, and snippets.

@machinelearning147
Last active November 20, 2018 14:29
Show Gist options
  • Save machinelearning147/e4c486d274c8c3cbd7d6641cbf874ba6 to your computer and use it in GitHub Desktop.
Save machinelearning147/e4c486d274c8c3cbd7d6641cbf874ba6 to your computer and use it in GitHub Desktop.
Build face-dataset for face recognition application
# USAGE
# python build_face_dataset.py --cascade haarcascade_frontalface_default.xml --output dataset/shravan
# import the necessary packages
from imutils.video import VideoStream
import argparse
import imutils
import time
import cv2
import os
import sys
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-c", "--cascade", required=True,
help = "path to where the face cascade resides")
ap.add_argument("-o", "--output", required=True,
help="path to output directory")
args = vars(ap.parse_args())
# load OpenCV's Haar cascade for face detection from disk
detector = cv2.CascadeClassifier(args["cascade"])
directory = args["output"]
if not os.path.exists(directory):
os.makedirs(directory)
# initialize the video stream, allow the camera sensor to warm up,
# and initialize the total number of example faces written to disk
# thus far
print("[INFO] starting video stream...")
vs = VideoStream(src=1).start()
# vs = VideoStream(usePiCamera=True).start()
time.sleep(2.0)
total = 0
# loop over the frames from the video stream
while True:
# grab the frame from the threaded video stream, clone it, (just
# in case we want to write it to disk), and then resize the frame
# so we can apply face detection faster
frame = vs.read()
orig = frame.copy()
frame = imutils.resize(frame, width=400)
# detect faces in the grayscale frame
rects = detector.detectMultiScale(
cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), scaleFactor=1.1,
minNeighbors=5, minSize=(30, 30))
# loop over the face detections and draw them on the frame
for (x, y, w, h) in rects:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 0))
# show the output frame
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
# if the `k` key was pressed, write the *original* frame to disk
# so we can later process it and use it for face recognition
if key == ord("k"):
p = os.path.sep.join([args["output"], "{}.png".format(
str(total).zfill(5))])
roi = frame[y:y + h, x:x + w]
cv2.imshow("roi", roi)
key = cv2.waitKey(1) & 0xFF
cv2.imwrite(p, roi)
total += 1
# if the `q` key was pressed, break from the loop
elif key == ord("q"):
break
cv2.destroyAllWindows()
# do a bit of cleanup
print("[INFO] {} face images stored".format(total))
print("[INFO] cleaning up...")
cv2.destroyAllWindows()
vs.stop()
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment