Adapted code for model usage as "server"
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
Collecting tensorflow
|
||||
Using cached tensorflow-2.19.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (4.1 kB)
|
||||
Requirement already satisfied: absl-py>=1.0.0 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (2.3.1)
|
||||
Requirement already satisfied: astunparse>=1.6.0 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (1.6.3)
|
||||
Requirement already satisfied: flatbuffers>=24.3.25 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (25.2.10)
|
||||
Requirement already satisfied: gast!=0.5.0,!=0.5.1,!=0.5.2,>=0.2.1 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (0.6.0)
|
||||
Requirement already satisfied: google-pasta>=0.1.1 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (0.2.0)
|
||||
Requirement already satisfied: libclang>=13.0.0 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (18.1.1)
|
||||
Requirement already satisfied: opt-einsum>=2.3.2 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (3.4.0)
|
||||
Requirement already satisfied: packaging in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (25.0)
|
||||
Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<6.0.0dev,>=3.20.3 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (4.25.8)
|
||||
Requirement already satisfied: requests<3,>=2.21.0 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (2.32.4)
|
||||
Requirement already satisfied: setuptools in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (78.1.1)
|
||||
Requirement already satisfied: six>=1.12.0 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (1.17.0)
|
||||
Requirement already satisfied: termcolor>=1.1.0 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (3.1.0)
|
||||
Requirement already satisfied: typing-extensions>=3.6.6 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (4.14.1)
|
||||
Requirement already satisfied: wrapt>=1.11.0 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (1.17.2)
|
||||
Requirement already satisfied: grpcio<2.0,>=1.24.3 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (1.73.1)
|
||||
Requirement already satisfied: tensorboard~=2.19.0 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (2.19.0)
|
||||
Requirement already satisfied: keras>=3.5.0 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (3.10.0)
|
||||
Requirement already satisfied: numpy<2.2.0,>=1.26.0 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (1.26.4)
|
||||
Requirement already satisfied: h5py>=3.11.0 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (3.14.0)
|
||||
Requirement already satisfied: ml-dtypes<1.0.0,>=0.5.1 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (0.5.1)
|
||||
Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorflow) (0.37.1)
|
||||
Requirement already satisfied: charset_normalizer<4,>=2 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from requests<3,>=2.21.0->tensorflow) (3.4.2)
|
||||
Requirement already satisfied: idna<4,>=2.5 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from requests<3,>=2.21.0->tensorflow) (3.10)
|
||||
Requirement already satisfied: urllib3<3,>=1.21.1 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from requests<3,>=2.21.0->tensorflow) (2.5.0)
|
||||
Requirement already satisfied: certifi>=2017.4.17 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from requests<3,>=2.21.0->tensorflow) (2025.6.15)
|
||||
Requirement already satisfied: markdown>=2.6.8 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorboard~=2.19.0->tensorflow) (3.8.2)
|
||||
Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorboard~=2.19.0->tensorflow) (0.7.2)
|
||||
Requirement already satisfied: werkzeug>=1.0.1 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from tensorboard~=2.19.0->tensorflow) (3.1.3)
|
||||
Requirement already satisfied: wheel<1.0,>=0.23.0 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from astunparse>=1.6.0->tensorflow) (0.45.1)
|
||||
Requirement already satisfied: rich in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from keras>=3.5.0->tensorflow) (14.0.0)
|
||||
Requirement already satisfied: namex in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from keras>=3.5.0->tensorflow) (0.1.0)
|
||||
Requirement already satisfied: optree in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from keras>=3.5.0->tensorflow) (0.16.0)
|
||||
Requirement already satisfied: MarkupSafe>=2.1.1 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from werkzeug>=1.0.1->tensorboard~=2.19.0->tensorflow) (3.0.2)
|
||||
Requirement already satisfied: markdown-it-py>=2.2.0 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from rich->keras>=3.5.0->tensorflow) (3.0.0)
|
||||
Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from rich->keras>=3.5.0->tensorflow) (2.19.2)
|
||||
Requirement already satisfied: mdurl~=0.1 in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (from markdown-it-py>=2.2.0->rich->keras>=3.5.0->tensorflow) (0.1.2)
|
||||
Using cached tensorflow-2.19.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (644.8 MB)
|
||||
Installing collected packages: tensorflow
|
||||
Successfully installed tensorflow-2.19.0
|
||||
@@ -1 +0,0 @@
|
||||
Requirement already satisfied: numpy in /home/bijan_wsl/miniconda3/envs/obfuscation_platform/lib/python3.10/site-packages (1.26.4)
|
||||
Binary file not shown.
Binary file not shown.
+2
-2
@@ -14,8 +14,8 @@ class Config:
|
||||
obfuscated_output_directory="datasets/obfuscated/"
|
||||
max_images_per_dataset=1000
|
||||
|
||||
seg_model_type="yolo11m-seg.pt"
|
||||
pose_model_type="yolo11m-pose.pt"
|
||||
seg_model_type="models/yolo11m-seg.pt"
|
||||
pose_model_type="models/yolo11m-pose.pt"
|
||||
face_model_type="yunet"
|
||||
|
||||
evaluation_results_directory = "evaluation_results/"
|
||||
|
||||
Binary file not shown.
@@ -5,7 +5,6 @@ from typing import List, Dict, Tuple, Any
|
||||
import cv2
|
||||
import numpy as np
|
||||
import torch
|
||||
import torchvision # for IoU
|
||||
from ultralytics import YOLO
|
||||
from deepface import DeepFace
|
||||
import mediapipe as mp
|
||||
@@ -44,6 +43,7 @@ def _emotion(crop_rgb: np.ndarray) -> Tuple[str, Dict[str, float]]:
|
||||
We skip DeepFace's own detector because we already have the bbox.
|
||||
"""
|
||||
res = DeepFace.analyze(cv2.cvtColor(crop_rgb, cv2.COLOR_RGB2BGR), detector_backend="skip", actions=["emotion"], enforce_detection=False)[0]
|
||||
res['emotion'] = {k: float(v) for k, v in res['emotion'].items()}
|
||||
return res["dominant_emotion"], res["emotion"]
|
||||
|
||||
|
||||
@@ -98,7 +98,6 @@ class MetaExtractor:
|
||||
def _extract_objects_and_persons(self, img_rgb: np.ndarray) -> Tuple[List[PersonMeta], List[ObjectMeta]]:
|
||||
H, W = img_rgb.shape[:2]
|
||||
seg_res = self.segmentor.predict(img_rgb, verbose=False, device=0)[0]
|
||||
|
||||
classes = seg_res.boxes.cls.cpu().numpy().astype(int)
|
||||
|
||||
person_ids = np.where(classes == 0)[0] # COCO class 0 == person
|
||||
|
||||
@@ -5,6 +5,8 @@ import matplotlib
|
||||
|
||||
|
||||
matplotlib.use('Agg')
|
||||
from fastapi import FastAPI, HTTPException
|
||||
|
||||
import cv2
|
||||
import yaml
|
||||
from argparse import ArgumentParser
|
||||
@@ -21,7 +23,6 @@ import torch
|
||||
from sync_batchnorm import DataParallelWithCallback
|
||||
import torch.nn.functional as F
|
||||
|
||||
#from modules.segmentation_module import SegmentationModule
|
||||
from modules.segmentation_module import SegmentationModule
|
||||
from modules.reconstruction_module import ReconstructionModule
|
||||
|
||||
@@ -104,7 +105,7 @@ class PartSwapGenerator(ReconstructionModule):
|
||||
|
||||
def load_checkpoints(config, checkpoint, blend_scale=0.125, first_order_motion_model=False, cpu=False):
|
||||
with open(config) as f:
|
||||
config = yaml.safe_load(f)#yaml.load(f)
|
||||
config = yaml.safe_load(f) #yaml.load(f) #
|
||||
|
||||
reconstruction_module = PartSwapGenerator(blend_scale=blend_scale,
|
||||
first_order_motion_model=first_order_motion_model,
|
||||
@@ -197,9 +198,23 @@ def make_video(swap_index, source_image, target_video, reconstruction_module, se
|
||||
predictions.append(np.transpose(out['prediction'].data.cpu().numpy(), [0, 2, 3, 1])[0])
|
||||
return predictions
|
||||
|
||||
#python face_obfuscator.py --config config/vox-256-sem-15segments.yaml --checkpoint stuff/vox-15segments.pth.tar --swap_index 2,3,9,11,15
|
||||
def obfuscate(swap_index, source_image, target_image, reconstruction_module, segmentation_module):
|
||||
original_shape = target_image.shape
|
||||
model_output = img_as_ubyte(make_video(swap_index=swap_index,
|
||||
source_image=source_image,
|
||||
target_video=[resize(target_image, (256, 256))[..., :3]],
|
||||
reconstruction_module=reconstruction_module,
|
||||
segmentation_module=segmentation_module, cpu=True)[0])
|
||||
#TODO Bgr-Rgb ?
|
||||
cv2.imshow("TEST", model_output)
|
||||
matched = match_histograms(model_output, target_image)
|
||||
cv2.imshow("TEST1", model_output)
|
||||
output = cv2.resize(matched, (original_shape[1], original_shape[0]), interpolation=cv2.INTER_LINEAR)
|
||||
return output
|
||||
|
||||
class EPIC:
|
||||
def __init__(self, checkpoint_path, config_path, fake_face_path):
|
||||
print("Running speed_evaluation")
|
||||
self.swap_indices = [2, 3, 9, 11, 15] #Indices of face regions to swap
|
||||
self.fake_face = resize(imageio.imread(fake_face_path), (256, 256))[..., :3]
|
||||
self.reconstruction_module, self.segmentation_module = load_checkpoints(config_path, checkpoint_path, blend_scale=1, cpu=True)
|
||||
@@ -212,56 +227,57 @@ class EPIC:
|
||||
segmentation_module=self.segmentation_module)
|
||||
|
||||
|
||||
#python face_obfuscator.py --config config/vox-256-sem-15segments.yaml --checkpoint stuff/vox-15segments.pth.tar --swap_index 2,3,9,11,15
|
||||
def obfuscate(swap_index, source_image, target_image, reconstruction_module, segmentation_module):
|
||||
original_shape = target_image.shape
|
||||
model_output = img_as_ubyte(make_video(swap_index=swap_index,
|
||||
source_image=source_image,
|
||||
target_video=[resize(target_image, (256, 256))[..., :3]],
|
||||
reconstruction_module=reconstruction_module,
|
||||
segmentation_module=segmentation_module)[0])
|
||||
matched = match_histograms(model_output, target_image)
|
||||
output = cv2.resize(matched, (original_shape[1], original_shape[0]), interpolation=cv2.INTER_LINEAR)
|
||||
return output
|
||||
|
||||
app = FastAPI()
|
||||
epic = None
|
||||
image_processing_path = "C:/Users/bijan/Desktop/obfuscation_platform/project/models/epic/image_cache/"
|
||||
|
||||
@app.get("/init")
|
||||
def init():
|
||||
try:
|
||||
print("initiating epic")
|
||||
checkpoint_path = "stuff/vox-15segments.pth.tar"
|
||||
config_path = "config/vox-256-sem-15segments.yaml"
|
||||
fake_face_path = "stuff/fake_face.jpg"
|
||||
global epic
|
||||
epic = EPIC(checkpoint_path, config_path, fake_face_path)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
raise HTTPException(500, detail=str(e))
|
||||
|
||||
@app.get("/obfuscate")
|
||||
def obfuscate_api():
|
||||
print("obfuscating")
|
||||
img = cv2.imread(os.path.join(image_processing_path, "face.jpg"), cv2.IMREAD_UNCHANGED)
|
||||
if img is None:
|
||||
raise HTTPException(500, f"cv2 failed to read image at {image_processing_path}")
|
||||
global epic
|
||||
obfusacted_img = epic.obfuscate(img)
|
||||
cv2.imwrite(os.path.join(image_processing_path, "obfuscated_face.jpg"), obfusacted_img)
|
||||
print("done")
|
||||
|
||||
|
||||
def measure_speed(fake_face, target_image_paths, reconstruction_module, segmentation_module):
|
||||
times = []
|
||||
counter = 0
|
||||
for path in target_image_paths:
|
||||
target_image = imageio.imread(path)
|
||||
original_shape = target_image.shape
|
||||
fake_face = resize(fake_face, (256, 256))[..., :3] #256
|
||||
target_video = [target_image]
|
||||
target_video = [resize(frame, (256, 256))[..., :3] for frame in target_video]
|
||||
swap_indices = [2, 3, 9, 11, 15]
|
||||
start = time.time()
|
||||
output = obfuscate(swap_indices, fake_face, target_video, reconstruction_module, segmentation_module)
|
||||
output = cv2.resize(output, (original_shape[1], original_shape[0]), interpolation=cv2.INTER_LINEAR)
|
||||
end = time.time()
|
||||
times.append(end - start) #0.9638 bei 256 UND 0.241 bei 128
|
||||
print(counter)
|
||||
counter += 1
|
||||
avg = sum(times) / len(times)
|
||||
return avg
|
||||
@app.get("/health")
|
||||
def health(): return {"ok": True}
|
||||
|
||||
|
||||
def main():
|
||||
print("Running speed_evaluation")
|
||||
checkpoint = "stuff/vox-15segments.pth.tar"
|
||||
config = "config/vox-256-sem-15segments.yaml"
|
||||
fake_face = imageio.imread("stuff/fake_face.jpg")
|
||||
target_image_paths = os.listdir("stuff/speed_eval_images/")
|
||||
random.shuffle(target_image_paths)
|
||||
target_image_paths = [os.path.join("stuff/speed_eval_images/", path) for path in target_image_paths[:1000]]
|
||||
blend_scale = 1
|
||||
reconstruction_module, segmentation_module = load_checkpoints(config, checkpoint, blend_scale=blend_scale, cpu=True)
|
||||
avg_speed = measure_speed(fake_face, target_image_paths, reconstruction_module, segmentation_module)
|
||||
print(avg_speed)
|
||||
print("Done")
|
||||
# def main_old():
|
||||
# print("Running speed_evaluation")
|
||||
# checkpoint = "stuff/vox-15segments.pth.tar"
|
||||
# config = "config/vox-256-sem-15segments.yaml"
|
||||
# fake_face = imageio.imread("stuff/fake_face.jpg")
|
||||
# target_image_paths = os.listdir("stuff/speed_eval_images/")
|
||||
# random.shuffle(target_image_paths)
|
||||
# target_image_paths = [os.path.join("stuff/speed_eval_images/", path) for path in target_image_paths[:1000]]
|
||||
# blend_scale = 1
|
||||
# reconstruction_module, segmentation_module = load_checkpoints(config, checkpoint, blend_scale=blend_scale, cpu=True)
|
||||
# avg_speed = measure_speed(fake_face, target_image_paths, reconstruction_module, segmentation_module)
|
||||
# print(avg_speed)
|
||||
# print("Done")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
print("Starting from main")
|
||||
# main()
|
||||
# parser = ArgumentParser()
|
||||
# parser.add_argument("--config", required=True, help="path to config")
|
||||
# parser.add_argument("--checkpoint", default='vox-cpk.pth.tar', help="path to checkpoint to restore")
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
@@ -4,7 +4,7 @@ import torch.nn.functional as F
|
||||
import imageio
|
||||
|
||||
import os
|
||||
from skimage.draw import circle
|
||||
#from skimage.draw import circle TODO REMOVED
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import collections
|
||||
@@ -133,10 +133,10 @@ class Visualizer:
|
||||
image = np.copy(image)
|
||||
spatial_size = np.array(image.shape[:2][::-1])[np.newaxis]
|
||||
kp_array = spatial_size * (kp_array + 1) / 2
|
||||
num_kp = kp_array.shape[0]
|
||||
for kp_ind, kp in enumerate(kp_array):
|
||||
rr, cc = circle(kp[1], kp[0], self.kp_size, shape=image.shape[:2])
|
||||
image[rr, cc] = np.array(self.colormap(kp_ind / num_kp))[:3]
|
||||
# num_kp = kp_array.shape[0]
|
||||
# for kp_ind, kp in enumerate(kp_array):
|
||||
# rr, cc = circle(kp[1], kp[0], self.kp_size, shape=image.shape[:2]) TODO REMOVED
|
||||
# image[rr, cc] = np.array(self.colormap(kp_ind / num_kp))[:3]
|
||||
return image
|
||||
|
||||
def create_image_column_with_kp(self, images, kp):
|
||||
|
||||
@@ -21,7 +21,7 @@ scikit-image==0.14.0
|
||||
scikit-learn==0.19.2
|
||||
scipy==1.1.0
|
||||
six==1.11.0
|
||||
toolz==0.9.0
|
||||
#toolz==0.9.0
|
||||
torch==1.0.0
|
||||
torchvision==0.2.1
|
||||
tqdm==4.24.0
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
@echo off
|
||||
REM — Activate your conda env and return here
|
||||
call conda activate obfuscation_platform_p37
|
||||
if ERRORLEVEL 1 (
|
||||
echo Failed to activate obda environment.
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo Environment activated. Starting server...
|
||||
uvicorn face_obfuscator:app --host 127.0.0.1 --port 8000
|
||||
Binary file not shown.
+16
-15
@@ -1,29 +1,30 @@
|
||||
import numpy as np
|
||||
import os
|
||||
|
||||
import numpy as np
|
||||
import cv2
|
||||
from obfuscation.base_obfuscation_technique import ObfuscationTechnique
|
||||
from models.epic.face_obfuscator import EPIC
|
||||
import requests
|
||||
|
||||
|
||||
class Epic(ObfuscationTechnique):
|
||||
"""
|
||||
Replace face with generated face
|
||||
"""
|
||||
def __init__(self):
|
||||
self.image_processing_path = "C:/Users/bijan/Desktop/obfuscation_platform/project/models/epic/image_cache/"
|
||||
resp = requests.get(f"http://127.0.0.1:8000/init", timeout=5)
|
||||
resp.raise_for_status()
|
||||
|
||||
#TODO: da drin nochmal alle sub dinger prüfen was da angepasst wurde jeweils
|
||||
|
||||
#TODO: in conda python 3.7 umgebung erstellen und einrichten mit requirements usw.
|
||||
#TODO: chatgpt checken
|
||||
#TODO: server, client anpassen
|
||||
|
||||
#TODO: Für alle anderen modelle das gleiche tun mit anderen python versionen
|
||||
|
||||
checkpoint_path = "models/epic/stuff/vox-15segments.pth.tar"
|
||||
config_path = "models/epic/config/vox-256-sem-15segments.yaml"
|
||||
fake_face_path = "models/epic/stuff/fake_face.jpg"
|
||||
self.model = EPIC(checkpoint_path=checkpoint_path, config_path=config_path, fake_face_path=fake_face_path)
|
||||
|
||||
def obfuscate(self, face_roi_img_bgr: np.ndarray) -> np.ndarray:
|
||||
return self.model.obfuscate(face_roi_img_bgr)
|
||||
cv2.imwrite(os.path.join(self.image_processing_path, "face.jpg"), face_roi_img_bgr)
|
||||
resp = requests.get(f"http://127.0.0.1:8000/obfuscate", timeout=5)
|
||||
resp.raise_for_status()
|
||||
obfuscated_face = cv2.imread(os.path.join(self.image_processing_path, "obfuscated_face.jpg"))
|
||||
exit() #TODO Entfernen
|
||||
#TODO: Das bild ggf zu RGB bei face_obfuscator.py und dann wieder zurück
|
||||
#TODO bei epic noch cloning, schauen wie das war
|
||||
return obfuscated_face
|
||||
|
||||
def name(self):
|
||||
return "EPIC"
|
||||
Reference in New Issue
Block a user