Winston de Jong commited on
Commit ·
abdd359
1
Parent(s): e108bf9
Add option to return face locations, so we can highlight them on the image in the future
Browse files- app.py +1 -1
- face_detection.py +27 -7
app.py
CHANGED
|
@@ -16,7 +16,7 @@ import torch
|
|
| 16 |
# return gr.Image(outputs[0])
|
| 17 |
|
| 18 |
def process_image_str(image : str):
|
| 19 |
-
face_detection.
|
| 20 |
# do AI stuff here
|
| 21 |
|
| 22 |
return gr.Image(image)
|
|
|
|
| 16 |
# return gr.Image(outputs[0])
|
| 17 |
|
| 18 |
def process_image_str(image : str):
|
| 19 |
+
locations, paths = face_detection.getFaceLocationsAndFiles(image, "outputs", "imgs")
|
| 20 |
# do AI stuff here
|
| 21 |
|
| 22 |
return gr.Image(image)
|
face_detection.py
CHANGED
|
@@ -22,22 +22,42 @@ def getCroppedImages(image: Image.Image, cap = -1):
|
|
| 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(p.abspath(path))
|
| 41 |
num += 1
|
| 42 |
|
| 43 |
|
|
|
|
| 22 |
outputs.append(image.crop([f[3], f[0], f[1], f[2]]))
|
| 23 |
num += 1
|
| 24 |
|
| 25 |
+
return face_locations, outputs
|
| 26 |
+
|
| 27 |
|
| 28 |
+
def getFaceLocationsAndFiles(input_path: str, output_dir: str, output_name: str,):
|
| 29 |
+
"""
|
| 30 |
+
input_path: local or absolute path to the input image\n
|
| 31 |
+
output_dir: folder (local or absolute path) to save the output image to\n
|
| 32 |
+
output_name: name for the new image (do not include extension)\n
|
| 33 |
+
returns: list of face locations and list of paths to the cropped face images
|
| 34 |
+
"""
|
| 35 |
+
locs, imgs = getCroppedImages(Image.open(input_path))
|
| 36 |
+
output_paths = []
|
| 37 |
+
num = 0
|
| 38 |
+
for i in imgs:
|
| 39 |
+
path = p.join(output_dir, f"{output_name}_{num}.png")
|
| 40 |
+
i.save(path)
|
| 41 |
+
output_paths.append(p.abspath(path))
|
| 42 |
+
num += 1
|
| 43 |
+
|
| 44 |
+
return locs, output_paths
|
| 45 |
+
|
| 46 |
|
| 47 |
def createCroppedSetFromImage(input_path: str, output_dir: str, output_name: str, cap = -1):
|
| 48 |
"""
|
| 49 |
+
input_path: local or absolute path to the input image\n
|
| 50 |
+
output_dir: folder (local or absolute path) to save the output image to\n
|
| 51 |
+
output_name: name for the new image (do not include extension)\n
|
| 52 |
+
cap: set to -1 to process all faces detected, otherwise will limit faces to value\n
|
| 53 |
+
returns: list of absolute paths to the images
|
| 54 |
"""
|
| 55 |
+
imgs = getCroppedImages(Image.open(input_path))[1]
|
| 56 |
+
|
| 57 |
num = 0
|
| 58 |
for i in imgs:
|
| 59 |
path = p.join(output_dir, f"{output_name}_{num}.png")
|
| 60 |
i.save(path)
|
|
|
|
| 61 |
num += 1
|
| 62 |
|
| 63 |
|