Spaces:
Running
Running
Anish commited on
Commit ·
739e45e
1
Parent(s): 95858c6
[Feature Added] > frame_detector. This detects frames and spits out AI or REAL, based on the provided Video. Tested completely, with EDGE CASES.
Browse files- .gitignore +2 -1
- backend/app/ai/video/frame_detector.py +70 -0
- backend/requirements.txt +0 -0
.gitignore
CHANGED
|
@@ -36,4 +36,5 @@ folder_struct.txt
|
|
| 36 |
# Misc
|
| 37 |
docs/
|
| 38 |
AGENTS.md
|
| 39 |
-
to_be_implemented.md
|
|
|
|
|
|
| 36 |
# Misc
|
| 37 |
docs/
|
| 38 |
AGENTS.md
|
| 39 |
+
to_be_implemented.md
|
| 40 |
+
Testing
|
backend/app/ai/video/frame_detector.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from torchvision import models, transforms
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
import logging
|
| 7 |
+
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
+
|
| 10 |
+
class FrameDetector:
|
| 11 |
+
def __init__(self):
|
| 12 |
+
self.device = torch.device("cpu")
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
self.model = models.efficientnet_b0(weights=models.EfficientNet_B0_Weights.IMAGENET1K_V1)
|
| 16 |
+
|
| 17 |
+
for param in self.model.parameters():
|
| 18 |
+
param.requires_grad = False
|
| 19 |
+
|
| 20 |
+
num_ftrs = self.model.classifier[1].in_features
|
| 21 |
+
self.model.classifier[1] = nn.Linear(num_ftrs, 1)
|
| 22 |
+
|
| 23 |
+
self.model = self.model.to(self.device)
|
| 24 |
+
self.model.eval()
|
| 25 |
+
|
| 26 |
+
self.transforms = transforms.Compose([
|
| 27 |
+
transforms.Resize((224, 224)),
|
| 28 |
+
transforms.ToTensor(),
|
| 29 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 30 |
+
])
|
| 31 |
+
logger.info("EfficientNet-B0 CPU Inference Engine Initialized Successfully.")
|
| 32 |
+
|
| 33 |
+
except Exception as e:
|
| 34 |
+
logger.error(f"FATAL: Could not boot AI model: {str(e)}")
|
| 35 |
+
raise
|
| 36 |
+
|
| 37 |
+
def predict(self, frame: np.ndarray) -> float:
|
| 38 |
+
try:
|
| 39 |
+
"""
|
| 40 |
+
Flipping the frame matrix, since OpenCV reads
|
| 41 |
+
colors backwards (B, G, R). But PyTorch expects (R, G, B).
|
| 42 |
+
Hence the flipping of matrix
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
+
rgb_frame = frame[:, :, ::-1]
|
| 46 |
+
pil_image = Image.fromarray(rgb_frame)
|
| 47 |
+
|
| 48 |
+
input_tensor = self.transforms(pil_image).unsqueeze(0).to(self.device)
|
| 49 |
+
|
| 50 |
+
with torch.no_grad():
|
| 51 |
+
raw_score = self.model(input_tensor)
|
| 52 |
+
probability = torch.sigmoid(raw_score).item()
|
| 53 |
+
|
| 54 |
+
return probability
|
| 55 |
+
|
| 56 |
+
except Exception as e:
|
| 57 |
+
logger.error(f"AI Frame Prediction Crashed: {str(e)}")
|
| 58 |
+
return 0.0
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
_detector_singleton = FrameDetector()
|
| 62 |
+
except Exception:
|
| 63 |
+
_detector_singleton = None
|
| 64 |
+
|
| 65 |
+
def analyze_frame(frame: np.ndarray) -> float:
|
| 66 |
+
if _detector_singleton is None:
|
| 67 |
+
logger.error("AI Engine is offline. Returning safe default.")
|
| 68 |
+
return 0.0
|
| 69 |
+
|
| 70 |
+
return _detector_singleton.predict(frame)
|
backend/requirements.txt
CHANGED
|
Binary files a/backend/requirements.txt and b/backend/requirements.txt differ
|
|
|