cailean
2 months ago
commit
c23ab507e7
4 changed files with 147 additions and 0 deletions
@ -0,0 +1,2 @@ |
|||||
|
/images/ |
||||
|
embeddings.json |
@ -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() |
@ -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 |
Loading…
Reference in new issue