Commit bf1a7679 authored by tihmels's avatar tihmels

Projekt abgabebereit gemacht

parent 9f491647
# Created by https://www.gitignore.io/api/macos,python,windows # Created by https://www.gitignore.io/api/macos,python,windows
### macOS ### ### macOS ###
...@@ -156,9 +155,15 @@ $RECYCLE.BIN/ ...@@ -156,9 +155,15 @@ $RECYCLE.BIN/
*.msi *.msi
*.msm *.msm
*.msp *.msp
.idea/**
# Windows shortcuts # Windows shortcuts
*.lnk *.lnk
# End of https://www.gitignore.io/api/macos,python,windows # End of https://www.gitignore.io/api/macos,python,windows
.idea/
projectmood/logs/
projectmood/resources/csv/
projectmood/resources/img_data/dataset/
projectmood/resources/img_data/sorted_set/
projectmood/resources/img_data/source_emotion/
projectmood/resources/img_data/source_images/
...@@ -34,3 +34,25 @@ Setup ...@@ -34,3 +34,25 @@ Setup
5. Dependencies aus requirements.txt installieren:: 5. Dependencies aus requirements.txt installieren::
pip3 install -r requirements.txt pip3 install -r requirements.txt
***************
Model Injection
***************
Um das Hauptprogramm ``webcam.py`` auszuführen,
muss ein konkretes Model übergeben werden und die darin beschriebenen Emotionen als Parameter definiert sein.
Im Ordner ``projectmood/resources/models/`` befinden sich zwei verschiedene Models: ``five_emotions.xml`` sowie ``three_emotions.xml``
Die drei Emotionen behandeln *neutral*, *happy* und *sadness*.
Der Fünf-Klassen-Classifier außerdem noch *anger* und *sadness*.
Konsolenbefehl um die beiden Classifier zu injizieren und das Programm zu starten:
1. ``python3 webcam.py -013 -m resources/models/three_emotions.xml -r 250``
2. ``python3 webcam.py -01236 -m resources/models/five_emotions -r 250``
Für weitere Informationen ``python3 webcam.py -h``
\ No newline at end of file
...@@ -2,7 +2,6 @@ from threading import Thread ...@@ -2,7 +2,6 @@ from threading import Thread
import cv2 import cv2
class WebcamVideoStream: class WebcamVideoStream:
def __init__(self, src=0): def __init__(self, src=0):
......
# ***************************************************
# * Copyright (C) 2010-2011 Tjado Ihmels <tjado.ihmels@uni-oldenburg.de>
# *
# * This file is part of whiteboard-project-matcher.
# *
# * whiteboard-project-matcher can not be copied and/or distributed without the express
# * permission of Tjado Ihmels
# ***************************************************/
import argparse import argparse
import logging import logging
import os import os
...@@ -29,7 +38,7 @@ logging.debug(arguments) ...@@ -29,7 +38,7 @@ logging.debug(arguments)
emotiondict = {0: 'neutral', 1: 'happy', 2: 'sadness', 3: 'surprise', 4: 'fear', 5: 'disgust', 6: 'anger', 7: 'contempt', emotiondict = {0: 'neutral', 1: 'happy', 2: 'sadness', 3: 'surprise', 4: 'fear', 5: 'disgust', 6: 'anger', 7: 'contempt',
8: 'none', 9: 'uncertain', 10: 'noface'} 8: 'none', 9: 'uncertain', 10: 'noface'}
# count emotions # count found emotions
emotioncounter = {} emotioncounter = {}
for emotion in arguments.emotions: for emotion in arguments.emotions:
emotioncounter[emotion] = 0 emotioncounter[emotion] = 0
......
# ***************************************************
# * Copyright © 2010-2011 Tjado Ihmels <tjado.ihmels@uni-oldenburg.de>
# *
# * This file is part of whiteboard-project-matcher.
# *
# * whiteboard-project-matcher can not be copied and/or distributed without the express
# * permission of Tjado Ihmels
# *
# * Parts of this program are legally copied and adapted from
# * van Gent, P. (2016). Emotion Recognition With Python, OpenCV and a Face Dataset. A tech blog about fun things with Python and embedded electronics. Retrieved from:
# * http://www.paulvangent.com/2016/04/01/emotion-recognition-with-python-opencv-and-a-face-dataset/
# ***************************************************/
""" """
This script maps the source_emotions to the source_images and creates a sorted set This script maps the source_emotions to the source_images and creates a sorted set
""" """
......
# ***************************************************
# * Copyright (C) 2010-2011 Tjado Ihmels <tjado.ihmels@uni-oldenburg.de>
# *
# * This file is part of whiteboard-project-matcher.
# *
# * whiteboard-project-matcher can not be copied and/or distributed without the express
# * permission of Tjado Ihmels
# ***************************************************/
import os import os
import smtplib import smtplib
from email import encoders from email import encoders
...@@ -8,7 +19,7 @@ from email.mime.text import MIMEText ...@@ -8,7 +19,7 @@ from email.mime.text import MIMEText
def sendMail(subject, to='tjado.ihmels@uni-oldenburg.de', body='', filepath=''): def sendMail(subject, to='tjado.ihmels@uni-oldenburg.de', body='', filepath=''):
""" """
For a longer runtime on a remote server emails serve as feedback For a longer runtime on a remote server, emails serve as feedback
:param subject: subject :param subject: subject
:param to: email adress :param to: email adress
:param body: email text :param body: email text
......
# ***************************************************
# * Copyright (C) 2010-2011 Tjado Ihmels <tjado.ihmels@uni-oldenburg.de>
# *
# * This file is part of whiteboard-project-matcher.
# *
# * whiteboard-project-matcher can not be copied and/or distributed without the express
# * permission of Tjado Ihmels
# *
# * Parts of this program are legally copied and adapted from
# * van Gent, P. (2016). Emotion Recognition With Python, OpenCV and a Face Dataset. A tech blog about fun things with Python and embedded electronics. Retrieved from:
# * http://www.paulvangent.com/2016/04/01/emotion-recognition-with-python-opencv-and-a-face-dataset/
# ***************************************************/
import cv2 import cv2
# Haarcascades # Haarcascades
......
""" # ***************************************************
Utility Class for working with images in OpenCV # * Copyright (C) 2010-2011 Tjado Ihmels <tjado.ihmels@uni-oldenburg.de>
""" # *
# * This file is part of whiteboard-project-matcher.
# *
# * whiteboard-project-matcher can not be copied and/or distributed without the express
# * permission of Tjado Ihmels
# *
# * Parts of this program are legally copied and adapted from
# * van Gent, P. (2016). Emotion Recognition With Python, OpenCV and a Face Dataset. A tech blog about fun things with Python and embedded electronics. Retrieved from:
# * http://www.paulvangent.com/2016/04/01/emotion-recognition-with-python-opencv-and-a-face-dataset/
# ***************************************************/
import cv2 import cv2
import numpy as np import numpy as np
from PIL import Image from PIL import Image
# Utility Class for working with images in OpenCV
def image_as_nparray(image): def image_as_nparray(image):
""" """
...@@ -35,9 +45,9 @@ def load_image(source_path): ...@@ -35,9 +45,9 @@ def load_image(source_path):
return cv2.cvtColor(source_image, cv2.COLOR_BGR2GRAY) return cv2.cvtColor(source_image, cv2.COLOR_BGR2GRAY)
def draw_with_alpha(source_image, image_to_draw, coordinates): def draw_alpha_3dim(source_image, image_to_draw, coordinates):
""" """
Draw a partially transparent image on another image Draws a partially transparent, three dimensional image on another image
:param source_image: Background image :param source_image: Background image
:param image_to_draw: Overdraw image :param image_to_draw: Overdraw image
:param coordinates: : Coordinates to which the image should be drawn :param coordinates: : Coordinates to which the image should be drawn
...@@ -48,9 +58,9 @@ def draw_with_alpha(source_image, image_to_draw, coordinates): ...@@ -48,9 +58,9 @@ def draw_with_alpha(source_image, image_to_draw, coordinates):
for c in range(0, 3): for c in range(0, 3):
source_image[y:y + h, x:x + w, c] = image_array[:, :, c] * (image_array[:, :, 3] / 255.0) + source_image[y:y + h, x:x + w, c] * (1.0 - image_array[:, :, 3] / 255.0) source_image[y:y + h, x:x + w, c] = image_array[:, :, c] * (image_array[:, :, 3] / 255.0) + source_image[y:y + h, x:x + w, c] * (1.0 - image_array[:, :, 3] / 255.0)
def draw_img(source_image, image_to_draw, coordinates): def draw_2dim(source_image, image_to_draw, coordinates):
""" """
Draw an image to another image Draws an two dimensional image to another image
:param source_image: Background image :param source_image: Background image
:param image_to_draw: Overdraw image :param image_to_draw: Overdraw image
:param coordinates: : Coordinates to which the image should be drawn :param coordinates: : Coordinates to which the image should be drawn
......
# This class makes the training of the model possible # ***************************************************
# * Copyright © 2010-2011 Tjado Ihmels <tjado.ihmels@uni-oldenburg.de>
# *
# * This file is part of whiteboard-project-matcher.
# *
# * whiteboard-project-matcher can not be copied and/or distributed without the express
# * permission of Tjado Ihmels
# *
# * Parts of this program are legally copied and adapted from
# * van Gent, P. (2016). Emotion Recognition With Python, OpenCV and a Face Dataset. A tech blog about fun things with Python and embedded electronics. Retrieved from:
# * http://www.paulvangent.com/2016/04/01/emotion-recognition-with-python-opencv-and-a-face-dataset/
# ***************************************************/
import argparse import argparse
import glob import glob
...@@ -10,6 +21,8 @@ import sys ...@@ -10,6 +21,8 @@ import sys
from email_service import sendMail from email_service import sendMail
# This program takes the pictures from the dataset and generates a trained model
logfile = 'logs/process_model.log' logfile = 'logs/process_model.log'
# Creates and configures the logger # Creates and configures the logger
...@@ -28,6 +41,7 @@ parser.add_argument('-5', action='append_const', dest='emotions', const='disgust ...@@ -28,6 +41,7 @@ parser.add_argument('-5', action='append_const', dest='emotions', const='disgust
parser.add_argument('-6', action='append_const', dest='emotions', const='anger', help='anger') parser.add_argument('-6', action='append_const', dest='emotions', const='anger', help='anger')
parser.add_argument('-d', '--dataset', action='store', dest='dataset', default='resources/img_data/dataset/', parser.add_argument('-d', '--dataset', action='store', dest='dataset', default='resources/img_data/dataset/',
help='path to dataset') help='path to dataset')
parser.add_argument('-r', '--ratio', action='store', dest='ratio', help='the relative size of the training set in float [0.1,1]', default=1, type=float)
parser.add_argument('-i' '--iterations', action='store', dest='iterations', type=int, default=1, parser.add_argument('-i' '--iterations', action='store', dest='iterations', type=int, default=1,
help='number of iterations') help='number of iterations')
parser.add_argument('-p', '--properties', nargs='*', dest='properties', help='pre-processing steps for logging') parser.add_argument('-p', '--properties', nargs='*', dest='properties', help='pre-processing steps for logging')
...@@ -52,9 +66,9 @@ def _get_faces_from_emotion(emotion): ...@@ -52,9 +66,9 @@ def _get_faces_from_emotion(emotion):
files = glob.glob(arguments.dataset + '{}/*'.format(emotion)) files = glob.glob(arguments.dataset + '{}/*'.format(emotion))
random.shuffle(files) random.shuffle(files)
# Mix the dataset in training and comparison images in the ratio 80 to 20 # Mix the dataset in training and comparison images in the given ratio
training = files[:int(len(files) * 1)] training = files[:int(len(files) * arguments.ratio)]
prediction = files[-int(len(files) * 0):] prediction = files[-int(len(files) * (1 - arguments.ratio)):]
return training, prediction return training, prediction
......
This source diff could not be displayed because it is too large. You can view the blob instead.
# ***************************************************
# * Copyright © 2010-2011 Tjado Ihmels <tjado.ihmels@uni-oldenburg.de>
# *
# * This file is part of whiteboard-project-matcher.
# *
# * whiteboard-project-matcher can not be copied and/or distributed without the express
# * permission of Tjado Ihmels
# *
# * Parts of this program are legally copied and adapted from
# * van Gent, P. (2016). Emotion Recognition With Python, OpenCV and a Face Dataset. A tech blog about fun things with Python and embedded electronics. Retrieved from:
# * http://www.paulvangent.com/2016/04/01/emotion-recognition-with-python-opencv-and-a-face-dataset/
# ***************************************************/
import argparse import argparse
import glob import glob
import logging import logging
......
# ***************************************************
# * Copyright © 2010-2011 Tjado Ihmels <tjado.ihmels@uni-oldenburg.de>
# *
# * This file is part of whiteboard-project-matcher.
# *
# * whiteboard-project-matcher can not be copied and/or distributed without the express
# * permission of Tjado Ihmels
# *
# * Parts of this program are legally copied and adapted from
# * van Gent, P. (2016). Emotion Recognition With Python, OpenCV and a Face Dataset. A tech blog about fun things with Python and embedded electronics. Retrieved from:
# * http://www.paulvangent.com/2016/04/01/emotion-recognition-with-python-opencv-and-a-face-dataset/
# ***************************************************/
import argparse import argparse
import os
import cv2 import cv2
import cv2.face import cv2.face
...@@ -19,7 +31,7 @@ parser.add_argument('-4', action='append_const', dest='emotions', const='fear', ...@@ -19,7 +31,7 @@ parser.add_argument('-4', action='append_const', dest='emotions', const='fear',
parser.add_argument('-5', action='append_const', dest='emotions', const='disgust', help='disgust') parser.add_argument('-5', action='append_const', dest='emotions', const='disgust', help='disgust')
parser.add_argument('-6', action='append_const', dest='emotions', const='anger', help='anger') parser.add_argument('-6', action='append_const', dest='emotions', const='anger', help='anger')
parser.add_argument('-b', '--buffer', action='store', dest='buffer', default=12, type=int, help='size of ringbuffer') parser.add_argument('-b', '--buffer', action='store', dest='buffer', default=12, type=int, help='size of ringbuffer')
parser.add_argument('-m', '--model', action='store', dest='model', default='resources/models/detection_model.xml', parser.add_argument('-m', '--model', action='store', dest='model', default='resources/models/five_emotions.xml',
help='path to model') help='path to model')
parser.add_argument('-r', '--resize', action='store', type=int, dest='resizefactor') parser.add_argument('-r', '--resize', action='store', type=int, dest='resizefactor')
arguments = parser.parse_args() arguments = parser.parse_args()
...@@ -34,9 +46,10 @@ def _load_emoticons(emotions): ...@@ -34,9 +46,10 @@ def _load_emoticons(emotions):
return [nparray_as_image(cv2.imread('resources/emojis/{}.png'.format(emotion), -1), mode=None) for emotion in return [nparray_as_image(cv2.imread('resources/emojis/{}.png'.format(emotion), -1), mode=None) for emotion in
emotions] emotions]
def show_webcam_and_run(model, emoticons, window_size=(600, 600), window_name=parser.description, update_time=1): def show_webcam_and_run(model, emoticons, window_size=(600, 600), window_name=parser.description, update_time=1):
""" """
Shows a webcam image, recognizes faces and emotions in real time and draws emoticons next to the faces. Shows a webcam input, recognizes faces and emotions in real time and draws emoticons next to the faces.
: param model: Trained Model : param model: Trained Model
: param emoticons: list of emoticons. : param emoticons: list of emoticons.
: param window_size: Size of the webcam window. : param window_size: Size of the webcam window.
...@@ -75,7 +88,7 @@ def show_webcam_and_run(model, emoticons, window_size=(600, 600), window_name=pa ...@@ -75,7 +88,7 @@ def show_webcam_and_run(model, emoticons, window_size=(600, 600), window_name=pa
processed_image = nparray_as_image(normalized_face[:, :], mode='L') processed_image = nparray_as_image(normalized_face[:, :], mode='L')
w, h = vc.size() w, h = vc.size()
# And print it to the frame # And print it to the frame
draw_img(frame, processed_image, (w-300, h-300, 250, 250)) draw_img(frame, processed_image, (w - 300, h - 300, 250, 250))
# No entry in the ring buffer is None # No entry in the ring buffer is None
if not (any(x is None for x in predictions)): if not (any(x is None for x in predictions)):
......
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