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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
import os
|
| 5 |
+
from face_cropper import detect_and_label_faces
|
| 6 |
+
# Define a custom function to convert an image to grayscale
|
| 7 |
+
def to_grayscale(input_image):
|
| 8 |
+
grayscale_image = Image.fromarray(np.array(input_image).mean(axis=-1).astype(np.uint8))
|
| 9 |
+
return grayscale_image
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
description_markdown = """
|
| 13 |
+
# Fake Face Detection tool from TrustWorthy BiometraVision Lab IISER Bhopal
|
| 14 |
+
|
| 15 |
+
## Usage
|
| 16 |
+
This tool expects a face image as input. Upon submission, it will process the image and provide an output with bounding boxes drawn on the face. Alongside the visual markers, the tool will give a detection result indicating whether the face is fake or real.
|
| 17 |
+
|
| 18 |
+
## Disclaimer
|
| 19 |
+
Please note that this tool is for research purposes only and may not always be 100% accurate. Users are advised to exercise discretion and supervise the tool's usage accordingly.
|
| 20 |
+
|
| 21 |
+
## Licensing and Permissions
|
| 22 |
+
This tool has been developed solely for research and demonstrative purposes. Any commercial utilization of this tool is strictly prohibited unless explicit permission has been obtained from the developers.
|
| 23 |
+
|
| 24 |
+
## Developer Contact
|
| 25 |
+
For further inquiries or permissions, you can reach out to the developer through the following social media accounts:
|
| 26 |
+
- [LAB Webpage](https://sites.google.com/iiitd.ac.in/agarwalakshay/labiiserb?authuser=0)
|
| 27 |
+
- [LinkedIn](https://www.linkedin.com/in/shivam-shukla-0a50ab1a2/)
|
| 28 |
+
- [GitHub](https://github.com/SaShukla090)
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Create the Gradio app
|
| 35 |
+
app = gr.Interface(
|
| 36 |
+
fn=detect_and_label_faces,
|
| 37 |
+
inputs=gr.Image(type="pil"),
|
| 38 |
+
outputs="image",
|
| 39 |
+
# examples=[
|
| 40 |
+
# "path_to_example_image_1.jpg",
|
| 41 |
+
# "path_to_example_image_2.jpg"
|
| 42 |
+
# ]
|
| 43 |
+
examples=[
|
| 44 |
+
os.path.join("Examples", image_name) for image_name in os.listdir("Examples")
|
| 45 |
+
],
|
| 46 |
+
title="Fake Face Detection",
|
| 47 |
+
description=description_markdown,
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Run the app
|
| 51 |
+
app.launch()
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
# import torch.nn.functional as F
|
| 86 |
+
# import torch
|
| 87 |
+
# import torch.nn as nn
|
| 88 |
+
# import torch.optim as optim
|
| 89 |
+
# from torch.utils.data import DataLoader
|
| 90 |
+
# from sklearn.metrics import accuracy_score, precision_recall_fscore_support
|
| 91 |
+
# from torch.optim.lr_scheduler import CosineAnnealingLR
|
| 92 |
+
# from tqdm import tqdm
|
| 93 |
+
# import warnings
|
| 94 |
+
# warnings.filterwarnings("ignore")
|
| 95 |
+
|
| 96 |
+
# from utils.config import cfg
|
| 97 |
+
# from dataset.real_n_fake_dataloader import Extracted_Frames_Dataset
|
| 98 |
+
# from utils.data_transforms import get_transforms_train, get_transforms_val
|
| 99 |
+
# from net.Multimodalmodel import Image_n_DCT
|
| 100 |
+
# import gradio as gr
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
# import os
|
| 106 |
+
# import json
|
| 107 |
+
# import torch
|
| 108 |
+
# from torchvision import transforms
|
| 109 |
+
# from torch.utils.data import DataLoader, Dataset
|
| 110 |
+
# from PIL import Image
|
| 111 |
+
# import numpy as np
|
| 112 |
+
# import pandas as pd
|
| 113 |
+
# import cv2
|
| 114 |
+
# import argparse
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
# from sklearn.metrics import classification_report, confusion_matrix
|
| 122 |
+
# import matplotlib.pyplot as plt
|
| 123 |
+
# import seaborn as sns
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
# class Test_Dataset(Dataset):
|
| 130 |
+
# def __init__(self, test_data_path = None, transform = None, image = None):
|
| 131 |
+
# """
|
| 132 |
+
# Args:
|
| 133 |
+
# returns:
|
| 134 |
+
# """
|
| 135 |
+
|
| 136 |
+
# if test_data_path is None and image is not None:
|
| 137 |
+
# self.dataset = [(image, 2)]
|
| 138 |
+
# self.transform = transform
|
| 139 |
+
|
| 140 |
+
# def __len__(self):
|
| 141 |
+
# return len(self.dataset)
|
| 142 |
+
|
| 143 |
+
# def __getitem__(self, idx):
|
| 144 |
+
# sample_input = self.get_sample_input(idx)
|
| 145 |
+
# return sample_input
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
# def get_sample_input(self, idx):
|
| 149 |
+
# rgb_image = self.get_rgb_image(self.dataset[idx][0])
|
| 150 |
+
# dct_image = self.compute_dct_color(self.dataset[idx][0])
|
| 151 |
+
# # label = self.get_label(idx)
|
| 152 |
+
# sample_input = {"rgb_image": rgb_image, "dct_image": dct_image}
|
| 153 |
+
|
| 154 |
+
# return sample_input
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
# def get_rgb_image(self, rgb_image):
|
| 158 |
+
# # rgb_image_path = self.dataset[idx][0]
|
| 159 |
+
# # rgb_image = Image.open(rgb_image_path)
|
| 160 |
+
# if self.transform:
|
| 161 |
+
# rgb_image = self.transform(rgb_image)
|
| 162 |
+
# return rgb_image
|
| 163 |
+
|
| 164 |
+
# def get_dct_image(self, idx):
|
| 165 |
+
# rgb_image_path = self.dataset[idx][0]
|
| 166 |
+
# rgb_image = cv2.imread(rgb_image_path)
|
| 167 |
+
# dct_image = self.compute_dct_color(rgb_image)
|
| 168 |
+
# if self.transform:
|
| 169 |
+
# dct_image = self.transform(dct_image)
|
| 170 |
+
|
| 171 |
+
# return dct_image
|
| 172 |
+
|
| 173 |
+
# def get_label(self, idx):
|
| 174 |
+
# return self.dataset[idx][1]
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
# def compute_dct_color(self, image):
|
| 178 |
+
# image_float = np.float32(image)
|
| 179 |
+
# dct_image = np.zeros_like(image_float)
|
| 180 |
+
# for i in range(3):
|
| 181 |
+
# dct_image[:, :, i] = cv2.dct(image_float[:, :, i])
|
| 182 |
+
# if self.transform:
|
| 183 |
+
# dct_image = self.transform(dct_image)
|
| 184 |
+
# return dct_image
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
# device = torch.device("cpu")
|
| 188 |
+
# # print(device)
|
| 189 |
+
# model = Image_n_DCT()
|
| 190 |
+
# model.load_state_dict(torch.load('weights/best_model.pth', map_location = device))
|
| 191 |
+
# model.to(device)
|
| 192 |
+
# model.eval()
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
# def classify(image):
|
| 196 |
+
# test_dataset = Test_Dataset(transform = get_transforms_val(), image = image)
|
| 197 |
+
# inputs = test_dataset[0]
|
| 198 |
+
# rgb_image, dct_image = inputs['rgb_image'].to(device), inputs['dct_image'].to(device)
|
| 199 |
+
# output = model(rgb_image.unsqueeze(0), dct_image.unsqueeze(0))
|
| 200 |
+
# # _, predicted = torch.max(output.data, 1)
|
| 201 |
+
# # print(f"the face is {'real' if predicted==1 else 'fake'}")
|
| 202 |
+
# return {'Fake': output[0][0], 'Real': output[0][1]}
|
| 203 |
+
|
| 204 |
+
# iface = gr.Interface(fn=classify, inputs="image", outputs="label")
|
| 205 |
+
# if __name__ == "__main__":
|
| 206 |
+
# iface.launch()
|