Commit 3fe71c84 authored by tihmels's avatar tihmels

Refactoring und Verbesserungen

parent 4328dc1b
......@@ -10,7 +10,7 @@ faceDet_four = cv2.CascadeClassifier('resources/Haarcascade/haarcascade_frontalf
def find_faces(image):
faces_coordinates = _locate_faces(image)
faces_coordinates = locate_faces(image)
cutted_faces = [image[y:y + h, x:x + w] for (x, y, w, h) in faces_coordinates]
normalized_faces = [_normalize_face(face) for face in cutted_faces]
return zip(normalized_faces, faces_coordinates)
......@@ -19,30 +19,30 @@ def find_faces(image):
def _normalize_face(face):
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
face = cv2.resize(face, (250, 250))
return face
def _locate_faces(image):
face_one = faceDet.detectMultiScale(image, scaleFactor=2, minNeighbors=4, minSize=(50, 50),
def locate_faces(image, scaleFactor=2, minNeighbors=4):
face_one = faceDet.detectMultiScale(image, scaleFactor, minNeighbors, minSize=(20, 20),
flags=cv2.CASCADE_SCALE_IMAGE)
if len(face_one) == 1:
return face_one
face_two = faceDet_two.detectMultiScale(image, scaleFactor=2, minNeighbors=4, minSize=(50, 50),
face_two = faceDet_two.detectMultiScale(image, scaleFactor, minNeighbors, minSize=(20, 20),
flags=cv2.CASCADE_SCALE_IMAGE)
if len(face_two) == 1:
return face_two
face_three = faceDet_three.detectMultiScale(image, scaleFactor=2, minNeighbors=4, minSize=(50, 50),
face_three = faceDet_three.detectMultiScale(image, scaleFactor, minNeighbors, minSize=(20, 20),
flags=cv2.CASCADE_SCALE_IMAGE)
if len(face_three) == 1:
return face_three
face_four = faceDet_four.detectMultiScale(image, scaleFactor=2, minNeighbors=4, minSize=(50, 50),
face_four = faceDet_four.detectMultiScale(image, scaleFactor, minNeighbors, minSize=(20, 20),
flags=cv2.CASCADE_SCALE_IMAGE)
if len(face_four) == 1:
......
......@@ -8,7 +8,7 @@ import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
filename='logs/process_model.log')
tags = sys.argv
args = sys.argv
logging.debug('Fisherface training initialized')
def get_files_from_emotion(emotion): # Define function to get file list, randomly shuffle it and split 80/20
......@@ -21,8 +21,7 @@ def get_files_from_emotion(emotion): # Define function to get file list, random
def image_preprocessing(item):
image = cv2.imread(item) # open image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # convert to grayscale
equalized = cv2.equalizeHist(gray)
return equalized
return gray
def make_sets():
training_data = []
......@@ -63,9 +62,9 @@ def run_recognizer():
cnt += 1
return ((100 * correct) / (correct + incorrect))
if len(tags) > 1:
message = ', '.join(tags[1:])
logging.debug(message.upper())
if len(args) > 1:
tags = ', '.join(args[1:])
logging.debug(tags.upper())
emotions = ["anger", "disgust", "happy", "neutral", "surprise"] # Emotion list
fishface = cv2.face.FisherFaceRecognizer_create()
......
import cv2
import glob
import logging
faceDet = cv2.CascadeClassifier('resources/Haarcascade/haarcascade_frontalface_default.xml')
faceDet_two = cv2.CascadeClassifier('resources/Haarcascade/haarcascade_frontalface_alt2.xml')
faceDet_three = cv2.CascadeClassifier('resources/Haarcascade/haarcascade_frontalface_alt.xml')
faceDet_four = cv2.CascadeClassifier('resources/Haarcascade/haarcascade_frontalface_alt_tree.xml')
from face_detect import locate_faces
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
......@@ -32,28 +28,10 @@ def detect_faces(emotion):
for f in files:
frame = cv2.imread(f) # Open image
# Detect face using 4 different classifiers
face = faceDet.detectMultiScale(frame, scaleFactor=1.05, minNeighbors=10, minSize=(10, 10),
flags=cv2.CASCADE_SCALE_IMAGE)
face_two = faceDet_two.detectMultiScale(frame, scaleFactor=1.05, minNeighbors=10, minSize=(10, 10),
flags=cv2.CASCADE_SCALE_IMAGE)
face_three = faceDet_three.detectMultiScale(frame, scaleFactor=1.05, minNeighbors=10, minSize=(10, 10),
flags=cv2.CASCADE_SCALE_IMAGE)
face_four = faceDet_four.detectMultiScale(frame, scaleFactor=1.05, minNeighbors=10, minSize=(10, 10),
flags=cv2.CASCADE_SCALE_IMAGE)
# Go over detected faces, stop at first detected face, return empty if no face.
if len(face) == 1:
facefeatures = face
elif len(face_two) == 1:
facefeatures = face_two
elif len(face_three) == 1:
facefeatures = face_three
elif len(face_four) == 1:
facefeatures = face_four
else:
facefeatures = locate_faces(frame, 1.05, 6)
if facefeatures is '':
undetected.append(f)
facefeatures = ''
# Cut and save face
for (x, y, w, h) in facefeatures: # get coordinates and size of rectangle containing face
......@@ -69,6 +47,7 @@ def detect_faces(emotion):
totalFiles += 1 # Increment image number
fileNumber += 1
logging.debug("end of set\n")
......
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