|
import cv2 |
|
import mediapipe as mp |
|
import os |
|
from gradio_client import Client |
|
|
|
|
|
from test_image import Test |
|
import numpy as np |
|
|
|
|
|
|
|
from PIL import Image |
|
import numpy as np |
|
import cv2 |
|
|
|
|
|
|
|
data = 'faceswap' |
|
dct = 'fft' |
|
|
|
|
|
|
|
|
|
|
|
|
|
testet = Test(model_path =f"weights/{data}-hh-best_model.pth", |
|
multi_modal ='hh') |
|
|
|
|
|
mp_face_detection = mp.solutions.face_detection |
|
mp_drawing = mp.solutions.drawing_utils |
|
face_detection = mp_face_detection.FaceDetection(model_selection=1, min_detection_confidence=0.35) |
|
|
|
|
|
save_dir = "cropped_faces" |
|
os.makedirs(save_dir, exist_ok=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def pil_to_opencv(pil_image): |
|
open_cv_image = np.array(pil_image) |
|
|
|
open_cv_image = open_cv_image[:, :, ::-1].copy() |
|
return open_cv_image |
|
|
|
|
|
def opencv_to_pil(opencv_image): |
|
|
|
pil_image = Image.fromarray(opencv_image[:, :, ::-1]) |
|
return pil_image |
|
|
|
|
|
|
|
|
|
def detect_and_label_faces(frame): |
|
frame = pil_to_opencv(frame) |
|
|
|
|
|
print(type(frame)) |
|
|
|
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) |
|
|
|
results = face_detection.process(frame_rgb) |
|
|
|
|
|
if results.detections: |
|
for face_count,detection in enumerate(results.detections): |
|
bboxC = detection.location_data.relative_bounding_box |
|
ih, iw, _ = frame.shape |
|
x, y, w, h = int(bboxC.xmin * iw), int(bboxC.ymin * ih), int(bboxC.width * iw), int(bboxC.height * ih) |
|
|
|
crop_img = frame[max(0, y):min(ih, y+h), max(0, x):min(iw, x+w)] |
|
if crop_img.size > 0: |
|
face_filename = os.path.join(save_dir, f'face_{face_count}.jpg') |
|
cv2.imwrite(face_filename, crop_img) |
|
|
|
label = testet.testimage(face_filename) |
|
|
|
if os.path.exists(face_filename): |
|
os.remove(face_filename) |
|
|
|
color = (0, 0, 255) if label == 'fake' else (0, 255, 0) |
|
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2) |
|
cv2.putText(frame, label, (x, y + 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2) |
|
return opencv_to_pil(frame) |
|
|
|
|