# import the necessary packages
import argparse
import datetime
import time

import cv2

import imutils
from imutils.video import VideoStream

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--picamera", type=int, default=-1,
	help="whether or not the Raspberry Pi camera should be used")
args = vars(ap.parse_args())
# initialize the video stream and allow the cammera sensor to warmup
vs = VideoStream(usePiCamera=args["picamera"] > 0).start()
time.sleep(2.0)

# Load a cascade file for detecting faces
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")


# loop over the frames from the video stream
while True:
	# grab the frame from the threaded video stream and resize it
	# to have a maximum width of 400 pixels
	frame = vs.read()
	frame = imutils.resize(frame, width=400)

	# Convert into grayscale
	gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
	# # Look for faces in the image using the loaded cascade file
	faces = faceCascade.detectMultiScale(gray, 1.2, 5)
	for (x,y,w,h) in faces:
		# Create rectangle around faces
		cv2.rectangle(frame,(x,y),(x+w,y+h),(255,255,0),2)

	# show the frame
	cv2.imshow("Frame", frame)
	key = cv2.waitKey(1) & 0xFF
	# if the `q` key was pressed, break from the loop
	if key == ord("q"):
		break
# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()


