Skip to content

Commit

Permalink
FIX: halving of det_size
Browse files Browse the repository at this point in the history
  • Loading branch information
Gourieff committed Oct 5, 2023
1 parent 0778623 commit 0070cea
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions scripts/reactor_swapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,20 @@ def get_face_age(face, face_index):
return "None"
return face_age

def reget_face_single(face, det_size, face_index, gender_source, gender_target):
det_size_half = (det_size[0] // 2, det_size[1] // 2)
return get_face_single(face, face_index=face_index, det_size=det_size_half, gender_source=gender_source, gender_target=gender_target)
# def reget_face_single(img_data: np.ndarray, face, det_size, face_index, gender_source, gender_target):
# det_size_half = (det_size[0] // 2, det_size[1] // 2)
# return get_face_single(img_data, face, face_index=face_index, det_size=det_size_half, gender_source=gender_source, gender_target=gender_target)

def half_det_size(det_size):
logger.info("Trying to halve 'det_size' parameter")
return (det_size[0] // 2, det_size[1] // 2)

def analyze_faces(img_data: np.ndarray, det_size=(640, 640)):
face_analyser = copy.deepcopy(getAnalysisModel())
face_analyser.prepare(ctx_id=0, det_size=det_size)
return face_analyser.get(img_data)

def get_face_single(face, face_index=0, det_size=(640, 640), gender_source=0, gender_target=0):
def get_face_single(img_data: np.ndarray, face, face_index=0, det_size=(640, 640), gender_source=0, gender_target=0):

buffalo_path = os.path.join(models_path, "insightface/models/buffalo_l.zip")
if os.path.exists(buffalo_path):
Expand All @@ -233,18 +237,21 @@ def get_face_single(face, face_index=0, det_size=(640, 640), gender_source=0, ge

if gender_source != 0:
if len(face) == 0 and det_size[0] > 320 and det_size[1] > 320:
return reget_face_single(face, det_size, face_index, gender_source, gender_target)
det_size_half = half_det_size(det_size)
return get_face_single(img_data, analyze_faces(img_data, det_size_half), face_index, det_size_half, gender_source, gender_target)
faces, wrong_gender = get_face_gender(face,face_index,gender_source,"Source",gender_detected)
return faces, wrong_gender, face_age, face_gender

if gender_target != 0:
if len(face) == 0 and det_size[0] > 320 and det_size[1] > 320:
return reget_face_single(face, det_size, face_index, gender_source, gender_target)
det_size_half = half_det_size(det_size)
return get_face_single(img_data, analyze_faces(img_data, det_size_half), face_index, det_size_half, gender_source, gender_target)
faces, wrong_gender = get_face_gender(face,face_index,gender_target,"Target",gender_detected)
return faces, wrong_gender, face_age, face_gender

if len(face) == 0 and det_size[0] > 320 and det_size[1] > 320:
return reget_face_single(face, det_size, face_index, gender_source, gender_target)
det_size_half = half_det_size(det_size)
return get_face_single(img_data, analyze_faces(img_data, det_size_half), face_index, det_size_half, gender_source, gender_target)

try:
return sorted(face, key=lambda x: x.bbox[0])[face_index], 0, face_age, face_gender
Expand Down Expand Up @@ -298,7 +305,7 @@ def swap_face(
target_faces = analyze_faces(target_img)

logger.info("Detecting Source Face, Index = %s", source_faces_index[0])
source_face, wrong_gender, source_age, source_gender = get_face_single(source_faces, face_index=source_faces_index[0], gender_source=gender_source)
source_face, wrong_gender, source_age, source_gender = get_face_single(source_img, source_faces, face_index=source_faces_index[0], gender_source=gender_source)
if source_age != "None" or source_gender != "None":
logger.info("Detected: -%s- y.o. %s", source_age, source_gender)

Expand All @@ -319,7 +326,7 @@ def swap_face(
return result_image, [], 0
if len(source_faces_index) > 1 and source_face_idx > 0:
logger.info("Detecting Source Face, Index = %s", source_faces_index[source_face_idx])
source_face, wrong_gender, source_age, source_gender = get_face_single(source_faces, face_index=source_faces_index[source_face_idx], gender_source=gender_source)
source_face, wrong_gender, source_age, source_gender = get_face_single(source_img, source_faces, face_index=source_faces_index[source_face_idx], gender_source=gender_source)
if source_age != "None" or source_gender != "None":
logger.info("Detected: -%s- y.o. %s", source_age, source_gender)

Expand All @@ -330,7 +337,7 @@ def swap_face(

if source_face is not None and wrong_gender == 0:
logger.info("Detecting Target Face, Index = %s", face_num)
target_face, wrong_gender, target_age, target_gender = get_face_single(target_faces, face_index=face_num, gender_target=gender_target)
target_face, wrong_gender, target_age, target_gender = get_face_single(target_img, target_faces, face_index=face_num, gender_target=gender_target)
if target_age != "None" or target_gender != "None":
logger.info("Detected: -%s- y.o. %s", target_age, target_gender)

Expand Down Expand Up @@ -376,7 +383,7 @@ def swap_face(
result_image = enhance_image(result_image, enhancement_options)

else:
logger.info("No source face(s) in Index %s", source_faces_index[source_face_idx])
logger.info("No source face(s) in the provided Index")
else:
logger.info("No source face(s) found")

Expand Down

0 comments on commit 0070cea

Please sign in to comment.