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