Spaces:
Sleeping
Sleeping
mjs-07 commited on
Commit ·
c933bef
1
Parent(s): 2177126
Remove deprecated image detection module
Browse files
backend/app/image_detection/__init__.py
DELETED
|
File without changes
|
backend/app/image_detection/detector.py
DELETED
|
@@ -1,35 +0,0 @@
|
|
| 1 |
-
from PIL import Image
|
| 2 |
-
|
| 3 |
-
from backend.app.image_detection.univfd_detector import UnivFDDetector
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
class ImageDetector:
|
| 7 |
-
|
| 8 |
-
def __init__(self):
|
| 9 |
-
|
| 10 |
-
print("Image Detector Initialized")
|
| 11 |
-
|
| 12 |
-
self.univfd = UnivFDDetector()
|
| 13 |
-
|
| 14 |
-
def predict(self, image_path):
|
| 15 |
-
|
| 16 |
-
image = Image.open(image_path)
|
| 17 |
-
|
| 18 |
-
width, height = image.size
|
| 19 |
-
image_format = image.format
|
| 20 |
-
|
| 21 |
-
probability = self.univfd.predict(image_path)
|
| 22 |
-
|
| 23 |
-
classification = (
|
| 24 |
-
"AI Generated"
|
| 25 |
-
if probability > 0.5
|
| 26 |
-
else "Human Generated"
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
return {
|
| 30 |
-
"classification": classification,
|
| 31 |
-
"ai_probability": round(probability * 100, 2),
|
| 32 |
-
"width": width,
|
| 33 |
-
"height": height,
|
| 34 |
-
"format": image_format
|
| 35 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
backend/app/image_detection/test_detector.py
DELETED
|
@@ -1,14 +0,0 @@
|
|
| 1 |
-
from pathlib import Path
|
| 2 |
-
from detector import ImageDetector
|
| 3 |
-
|
| 4 |
-
detector = ImageDetector()
|
| 5 |
-
|
| 6 |
-
image_path = (
|
| 7 |
-
Path(__file__).parent
|
| 8 |
-
/ "test_images"
|
| 9 |
-
/ "cat.jpeg"
|
| 10 |
-
)
|
| 11 |
-
|
| 12 |
-
result = detector.predict(str(image_path))
|
| 13 |
-
|
| 14 |
-
print(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
backend/app/image_detection/test_univfd.py
DELETED
|
@@ -1,9 +0,0 @@
|
|
| 1 |
-
from univfd_detector import UnivFDDetector
|
| 2 |
-
|
| 3 |
-
detector = UnivFDDetector()
|
| 4 |
-
|
| 5 |
-
result = detector.predict(
|
| 6 |
-
"backend/app/image_detection/test_images/cat.jpeg"
|
| 7 |
-
)
|
| 8 |
-
|
| 9 |
-
print(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
backend/app/image_detection/univfd_detector.py
DELETED
|
@@ -1,60 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
from PIL import Image
|
| 3 |
-
from torchvision import transforms
|
| 4 |
-
import sys
|
| 5 |
-
|
| 6 |
-
# Add UnivFD repo to path
|
| 7 |
-
sys.path.append(
|
| 8 |
-
r"D:\Projects\AI-claim-verifier\UniversalFakeDetect\UniversalFakeDetect"
|
| 9 |
-
)
|
| 10 |
-
|
| 11 |
-
from models.imagenet_models import ImagenetModel
|
| 12 |
-
from models.clip_models import CLIPModel
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
class UnivFDDetector:
|
| 16 |
-
|
| 17 |
-
def __init__(self):
|
| 18 |
-
|
| 19 |
-
print("Loading UnivFD...")
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
self.model = CLIPModel("ViT-L/14")
|
| 23 |
-
|
| 24 |
-
state_dict = torch.load(
|
| 25 |
-
r"D:\Projects\AI-claim-verifier\UniversalFakeDetect\UniversalFakeDetect\pretrained_weights\fc_weights.pth",
|
| 26 |
-
map_location="cpu"
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
self.model.fc.load_state_dict(state_dict)
|
| 30 |
-
|
| 31 |
-
self.model.eval()
|
| 32 |
-
|
| 33 |
-
self.transform = transforms.Compose([
|
| 34 |
-
transforms.CenterCrop(224),
|
| 35 |
-
transforms.ToTensor(),
|
| 36 |
-
transforms.Normalize(
|
| 37 |
-
mean=[0.485, 0.456, 0.406],
|
| 38 |
-
std=[0.229, 0.224, 0.225]
|
| 39 |
-
),
|
| 40 |
-
])
|
| 41 |
-
|
| 42 |
-
print("Model Loaded")
|
| 43 |
-
|
| 44 |
-
def predict(self, image_path):
|
| 45 |
-
|
| 46 |
-
image = Image.open(image_path).convert("RGB")
|
| 47 |
-
|
| 48 |
-
image = self.transform(image)
|
| 49 |
-
|
| 50 |
-
image = image.unsqueeze(0)
|
| 51 |
-
|
| 52 |
-
with torch.no_grad():
|
| 53 |
-
|
| 54 |
-
output = self.model(image)
|
| 55 |
-
|
| 56 |
-
probability = torch.sigmoid(
|
| 57 |
-
output
|
| 58 |
-
).item()
|
| 59 |
-
|
| 60 |
-
return probability
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|