diff --git a/Face_Detect with borders.py b/Face_Detect with borders.py index 587edba..24dc7df 100644 --- a/Face_Detect with borders.py +++ b/Face_Detect with borders.py @@ -2,6 +2,8 @@ import cv2 import numpy as np import argparse import sys +import time +import os import datetime def init_argparse() -> argparse.ArgumentParser: @@ -22,6 +24,12 @@ def init_argparse() -> argparse.ArgumentParser: parser.add_argument( "-f", "--file", nargs="?", help="File to scan instead of using the camera. Useful for generating training data" ) + parser.add_argument( + "-s", "--no-screen", action='store_true', help="Do not show the successful frames" + ) + parser.add_argument( + "-t", "--training-data", action='store_true', help="When set, saves successful face-location images and coordinates to use for future training data" + ) return parser multiplication_factor = 0.05 @@ -65,12 +73,22 @@ parser = init_argparse() args = parser.parse_args() cap = cv2.VideoCapture(0, cv2.IMREAD_GRAYSCALE) # instead of grayscale you can also use -1, 0, or 1. -faceCascade = cv2.CascadeClassifier(r"./lbpcascade_frontalface.xml") # CHECK THIS FIRST TROUBLE SHOOTING -faceCascade_default = cv2.CascadeClassifier(r"./haarcascade_frontalface_default.xml") -faceCascade_alt = cv2.CascadeClassifier(r"./haarcascade_frontalface_alt.xml") -faceCascade_alt2 = cv2.CascadeClassifier(r"./haarcascade_frontalface_alt2.xml") -faceCascade_alttree = cv2.CascadeClassifier(r"./haarcascade_frontalface_alt_tree.xml") -profileFaceCascade = cv2.CascadeClassifier(r"./haarcascade_profileface.xml") +faceCascade = cv2.CascadeClassifier(r"./cascades/lbpcascade_frontalface.xml") # CHECK THIS FIRST TROUBLE SHOOTING +faceCascade_default = cv2.CascadeClassifier(r"./cascades/haarcascade_frontalface_default.xml") +faceCascade_alt = cv2.CascadeClassifier(r"./cascades/haarcascade_frontalface_alt.xml") +faceCascade_alt2 = cv2.CascadeClassifier(r"./cascades/haarcascade_frontalface_alt2.xml") +faceCascade_alttree = cv2.CascadeClassifier(r"./cascades/haarcascade_frontalface_alt_tree.xml") +profileFaceCascade = cv2.CascadeClassifier(r"./cascades/haarcascade_profileface.xml") + +datestamp = "{:%Y_%m_%d %H_%M_%S}".format(datetime.datetime.now()) +output_dir = r"./output/" + datestamp + r"/" + + +if args.training_data: + if not os.path.exists(output_dir): + os.makedirs(output_dir) + with open(output_dir + r"found_faces.csv", 'a') as fd: + fd.write(f"frame_name, x, y, width, height\n") tmp, frm = cap.read() height, width, channels = frm.shape @@ -129,15 +147,25 @@ while(True): minNeighbors=5, minSize=(30,30) ) + # Draw a rectangle around the faces for (x, y, w, h) in faces: + if args.training_data: + frame_name = frames_searched + with open(output_dir + r"found_faces.csv", 'a') as fd: + fd.write(f"frame_{frame_name}.jpg, {x}, {y}, {w}, {h}\n") + cv2.imwrite(output_dir + f"frame_{frame_name}.jpg", frame) + faces_found += 1 adjustment_required = get_adjustment_amount([width, height], x, y, w, h) cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 255, 255)) + if args.output: print(f"Adjust right: {adjustment_required[0]}".ljust(90, ' '), flush=True) print(f"Adjust up : {adjustment_required[1]}", flush=True) - cv2.imshow('frame', frame) + + if not args.no_screen: + cv2.imshow('frame', frame) if args.dashboard: draw_dashboard() diff --git a/haarcascade_frontalface_alt.xml b/cascades/haarcascade_frontalface_alt.xml similarity index 100% rename from haarcascade_frontalface_alt.xml rename to cascades/haarcascade_frontalface_alt.xml diff --git a/haarcascade_frontalface_alt2.xml b/cascades/haarcascade_frontalface_alt2.xml similarity index 100% rename from haarcascade_frontalface_alt2.xml rename to cascades/haarcascade_frontalface_alt2.xml diff --git a/haarcascade_frontalface_alt_tree.xml b/cascades/haarcascade_frontalface_alt_tree.xml similarity index 100% rename from haarcascade_frontalface_alt_tree.xml rename to cascades/haarcascade_frontalface_alt_tree.xml diff --git a/haarcascade_frontalface_default.xml b/cascades/haarcascade_frontalface_default.xml similarity index 100% rename from haarcascade_frontalface_default.xml rename to cascades/haarcascade_frontalface_default.xml diff --git a/haarcascade_profileface.xml b/cascades/haarcascade_profileface.xml similarity index 100% rename from haarcascade_profileface.xml rename to cascades/haarcascade_profileface.xml diff --git a/haarcascade_smile.xml b/cascades/haarcascade_smile.xml similarity index 100% rename from haarcascade_smile.xml rename to cascades/haarcascade_smile.xml diff --git a/lbpcascade_frontalface.xml b/cascades/lbpcascade_frontalface.xml similarity index 100% rename from lbpcascade_frontalface.xml rename to cascades/lbpcascade_frontalface.xml