From c23ab507e7ef3a80e00447eeff19618d7139d16d Mon Sep 17 00:00:00 2001 From: cailean Date: Fri, 16 Aug 2024 12:19:03 +0100 Subject: [PATCH] init --- .gitignore | 2 ++ README.md | 0 app.py | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 61 +++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 app.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2e9b0d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/images/ +embeddings.json \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/app.py b/app.py new file mode 100644 index 0000000..f2636cc --- /dev/null +++ b/app.py @@ -0,0 +1,84 @@ +from deepface import DeepFace +import glob +import os +import json +import numpy as np + +folderPath = "images" +output_file = "embeddings.json" + +embeddings = [] + +backends = [ + 'opencv', + 'ssd', + 'dlib', + 'mtcnn', + 'fastmtcnn', + 'retinaface', + 'mediapipe', + 'yolov8', + 'yunet', + 'centerface', +] + +def main(): + image_paths = setup() + run(image_paths) + saveToJson() + +def processImage(image, idx): + try: + + anaylse_obj = DeepFace.analyze( + img_path=image, + actions=['emotion'], + enforce_detection=False, + detector_backend=backends[0] + ) + + emotions = anaylse_obj[0]['emotion'] + + raw_emotions = [value for value in emotions.values()] + + normalised_emotions = l2_normalize(np.array(raw_emotions)) + + normalised_emotions = normalised_emotions.tolist() + + entry = { + "index": idx, + "filename": os.path.basename(image), # Extract the filename from the path + "vector": normalised_emotions + } + + # Add the entry to the JSON data list + embeddings.append(entry) + except: + print("Could not locate face in image. Skipping.") + +def run(image_paths): + for idx, image in enumerate(image_paths): + processImage(image, idx) + +def setup(): + # List of all images in the specified folder + image_files = glob.glob(os.path.join(folderPath, '*')) + + # Optional: Filter by specific image extensions (e.g., .jpg, .png) + image_paths = [img for img in image_files if img.endswith(('.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.gif'))] + + return image_paths + +def l2_normalize(vector): + norm = np.linalg.norm(vector) + if norm == 0: + return vector + return vector / norm + +def saveToJson(): + with open(output_file, 'w') as f: + json.dump(embeddings, f, indent=4) + print("Successfully created JSON file.") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8f28a10 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,61 @@ +absl-py==2.1.0 +astunparse==1.6.3 +beautifulsoup4==4.12.3 +blinker==1.8.2 +certifi==2024.7.4 +charset-normalizer==3.3.2 +click==8.1.7 +deepface==0.0.92 +filelock==3.15.4 +fire==0.6.0 +Flask==3.0.3 +flatbuffers==24.3.25 +gast==0.6.0 +gdown==5.2.0 +google-pasta==0.2.0 +grpcio==1.65.4 +gunicorn==23.0.0 +h5py==3.11.0 +idna==3.7 +importlib_metadata==8.2.0 +itsdangerous==2.2.0 +Jinja2==3.1.4 +keras==3.5.0 +libclang==18.1.1 +Markdown==3.6 +markdown-it-py==3.0.0 +MarkupSafe==2.1.5 +mdurl==0.1.2 +ml-dtypes==0.4.0 +mtcnn==0.1.1 +namex==0.0.8 +numpy==1.26.4 +opencv-python==4.10.0.84 +opt-einsum==3.3.0 +optree==0.12.1 +packaging==24.1 +pandas==2.2.2 +pillow==10.4.0 +protobuf==4.25.4 +Pygments==2.18.0 +PySocks==1.7.1 +python-dateutil==2.9.0.post0 +pytz==2024.1 +requests==2.32.3 +retina-face==0.0.17 +rich==13.7.1 +six==1.16.0 +soupsieve==2.6 +tensorboard==2.17.1 +tensorboard-data-server==0.7.2 +tensorflow==2.17.0 +tensorflow-io-gcs-filesystem==0.37.1 +termcolor==2.4.0 +tf_keras==2.17.0 +tqdm==4.66.5 +typing_extensions==4.12.2 +tzdata==2024.1 +urllib3==2.2.2 +Werkzeug==3.0.3 +wrapt==1.16.0 +zipp==3.20.0