Spaces:
Running
Running
Josh Brown Kramer
commited on
Commit
·
74a9cf9
1
Parent(s):
ade96ad
Draft of using new face parsing
Browse files- app.py +4 -2
- faceparsing2.py +68 -0
- zombie.py +5 -8
app.py
CHANGED
|
@@ -4,7 +4,7 @@ from huggingface_hub import hf_hub_download
|
|
| 4 |
import onnxruntime as ort
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
-
from
|
| 8 |
|
| 9 |
# import torch
|
| 10 |
# from your_pix2pixhd_code import YourPix2PixHDModel, load_image, tensor2im # Adapt these imports
|
|
@@ -34,7 +34,8 @@ ort_session = ort.InferenceSession(model_path, providers=['CUDAExecutionProvider
|
|
| 34 |
|
| 35 |
# # return output_image
|
| 36 |
|
| 37 |
-
def predict(input_image, mode):
|
|
|
|
| 38 |
if mode == "Classic":
|
| 39 |
# Use the transition_onnx function for side-by-side comparison
|
| 40 |
zombie_image = zombie.transition_onnx(input_image, ort_session)
|
|
@@ -42,6 +43,7 @@ def predict(input_image, mode):
|
|
| 42 |
return "No face found"
|
| 43 |
return zombie_image
|
| 44 |
elif mode == "In Place":
|
|
|
|
| 45 |
# Use the make_faces_zombie_from_array function for in-place transformation
|
| 46 |
#zombie_image = zombie.make_faces_zombie_from_array(im_array, None, ort_session)
|
| 47 |
#if zombie_image is None:
|
|
|
|
| 4 |
import onnxruntime as ort
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
+
from faceparsing2 import get_face_mask
|
| 8 |
|
| 9 |
# import torch
|
| 10 |
# from your_pix2pixhd_code import YourPix2PixHDModel, load_image, tensor2im # Adapt these imports
|
|
|
|
| 34 |
|
| 35 |
# # return output_image
|
| 36 |
|
| 37 |
+
def predict(input_image, mode):
|
| 38 |
+
input_image = input_image.convert("RGB")
|
| 39 |
if mode == "Classic":
|
| 40 |
# Use the transition_onnx function for side-by-side comparison
|
| 41 |
zombie_image = zombie.transition_onnx(input_image, ort_session)
|
|
|
|
| 43 |
return "No face found"
|
| 44 |
return zombie_image
|
| 45 |
elif mode == "In Place":
|
| 46 |
+
im_array = np.array(input_image)
|
| 47 |
# Use the make_faces_zombie_from_array function for in-place transformation
|
| 48 |
#zombie_image = zombie.make_faces_zombie_from_array(im_array, None, ort_session)
|
| 49 |
#if zombie_image is None:
|
faceparsing2.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import argparse
|
| 3 |
+
import logging
|
| 4 |
+
from typing import List, Tuple, Optional
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
from tqdm import tqdm
|
| 9 |
+
import onnxruntime as ort
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
+
from huggingface_hub import hf_hub_download
|
| 12 |
+
import cv2
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
model_path = hf_hub_download(repo_id="jbrownkramer/face-parsing", filename="resnet18.onnx")
|
| 16 |
+
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if ort.get_device() == 'GPU' else ['CPUExecutionProvider']
|
| 17 |
+
session = ort.InferenceSession(model_path, providers=providers)
|
| 18 |
+
|
| 19 |
+
def prepare_image(image, input_size: Tuple[int, int] = (512, 512)) -> np.ndarray:
|
| 20 |
+
# Resize the image
|
| 21 |
+
resized_image = cv2.resize(image, input_size, interpolation=cv2.INTER_LINEAR)
|
| 22 |
+
|
| 23 |
+
# # Define transformation pipeline
|
| 24 |
+
# transform = transforms.Compose([
|
| 25 |
+
# transforms.ToTensor(),
|
| 26 |
+
# transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
|
| 27 |
+
# ])
|
| 28 |
+
|
| 29 |
+
# # Apply transformations
|
| 30 |
+
# image_tensor = transform(resized_image)
|
| 31 |
+
# image_batch = image_tensor.unsqueeze(0)
|
| 32 |
+
|
| 33 |
+
image_batch = np.array(resized_image)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
image_batch = image_batch / 255.0
|
| 39 |
+
image_batch -= np.array([[[0.485, 0.456, 0.406]]])
|
| 40 |
+
image_batch /= np.array([[[0.229, 0.224, 0.225]]])
|
| 41 |
+
image_batch = image_batch.transpose(2, 0, 1)
|
| 42 |
+
image_batch = image_batch.astype(np.float32)
|
| 43 |
+
image_batch = image_batch.reshape(1, 3, input_size[1], input_size[0])
|
| 44 |
+
|
| 45 |
+
return image_batch
|
| 46 |
+
|
| 47 |
+
def get_face_mask(image):
|
| 48 |
+
# Store original image resolution
|
| 49 |
+
h,w = image.shape[:2]
|
| 50 |
+
|
| 51 |
+
# Prepare image for inference
|
| 52 |
+
image_batch = prepare_image(image)
|
| 53 |
+
|
| 54 |
+
# Run ONNX inference
|
| 55 |
+
input_name = session.get_inputs()[0].name
|
| 56 |
+
outputs = session.run(None, {input_name: image_batch})
|
| 57 |
+
|
| 58 |
+
# Get the first output (assuming it's the segmentation map)
|
| 59 |
+
output = outputs[0]
|
| 60 |
+
|
| 61 |
+
# Convert to segmentation mask
|
| 62 |
+
predicted_mask = output.squeeze(0).argmax(0)
|
| 63 |
+
|
| 64 |
+
#resize to original size
|
| 65 |
+
restored_mask = cv2.resize(predicted_mask, (w,h), interpolation=cv2.INTER_NEAREST)
|
| 66 |
+
|
| 67 |
+
return restored_mask
|
| 68 |
+
|
zombie.py
CHANGED
|
@@ -8,6 +8,8 @@ import align
|
|
| 8 |
# import time
|
| 9 |
import cv2
|
| 10 |
|
|
|
|
|
|
|
| 11 |
# sys.path.append(r"..\face-parsing.PyTorch")
|
| 12 |
# import inferface
|
| 13 |
|
|
@@ -118,7 +120,7 @@ def ImageOpen(filepath):
|
|
| 118 |
# cases: image don't have getexif
|
| 119 |
return Image.open(filepath)
|
| 120 |
|
| 121 |
-
def do_face(
|
| 122 |
z = square_human_2_zombie_onnx(aligned,ort_session)
|
| 123 |
t1 = time.time()
|
| 124 |
|
|
@@ -127,14 +129,9 @@ def do_face(facenet, aligned, box, im_array, ort_session):
|
|
| 127 |
h,w,c = im_array.shape
|
| 128 |
face_in_place = cv2.warpAffine(np.array(z),t,(w,h))
|
| 129 |
|
| 130 |
-
face_classes =
|
| 131 |
face_mask = np.logical_and(face_classes < 14, face_classes > 0)
|
| 132 |
|
| 133 |
-
# face_classes_z = inferface.run_net(facenet,z)
|
| 134 |
-
# face_mask_z = np.logical_and(face_classes_z < 14, face_classes > 0)
|
| 135 |
-
|
| 136 |
-
# face_mask = np.logical_or(face_mask,face_mask_z)
|
| 137 |
-
|
| 138 |
imagemask = cv2.warpAffine(face_mask.astype("uint8"),t,(w,h))
|
| 139 |
|
| 140 |
imagemask = imagemask.astype("uint8")
|
|
@@ -158,6 +155,6 @@ def make_faces_zombie_from_array(im_array_rgb, facenet, ort_session):
|
|
| 158 |
faces = []
|
| 159 |
|
| 160 |
for aligned,box in faces:
|
| 161 |
-
do_face(
|
| 162 |
|
| 163 |
return Image.fromarray(im_array_rgb)
|
|
|
|
| 8 |
# import time
|
| 9 |
import cv2
|
| 10 |
|
| 11 |
+
from faceparsing2 import get_face_mask
|
| 12 |
+
|
| 13 |
# sys.path.append(r"..\face-parsing.PyTorch")
|
| 14 |
# import inferface
|
| 15 |
|
|
|
|
| 120 |
# cases: image don't have getexif
|
| 121 |
return Image.open(filepath)
|
| 122 |
|
| 123 |
+
def do_face(aligned, box, im_array, ort_session):
|
| 124 |
z = square_human_2_zombie_onnx(aligned,ort_session)
|
| 125 |
t1 = time.time()
|
| 126 |
|
|
|
|
| 129 |
h,w,c = im_array.shape
|
| 130 |
face_in_place = cv2.warpAffine(np.array(z),t,(w,h))
|
| 131 |
|
| 132 |
+
face_classes = get_face_mask(aligned)
|
| 133 |
face_mask = np.logical_and(face_classes < 14, face_classes > 0)
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
imagemask = cv2.warpAffine(face_mask.astype("uint8"),t,(w,h))
|
| 136 |
|
| 137 |
imagemask = imagemask.astype("uint8")
|
|
|
|
| 155 |
faces = []
|
| 156 |
|
| 157 |
for aligned,box in faces:
|
| 158 |
+
do_face(aligned,box,im_array_rgb,ort_session)
|
| 159 |
|
| 160 |
return Image.fromarray(im_array_rgb)
|