Commit 5755de74 authored by tihmels's avatar tihmels

SortedSet mal ein bisschen krass gepimpt um Ergebnisse zu validieren. Output...

SortedSet mal ein bisschen krass gepimpt um Ergebnisse zu validieren. Output wird jetzt in Logfile gespeichert
parent ea20e14b
import cv2
import glob
import time
import logging
faceDet = cv2.CascadeClassifier('Haarcascade/haarcascade_frontalface_default.xml')
faceDet_two = cv2.CascadeClassifier('Haarcascade/haarcascade_frontalface_alt2.xml')
faceDet_three = cv2.CascadeClassifier('Haarcascade/haarcascade_frontalface_alt.xml')
faceDet_four = cv2.CascadeClassifier('Haarcascade/haarcascade_frontalface_alt_tree.xml')
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
filename='logs/sorted_set_facedetector.log',
filemode='w')
emotions = ["neutral", "anger", "disgust", "happy", "surprise"] # Define emotions
totalFiles:int = 0
totalFaces:int = 0
undetected:list = []
def detect_faces(emotion):
files = glob.glob('Basis_data/sorted_set/%s/*w' % emotion) # Get list of all images with emotion
files = glob.glob('Basis_data/sorted_set/%s/*' % emotion) # Get list of all images with emotion
global undetected
global totalFaces
global totalFiles
filenumber = 0
logging.debug("%d files in folder %s" % (len(files), emotion))
fileNumber = 0
for f in files:
frame = cv2.imread(f) # Open image
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Convert image to grayscale
scaleFactor = 1.05
minNeighbors = 10
minSize = (20, 20)
# Detect face using 4 different classifiers
face = faceDet.detectMultiScale(gray, scaleFactor, minNeighbors, minSize,
face = faceDet.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=6, minSize=(10, 10),
flags=cv2.CASCADE_SCALE_IMAGE)
face_two = faceDet_two.detectMultiScale(gray, scaleFactor, minNeighbors, minSize,
face_two = faceDet_two.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=6, minSize=(10, 10),
flags=cv2.CASCADE_SCALE_IMAGE)
face_three = faceDet_three.detectMultiScale(gray, scaleFactor, minNeighbors, minSize,
face_three = faceDet_three.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=6, minSize=(10, 10),
flags=cv2.CASCADE_SCALE_IMAGE)
face_four = faceDet_four.detectMultiScale(gray, scaleFactor, minNeighbors, minSize,
face_four = faceDet_four.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=6, minSize=(10, 10),
flags=cv2.CASCADE_SCALE_IMAGE)
# Go over detected faces, stop at first detected face, return empty if no face.
......@@ -41,20 +53,30 @@ def detect_faces(emotion):
elif len(face_four) == 1:
facefeatures = face_four
else:
facefeatures = ""
undetected.append(f)
# Cut and save face
for (x, y, w, h) in facefeatures: # get coordinates and size of rectangle containing face
print("face found in file: %s" % f)
logging.debug("face found in file: %s" % f)
totalFaces += 1
gray = gray[y:y + h, x:x + w] # Cut the frame to size
try:
out = cv2.resize(gray, (250, 250)) # Resize face so all images have same size
cv2.imwrite('Basis_data/dataset/%s/%s.jpg' % (emotion, filenumber), out) # Write image
cv2.imwrite('Basis_data/dataset/%s/%s.jpg' % (emotion, fileNumber), out) # Write image
except:
pass # If error, pass file
filenumber += 1 # Increment image number
totalFiles += 1 # Increment image number
fileNumber += 1
for emotion in emotions:
detect_faces(emotion) # Call functiona
detect_faces(emotion) # Call functional
logging.debug("\n")
logging.debug(str(totalFaces) + " Gesichter in " + str(totalFiles) + " Dateien gefunden.\n")
logging.debug("Kein Gesicht wurde gefunden in:")
for f in undetected:
logging.debug(f)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment