Created
June 16, 2018 18:26
-
-
Save machinelearning147/15e181e025e1bb39c887c9ea730cab65 to your computer and use it in GitHub Desktop.
build_face_dataset using webcam
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
# USAGE | |
# python build_face_dataset.py --cascade haarcascade_frontalface_default.xml --output dataset/adrian | |
# import the necessary packages | |
from imutils.video import VideoStream | |
import argparse | |
import imutils | |
import time | |
import cv2 | |
import os | |
# 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 | |
# do a bit of cleanup | |
print("[INFO] {} face images stored".format(total)) | |
print("[INFO] cleaning up...") | |
cv2.destroyAllWindows() | |
vs.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment