Skip to content

Commit 54f0c14

Browse files
committed
download better face detection module dynamically
1 parent db8ed5f commit 54f0c14

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

modules/textual_inversion/autocrop.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import cv2
2+
import requests
23
import os
34
from collections import defaultdict
45
from math import log, sqrt
@@ -293,6 +294,25 @@ def is_square(w, h):
293294
return w == h
294295

295296

297+
def download_and_cache_models(dirname):
298+
download_url = 'https://github.com/opencv/opencv_zoo/blob/91fb0290f50896f38a0ab1e558b74b16bc009428/models/face_detection_yunet/face_detection_yunet_2022mar.onnx?raw=true'
299+
model_file_name = 'face_detection_yunet.onnx'
300+
301+
if not os.path.exists(dirname):
302+
os.makedirs(dirname)
303+
304+
cache_file = os.path.join(dirname, model_file_name)
305+
if not os.path.exists(cache_file):
306+
print(f"downloading face detection model from '{download_url}' to '{cache_file}'")
307+
response = requests.get(download_url)
308+
with open(cache_file, "wb") as f:
309+
f.write(response.content)
310+
311+
if os.path.exists(cache_file):
312+
return cache_file
313+
return None
314+
315+
296316
class PointOfInterest:
297317
def __init__(self, x, y, weight=1.0, size=10):
298318
self.x = x

modules/textual_inversion/preprocess.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import time
88

99
from modules import shared, images
10+
from modules.paths import models_path
1011
from modules.shared import opts, cmd_opts
1112
from modules.textual_inversion import autocrop
1213
if cmd_opts.deepdanbooru:
@@ -146,14 +147,22 @@ def split_pic(image, inverse_xy):
146147
save_pic(splitted, index, existing_caption=existing_caption)
147148
process_default_resize = False
148149

149-
if process_entropy_focus and img.height != img.width:
150+
if process_focal_crop and img.height != img.width:
151+
152+
dnn_model_path = None
153+
try:
154+
dnn_model_path = autocrop.download_and_cache_models(os.path.join(models_path, "opencv"))
155+
except Exception as e:
156+
print("Unable to load face detection model for auto crop selection. Falling back to lower quality haar method.", e)
157+
150158
autocrop_settings = autocrop.Settings(
151159
crop_width = width,
152160
crop_height = height,
153161
face_points_weight = process_focal_crop_face_weight,
154162
entropy_points_weight = process_focal_crop_entropy_weight,
155163
corner_points_weight = process_focal_crop_edges_weight,
156-
annotate_image = process_focal_crop_debug
164+
annotate_image = process_focal_crop_debug,
165+
dnn_model_path = dnn_model_path,
157166
)
158167
for focal in autocrop.crop_image(img, autocrop_settings):
159168
save_pic(focal, index, existing_caption=existing_caption)

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ gradio==3.5
88
invisible-watermark
99
numpy
1010
omegaconf
11+
opencv-python
12+
requests
1113
piexif
1214
Pillow
1315
pytorch_lightning

0 commit comments

Comments
 (0)