serengil / deepface

A Lightweight Face Recognition and Facial Attribute Analysis (Age, Gender, Emotion and Race) Library for Python
https://www.youtube.com/watch?v=WnUVYQP4h44&list=PLsS_1RYmYQQFdWqxQggXHynP1rqaYXv_E&index=1
MIT License
14.33k stars 2.2k forks source link

Calculating similarity of faces best approach #736

Closed FurkanGozukara closed 1 year ago

FurkanGozukara commented 1 year ago

I am using Stable Diffusion training to generate my own images

Generated over 5k and I want to utilize DeepFace to sort them by most similar to my trained original face image

If you wonder what I mean by this you can check out my below tutorials

Now I made this below script but I am not sure is that the best way to calculate similarity

Can I improve it?

Can I make it use my GPU RTX 3090 ?

1.jpg is my original training image and others are AI generated images

from deepface import DeepFace
import cv2
import os

img1path = "1.jpg"
imgsfolder = "D:/86 se courses youtube kanali/09 face compare/generated_imgs"
models = ['Facenet512'] # list of models to use

# load the first image
img1 = cv2.imread(img1path)

# calculate distance score for each image in the folder
distance_scores = []
count = 0
for filename in os.listdir(imgsfolder):
    if filename.endswith('.jpg') or filename.endswith('.png'):
        try:
            img_path = os.path.join(imgsfolder, filename)
            img2 = cv2.imread(img_path)
            distance_score = DeepFace.verify(img1, img2, model_name=models[0])["distance"]
            distance_scores.append((filename, distance_score))
            count += 1
            print(str(count) + " - "+str( distance_score))
        except Exception as e:
            print("Error occurred while processing "+filename+": "+str(e))

        if count == 5000:
            break

# sort the distance scores in ascending order
distance_scores.sort(key=lambda x: x[1])

# rename the images based on their distance score
for i, (filename, _) in enumerate(distance_scores):
    new_filename = f"{i+1}.png" # rename to 1.png, 2.png, 3.png, ...
    os.rename(os.path.join(imgsfolder, filename), os.path.join(imgsfolder, new_filename))

Here example images

org img

1

AI images

image

Also when I use detect image and save in the drive, it is changing direction of image which doesnt make sense

Here example

image

And here my Stable Diffusion tutorials not related to this question but I am planning a tutorial for DeepFace for this approach

1.) Automatic1111 Web UI - PC - Free How To Install Python, Setup Virtual Environment VENV, Set Default Python System Path & Install Git image

2.) Automatic1111 Web UI - PC - Free Easiest Way to Install & Run Stable Diffusion Web UI on PC by Using Open Source Automatic Installer image

3.) Automatic1111 Web UI - PC - Free How to use Stable Diffusion V2.1 and Different Models in the Web UI - SD 1.5 vs 2.1 vs Anything V3 image

4.) Automatic1111 Web UI - PC - Free Zero To Hero Stable Diffusion DreamBooth Tutorial By Using Automatic1111 Web UI - Ultra Detailed image

5.) Automatic1111 Web UI - PC - Free DreamBooth Got Buffed - 22 January Update - Much Better Success Train Stable Diffusion Models Web UI image

6.) Automatic1111 Web UI - PC - Free How to Inject Your Trained Subject e.g. Your Face Into Any Custom Stable Diffusion Model By Web UI image

7.) Automatic1111 Web UI - PC - Free How To Do Stable Diffusion LORA Training By Using Web UI On Different Models - Tested SD 1.5, SD 2.1 image

8.) Automatic1111 Web UI - PC - Free 8 GB LoRA Training - Fix CUDA & xformers For DreamBooth and Textual Inversion in Automatic1111 SD UI image

9.) Automatic1111 Web UI - PC - Free How To Do Stable Diffusion Textual Inversion (TI) / Text Embeddings By Automatic1111 Web UI Tutorial image

10.) Automatic1111 Web UI - PC - Free How To Generate Stunning Epic Text By Stable Diffusion AI - No Photoshop - For Free - Depth-To-Image image

11.) Python Code - Hugging Face Diffusers Script - PC - Free How to Run and Convert Stable Diffusion Diffusers (.bin Weights) & Dreambooth Models to CKPT File image

12.) NMKD Stable Diffusion GUI - Open Source - PC - Free Forget Photoshop - How To Transform Images With Text Prompts using InstructPix2Pix Model in NMKD GUI image

13.) Google Colab Free - Cloud - No PC Is Required Transform Your Selfie into a Stunning AI Avatar with Stable Diffusion - Better than Lensa for Free image

14.) Google Colab Free - Cloud - No PC Is Required Stable Diffusion Google Colab, Continue, Directory, Transfer, Clone, Custom Models, CKPT SafeTensors image

15.) Automatic1111 Web UI - PC - Free Become A Stable Diffusion Prompt Master By Using DAAM - Attention Heatmap For Each Used Token - Word image

16.) Python Script - Gradio Based - ControlNet - PC - Free Transform Your Sketches into Masterpieces with Stable Diffusion ControlNet AI - How To Use Tutorial image

17.) Automatic1111 Web UI - PC - Free Sketches into Epic Art with 1 Click: A Guide to Stable Diffusion ControlNet in Automatic1111 Web UI image

18.) RunPod - Automatic1111 Web UI - Cloud - Paid - No PC Is Required Ultimate RunPod Tutorial For Stable Diffusion - Automatic1111 - Data Transfers, Extensions, CivitAI image

19.) RunPod - Automatic1111 Web UI - Cloud - Paid - No PC Is Required RunPod Fix For DreamBooth & xFormers - How To Use Automatic1111 Web UI Stable Diffusion on RunPod image

20.) Automatic1111 Web UI - PC - Free Fantastic New ControlNet OpenPose Editor Extension & Image Mixing - Stable Diffusion Web UI Tutorial image

21.) Automatic1111 Web UI - PC - Free Automatic1111 Stable Diffusion DreamBooth Guide: Optimal Classification Images Count Comparison Test image

22.) Automatic1111 Web UI - PC - Free Epic Web UI DreamBooth Update - New Best Settings - 10 Stable Diffusion Training Compared on RunPods image

23.) Automatic1111 Web UI - PC - Free New Style Transfer Extension, ControlNet of Automatic1111 Stable Diffusion T2I-Adapter Color Control image

24.) Automatic1111 Web UI - PC - Free Generate Text Arts & Fantastic Logos By Using ControlNet Stable Diffusion Web UI For Free Tutorial image

25.) Automatic1111 Web UI - PC - Free How To Install New DREAMBOOTH & Torch 2 On Automatic1111 Web UI PC For Epic Performance Gains Guide image

26.) Automatic1111 Web UI - PC - Free Training Midjourney Level Style And Yourself Into The SD 1.5 Model via DreamBooth Stable Diffusion image

27.) Automatic1111 Web UI - PC - Free Video To Anime - Generate An EPIC Animation From Your Phone Recording By Using Stable Diffusion AI image

28.) Python Script - Jupyter Based - PC - Free Midjourney Level NEW Open Source Kandinsky 2.1 Beats Stable Diffusion - Installation And Usage Guide image

29.) Automatic1111 Web UI - PC - Free RTX 3090 vs RTX 3060 Ultimate Showdown for Stable Diffusion, ML, AI & Video Rendering Performance image

serengil commented 1 year ago
FurkanGozukara commented 1 year ago

I have installed torch and cuda what packages do i need to as tensorflow-gpu dependency can you give me direction?

which pip etc?