sztuczna-empatia-kaczuszka/lab8/zad8_1emotions.ipynb

1.0 MiB
Raw Blame History

!pip install fer
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Collecting fer
  Downloading fer-22.5.0-py3-none-any.whl (1.5 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.5/1.5 MB 17.9 MB/s eta 0:00:00
[?25hRequirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (from fer) (3.7.1)
Requirement already satisfied: opencv-contrib-python in /usr/local/lib/python3.10/dist-packages (from fer) (4.7.0.72)
Requirement already satisfied: keras>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from fer) (2.12.0)
Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from fer) (1.5.3)
Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from fer) (2.27.1)
Collecting facenet-pytorch (from fer)
  Downloading facenet_pytorch-2.5.3-py3-none-any.whl (1.9 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.9/1.9 MB 68.9 MB/s eta 0:00:00
[?25hRequirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from fer) (4.65.0)
Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from facenet-pytorch->fer) (1.22.4)
Requirement already satisfied: torchvision in /usr/local/lib/python3.10/dist-packages (from facenet-pytorch->fer) (0.15.1+cu118)
Requirement already satisfied: pillow in /usr/local/lib/python3.10/dist-packages (from facenet-pytorch->fer) (8.4.0)
Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->fer) (1.0.7)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib->fer) (0.11.0)
Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->fer) (4.39.3)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->fer) (1.4.4)
Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->fer) (23.1)
Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->fer) (3.0.9)
Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib->fer) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas->fer) (2022.7.1)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->fer) (1.26.15)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->fer) (2022.12.7)
Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests->fer) (2.0.12)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->fer) (3.4)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib->fer) (1.16.0)
Requirement already satisfied: torch==2.0.0 in /usr/local/lib/python3.10/dist-packages (from torchvision->facenet-pytorch->fer) (2.0.0+cu118)
Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from torch==2.0.0->torchvision->facenet-pytorch->fer) (3.12.0)
Requirement already satisfied: typing-extensions in /usr/local/lib/python3.10/dist-packages (from torch==2.0.0->torchvision->facenet-pytorch->fer) (4.5.0)
Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch==2.0.0->torchvision->facenet-pytorch->fer) (1.11.1)
Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch==2.0.0->torchvision->facenet-pytorch->fer) (3.1)
Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch==2.0.0->torchvision->facenet-pytorch->fer) (3.1.2)
Requirement already satisfied: triton==2.0.0 in /usr/local/lib/python3.10/dist-packages (from torch==2.0.0->torchvision->facenet-pytorch->fer) (2.0.0)
Requirement already satisfied: cmake in /usr/local/lib/python3.10/dist-packages (from triton==2.0.0->torch==2.0.0->torchvision->facenet-pytorch->fer) (3.25.2)
Requirement already satisfied: lit in /usr/local/lib/python3.10/dist-packages (from triton==2.0.0->torch==2.0.0->torchvision->facenet-pytorch->fer) (16.0.3)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch==2.0.0->torchvision->facenet-pytorch->fer) (2.1.2)
Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->torch==2.0.0->torchvision->facenet-pytorch->fer) (1.3.0)
Installing collected packages: facenet-pytorch, fer
Successfully installed facenet-pytorch-2.5.3 fer-22.5.0
# Przykładowy kod do rozpoznawania emocji za pomocą biblioteki fer, OpenCV i TensorFlow

import cv2
import numpy as np
import matplotlib.pyplot as plt
from fer import FER

# Wczytanie obrazu
def get_emotions(image_path):
    image_path
    image = cv2.imread(image_path)

    # Inicjalizacja detektora emocji
    detector = FER()

    # Wykrycie twarzy na obrazie
    faces = detector.detect_emotions(image)

    # Wyświetlenie wyników
    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    plt.axis("off")

    for face in faces:
        x, y, w, h = face["box"]
        emotions = face["emotions"]

        # Wyświetlenie wykrytych emocji
        for emotion, score in emotions.items():
            text = f"{emotion}: {score:.2f}"
            plt.text(x, y, text, backgroundcolor="white")
            y += 20

    plt.show()
get_emotions("/content/happy.png")
get_emotions("/content/angry.png")
get_emotions("/content/scared.png")
get_emotions("/content/sad.png")
get_emotions("/content/neutral.png")
get_emotions("/content/suprise.png")