Winston de Jong
commited on
Commit
·
ec30251
1
Parent(s):
c4649fe
Test using file paths
Browse files- __pycache__/face_detection.cpython-312.pyc +0 -0
- app.py +14 -5
- face_detection.py +69 -0
- requirements.txt +3 -1
__pycache__/face_detection.cpython-312.pyc
ADDED
|
Binary file (3.74 kB). View file
|
|
|
app.py
CHANGED
|
@@ -3,20 +3,29 @@ import gradio as gr
|
|
| 3 |
import numpy as np
|
| 4 |
import random
|
| 5 |
import PIL
|
|
|
|
| 6 |
|
| 7 |
# import spaces #[uncomment to use ZeroGPU]
|
| 8 |
from diffusers import DiffusionPipeline
|
| 9 |
import torch
|
| 10 |
|
| 11 |
-
# Function to display the uploaded image
|
| 12 |
-
def process_image(image : PIL.Image.Image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# do AI stuff here
|
| 14 |
-
return image
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Create the Gradio interface
|
| 17 |
interface = gr.Interface(
|
| 18 |
-
fn=
|
| 19 |
-
inputs=gr.Image(type='
|
| 20 |
outputs=gr.Image(), # Display output
|
| 21 |
allow_flagging='never',
|
| 22 |
title="Celebrity Face Detector",
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import random
|
| 5 |
import PIL
|
| 6 |
+
import face_detection
|
| 7 |
|
| 8 |
# import spaces #[uncomment to use ZeroGPU]
|
| 9 |
from diffusers import DiffusionPipeline
|
| 10 |
import torch
|
| 11 |
|
| 12 |
+
# # Function to display the uploaded image
|
| 13 |
+
# def process_image(image : PIL.Image.Image):
|
| 14 |
+
# outputs = face_detection.getCroppedImages(image)
|
| 15 |
+
# # do AI stuff here
|
| 16 |
+
# return gr.Image(outputs[0])
|
| 17 |
+
|
| 18 |
+
def process_image_str(image : str):
|
| 19 |
+
face_detection.createCroppedSetFromImage(image, "outputs", "imgs")
|
| 20 |
# do AI stuff here
|
| 21 |
+
return gr.Image(image)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
|
| 25 |
# Create the Gradio interface
|
| 26 |
interface = gr.Interface(
|
| 27 |
+
fn=process_image_str, # Function to process the image
|
| 28 |
+
inputs=gr.Image(type='filepath'), # Upload input
|
| 29 |
outputs=gr.Image(), # Display output
|
| 30 |
allow_flagging='never',
|
| 31 |
title="Celebrity Face Detector",
|
face_detection.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import List
|
| 2 |
+
import face_recognition
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from os import path as p
|
| 5 |
+
import os
|
| 6 |
+
import shutil
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
def getCroppedImages(image: Image.Image, cap = -1):
|
| 10 |
+
"""Takes a PIL image, and returns a list of PIL images with the cropped faces"""
|
| 11 |
+
image = image.convert("RGB")
|
| 12 |
+
face_locations = face_recognition.face_locations(np.array(image))
|
| 13 |
+
|
| 14 |
+
outputs = []
|
| 15 |
+
num = 0
|
| 16 |
+
for f in face_locations:
|
| 17 |
+
# allow for capping the number of faces detected, helps prevent multiple faces being detected
|
| 18 |
+
# when there should only be one
|
| 19 |
+
if(cap != -1 and num >= cap):
|
| 20 |
+
break
|
| 21 |
+
|
| 22 |
+
outputs.append(image.crop([f[3], f[0], f[1], f[2]]))
|
| 23 |
+
num += 1
|
| 24 |
+
|
| 25 |
+
return outputs
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def createCroppedSetFromImage(input_path: str, output_dir: str, output_name: str, cap = -1):
|
| 29 |
+
"""
|
| 30 |
+
input_path: path to the input image
|
| 31 |
+
output_dir: folder to save the output image to
|
| 32 |
+
output_name: name for the new image (do not include extension)
|
| 33 |
+
cap: set to -1 to process all faces detected, otherwise will limit faces to value
|
| 34 |
+
"""
|
| 35 |
+
imgs = getCroppedImages(Image.open(input_path))
|
| 36 |
+
num = 0
|
| 37 |
+
for i in imgs:
|
| 38 |
+
path = p.join(output_dir, f"{output_name}_{num}.png")
|
| 39 |
+
i.save(path)
|
| 40 |
+
print(path)
|
| 41 |
+
num += 1
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def createCroppedSets(input_path: str, output_path: str):
|
| 45 |
+
"""
|
| 46 |
+
Iterates through subdirectories in the input directory, making a duplicate of it in the output directory.
|
| 47 |
+
Then iterates through all images in the subdirectory, cropping each face and saving it to the output directory.
|
| 48 |
+
|
| 49 |
+
For example for params "input" and "output", input/Dwayne Johnson/img.png will be cropped and
|
| 50 |
+
saved to output/Dwayne Johnson/0_0.png (repeated for all folder and the images they contain)
|
| 51 |
+
|
| 52 |
+
WARNING: IF A DIRECTORY WITH THE SAME NAME AS THE OUTPUT DIRECTORY, IT WILL BE DELETED
|
| 53 |
+
"""
|
| 54 |
+
# delete previous folder so there's no conflicts
|
| 55 |
+
if(p.exists(output_path)):
|
| 56 |
+
shutil.rmtree(output_path)
|
| 57 |
+
os.mkdir(output_path)
|
| 58 |
+
|
| 59 |
+
# iterate through all subdirectories in input directory
|
| 60 |
+
for dir in [name for name in os.listdir(input_path) if p.isdir(p.join(input_path, name))]:
|
| 61 |
+
sub_out = p.join(output_path, dir)
|
| 62 |
+
sub_in = p.join(input_path, dir)
|
| 63 |
+
os.mkdir(sub_out)
|
| 64 |
+
|
| 65 |
+
n = 0
|
| 66 |
+
# iterate through all files in subdirectory
|
| 67 |
+
for img in [name for name in os.listdir(sub_in) if p.isfile(p.join(sub_in, name))]:
|
| 68 |
+
createCroppedSetFromImage(p.join(sub_in, img), sub_out, n, 1)
|
| 69 |
+
n += 1
|
requirements.txt
CHANGED
|
@@ -3,4 +3,6 @@ diffusers
|
|
| 3 |
invisible_watermark
|
| 4 |
torch
|
| 5 |
transformers
|
| 6 |
-
xformers
|
|
|
|
|
|
|
|
|
| 3 |
invisible_watermark
|
| 4 |
torch
|
| 5 |
transformers
|
| 6 |
+
xformers
|
| 7 |
+
face_recognition
|
| 8 |
+
pillow
|