moved cascades to subfolder; added training data output

This commit is contained in:
Nickiel12 2023-09-29 16:30:33 -07:00
parent 0c46b9a190
commit f345468cbf
8 changed files with 35 additions and 7 deletions

View file

@ -2,6 +2,8 @@ import cv2
import numpy as np import numpy as np
import argparse import argparse
import sys import sys
import time
import os
import datetime import datetime
def init_argparse() -> argparse.ArgumentParser: def init_argparse() -> argparse.ArgumentParser:
@ -22,6 +24,12 @@ def init_argparse() -> argparse.ArgumentParser:
parser.add_argument( parser.add_argument(
"-f", "--file", nargs="?", help="File to scan instead of using the camera. Useful for generating training data" "-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 return parser
multiplication_factor = 0.05 multiplication_factor = 0.05
@ -65,12 +73,22 @@ parser = init_argparse()
args = parser.parse_args() args = parser.parse_args()
cap = cv2.VideoCapture(0, cv2.IMREAD_GRAYSCALE) # instead of grayscale you can also use -1, 0, or 1. 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 = cv2.CascadeClassifier(r"./cascades/lbpcascade_frontalface.xml") # CHECK THIS FIRST TROUBLE SHOOTING
faceCascade_default = cv2.CascadeClassifier(r"./haarcascade_frontalface_default.xml") faceCascade_default = cv2.CascadeClassifier(r"./cascades/haarcascade_frontalface_default.xml")
faceCascade_alt = cv2.CascadeClassifier(r"./haarcascade_frontalface_alt.xml") faceCascade_alt = cv2.CascadeClassifier(r"./cascades/haarcascade_frontalface_alt.xml")
faceCascade_alt2 = cv2.CascadeClassifier(r"./haarcascade_frontalface_alt2.xml") faceCascade_alt2 = cv2.CascadeClassifier(r"./cascades/haarcascade_frontalface_alt2.xml")
faceCascade_alttree = cv2.CascadeClassifier(r"./haarcascade_frontalface_alt_tree.xml") faceCascade_alttree = cv2.CascadeClassifier(r"./cascades/haarcascade_frontalface_alt_tree.xml")
profileFaceCascade = cv2.CascadeClassifier(r"./haarcascade_profileface.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() tmp, frm = cap.read()
height, width, channels = frm.shape height, width, channels = frm.shape
@ -129,14 +147,24 @@ while(True):
minNeighbors=5, minNeighbors=5,
minSize=(30,30) minSize=(30,30)
) )
# Draw a rectangle around the faces # Draw a rectangle around the faces
for (x, y, w, h) in 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 faces_found += 1
adjustment_required = get_adjustment_amount([width, height], x, y, w, h) adjustment_required = get_adjustment_amount([width, height], x, y, w, h)
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 255, 255)) cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 255, 255))
if args.output: if args.output:
print(f"Adjust right: {adjustment_required[0]}".ljust(90, ' '), flush=True) print(f"Adjust right: {adjustment_required[0]}".ljust(90, ' '), flush=True)
print(f"Adjust up : {adjustment_required[1]}", flush=True) print(f"Adjust up : {adjustment_required[1]}", flush=True)
if not args.no_screen:
cv2.imshow('frame', frame) cv2.imshow('frame', frame)
if args.dashboard: if args.dashboard: