├── Readme.md ├── analyze_face_emotions.json ├── img ├── 1.jpg ├── 2.jpg └── 3.jpg ├── libraries.bat └── main.py /Readme.md: -------------------------------------------------------------------------------- 1 | # Recognition of emotions from a photo 2 | In this project I will analyze how you can find out age, gender, race and emotion from a photo. 3 | 4 | ## How to install 5 | 1. Clone this repository on your computer 6 | `https://github.com/paveldat/emotion_recognition.git` 7 | 2. Install all the requirements 8 | `run libraries.bat` 9 | 3. Run the program 10 | `python main.py` 11 | 4. Write path to the image you want to analyze and press ENTER 12 | 13 | ## Result 14 | 15 | 16 | ``` 17 | Age: 29 18 | Gender: Woman 19 | Race: black 20 | Emotions: happy 21 | ``` 22 | 23 | More information you can find in "analyze_face_emotions.json" 24 | ``` 25 | { 26 | "age": 29, 27 | "region": { 28 | "x": 159, 29 | "y": 214, 30 | "w": 655, 31 | "h": 655 32 | }, 33 | "gender": "Woman", 34 | "race": { 35 | "asian": 21.657051146030426, 36 | "indian": 15.542873740196228, 37 | "black": 40.841639041900635, 38 | "white": 2.237129956483841, 39 | "middle eastern": 1.8103785812854767, 40 | "latino hispanic": 17.910930514335632 41 | }, 42 | "dominant_race": "black", 43 | "emotion": { 44 | "angry": 0.005997404163197863, 45 | "disgust": 9.302506100040701e-07, 46 | "fear": 3.2217927161702256, 47 | "happy": 91.00857484238448, 48 | "sad": 0.06716915304059946, 49 | "surprise": 0.6175690991349102, 50 | "neutral": 5.078888079060545 51 | }, 52 | "dominant_emotion": "happy" 53 | } 54 | ``` -------------------------------------------------------------------------------- /analyze_face_emotions.json: -------------------------------------------------------------------------------- 1 | { 2 | "age": 29, 3 | "region": { 4 | "x": 159, 5 | "y": 214, 6 | "w": 655, 7 | "h": 655 8 | }, 9 | "gender": "Woman", 10 | "race": { 11 | "asian": 21.657051146030426, 12 | "indian": 15.542873740196228, 13 | "black": 40.841639041900635, 14 | "white": 2.237129956483841, 15 | "middle eastern": 1.8103785812854767, 16 | "latino hispanic": 17.910930514335632 17 | }, 18 | "dominant_race": "black", 19 | "emotion": { 20 | "angry": 0.005997404163197863, 21 | "disgust": 9.302506100040701e-07, 22 | "fear": 3.2217927161702256, 23 | "happy": 91.00857484238448, 24 | "sad": 0.06716915304059946, 25 | "surprise": 0.6175690991349102, 26 | "neutral": 5.078888079060545 27 | }, 28 | "dominant_emotion": "happy" 29 | } -------------------------------------------------------------------------------- /img/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paveldat/emotion_recognition/176646637b98b4aab5c41b3f3aff16ba815db908/img/1.jpg -------------------------------------------------------------------------------- /img/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paveldat/emotion_recognition/176646637b98b4aab5c41b3f3aff16ba815db908/img/2.jpg -------------------------------------------------------------------------------- /img/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paveldat/emotion_recognition/176646637b98b4aab5c41b3f3aff16ba815db908/img/3.jpg -------------------------------------------------------------------------------- /libraries.bat: -------------------------------------------------------------------------------- 1 | pip install deepface -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from deepface import DeepFace 2 | import json 3 | 4 | def analyze_face_emotions(path_to_image): 5 | try: 6 | result = DeepFace.analyze(img_path=path_to_image, actions=['age', 'gender', 'race', 'dominant_race', 'dominant_emotion', 'emotion']) 7 | 8 | print(f'Age: {result.get("age")}') 9 | print(f'Gender: {result.get("gender")}') 10 | print(f'Race: {result.get("dominant_race")}') 11 | print(f'Emotions: {result.get("dominant_emotion")}') 12 | 13 | # write result in .json file 14 | with open('analyze_face_emotions.json', 'w') as file: 15 | json.dump(result, file, indent=4, ensure_ascii=False) 16 | 17 | except Exception as ex: 18 | return ex 19 | 20 | def main(): 21 | print("Please, type the name of the file. Or press enter to skip. Default name is img/1.jpg\n") 22 | path_to_image = input() 23 | if not path_to_image: 24 | path_to_image = 'img/1.jpg' 25 | 26 | analyze_face_emotions(path_to_image) 27 | 28 | if __name__ == '__main__': 29 | main() --------------------------------------------------------------------------------