Download predict_utils.py from UmReh/SachAI: direct link, hf CLI and curl.
- Browser
- Download file 1.79 kB
-
https://huggingface.co/spaces/UmReh/SachAI/resolve/main/predict_utils.py
- Command line
-
hf download hf://spaces/UmReh/SachAI/predict_utils.py
-
curl -L -o predict_utils.py https://huggingface.co/spaces/UmReh/SachAI/resolve/main/predict_utils.py
1.79 kB
| import numpy as np | |
| import cv2 | |
| from tensorflow.keras.models import load_model | |
| # β LOAD MODEL | |
| model = load_model("custom_cnn_model.h5") | |
| # β LOAD FACE DETECTOR | |
| face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") | |
| def detect_face(frame): | |
| gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
| faces = face_cascade.detectMultiScale( | |
| gray, | |
| scaleFactor=1.1, | |
| minNeighbors=3, | |
| minSize=(30, 30) | |
| ) | |
| print("Faces detected:", len(faces)) | |
| # β If no face β use full frame | |
| if len(faces) == 0: | |
| return frame | |
| x, y, w, h = faces[0] | |
| face = frame[y:y+h, x:x+w] | |
| # β Safety check | |
| if face is None or face.size == 0: | |
| return frame | |
| return face | |
| def preprocess_frame(frame): | |
| img = cv2.resize(frame, (128, 128)) | |
| img = img / 255.0 | |
| img = np.reshape(img, (1, 128, 128, 3)) | |
| return img | |
| def predict_frame(frame): | |
| # π₯ Step 1: detect face or fallback | |
| face_or_frame = detect_face(frame) | |
| # π₯ Step 2: preprocess | |
| img = preprocess_frame(face_or_frame) | |
| # π₯ Step 3: predict | |
| prediction = model.predict(img, verbose=0)[0][0] | |
| if prediction > 0.5: | |
| return "REAL", float(1 - prediction) | |
| else: | |
| return "FAKE", float(prediction) | |
| def aggregate_results(results): | |
| fake_scores = [s for l, s in results if l == "FAKE"] | |
| real_scores = [s for l, s in results if l == "REAL"] | |
| if len(fake_scores) > len(real_scores): | |
| return { | |
| "result": "FAKE", | |
| "confidence": sum(fake_scores) / len(results) | |
| } | |
| else: | |
| return { | |
| "result": "REAL", | |
| "confidence": sum(real_scores) / len(results) | |
| } |