Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shravankumar147/705439aec2fac57ee60719d5f2fafa73 to your computer and use it in GitHub Desktop.
Save shravankumar147/705439aec2fac57ee60719d5f2fafa73 to your computer and use it in GitHub Desktop.
build_face_dataset using webcam
# 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()
@ramkkreddy14
Copy link

error. help me

runfile('C:/build face dataset/build_face_dataset.py', wdir='C:/build face dataset')
usage: build_face_dataset.py [-h] -c CASCADE -o OUTPUT
build_face_dataset.py: error: the following arguments are required: -c/--cascade, -o/--output
An exception has occurred, use %tb to see the full traceback.

@HachemOuafa04
Copy link

error. help me

runfile('C:/build face dataset/build_face_dataset.py', wdir='C:/build face dataset')
usage: build_face_dataset.py [-h] -c CASCADE -o OUTPUT
build_face_dataset.py: error: the following arguments are required: -c/--cascade, -o/--output
An exception has occurred, use %tb to see the full traceback.

I have the same error. something wrong about the argument parser but I couldn't find what is it? help, please.

@prajwalnl
Copy link

error. help me

runfile('C:/build face dataset/build_face_dataset.py', wdir='C:/build face dataset')
usage: build_face_dataset.py [-h] -c CASCADE -o OUTPUT
build_face_dataset.py: error: the following arguments are required: -c/--cascade, -o/--output
An exception has occurred, use %tb to see the full traceback.

try following steps
1)open powershell inside your project
2) copy this command and enter "python build_face_dataset.py --cascade haarcascade_frontalface_default.xml --output dataset/adrian "

@abhichaudhri
Copy link

python build_face_dataset.py --cascade haarcascade_frontalface_default.xml --output dataset/abhishek

python + your python file name + haarcascade xml file + output keyword + file where u want to save the code

nd possibly keep the output folder in same folder as the code

hope it helps!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment