Spaces:
Runtime error
Runtime error
File size: 11,430 Bytes
8b5a765 40bddee 8b5a765 167ec34 40bddee 870e05e 40bddee 0532f2d 40bddee ae45eb6 40bddee 167ec34 40bddee 8b5a765 40bddee 8b5a765 40bddee fc084ee 40bddee fc85470 fc084ee f0283ee 40bddee 7e1f5d7 dea5dd7 bd9dbb6 fc084ee bd9dbb6 fc084ee bd9dbb6 fc084ee bd9dbb6 a3f864c bd9dbb6 fc084ee 6866042 fc084ee 0532f2d fa21e4e 0532f2d fc084ee 40bddee 1829368 40bddee |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
from turn import get_ice_servers
import cv2
import numpy as np
import time
import math
import streamlit as st
import av
from tensorflow.keras.models import load_model
from scipy.signal import convolve2d
from skimage import color
from skimage import io
from sklearn.metrics import accuracy_score
# VECTORIZATION the u factor
import matplotlib.pyplot as plt
import os
from PIL import Image
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv1D, MaxPooling1D, Flatten, Dropout
from tensorflow.keras.optimizers import Adam
from streamlit_webrtc import webrtc_streamer
import dlib
num_bins = 256
# Load the model
model = load_model('best_model_HQ_enhanced_v12.h5')
# model2 = load_model('best_model_HQ_v9.h5')
# model3 = load_model('best_model_HQ_enhanced_v12.h5')
def u_sliding_factor(image_channel, P):
result = np.zeros(image_channel.shape, np.float32)
# Define the sliding window size
window_size = (3, 3)
# Create the convolution kernel
kernel = np.ones(window_size, np.float32)
kernel[1, 1] = 0
kernel = kernel / (2 * P)
kernal2 = np.zeros(window_size, np.float32)
kernal2[1, 1] = 1
kernal2 = kernal2 / 2
# Perform the convolution using scipy's convolve2d
convolution_matrix = cv2.filter2D(image_channel, -1, kernel) + cv2.filter2D(image_channel, -1, kernal2)
result = convolution_matrix[1:-1, 1:-1]
return result.astype(np.float32)
def C_list_calculate(P):
C = []
for count in range(1, 9):
c_value = ((P - count) * (count - 1)) / math.floor(((P - 1) / 2)**2)
C.append(c_value)
return C
def ED_LBP_Sliding_Matrix(I, P):
# Define the amount of padding
padding_amount = 1
# Pad the array with zeros
I = np.pad(I, pad_width=padding_amount, mode='constant')
K = (2**P) - 1
C_list = C_list_calculate(8)
u_fac_matrix = u_sliding_factor(I.astype(np.float32), P)
slid_factor = np.zeros((u_fac_matrix.shape), np.float32)
m, n = u_fac_matrix.shape
ED_LBP = np.zeros(u_fac_matrix.shape, np.float32)
ED_LBP_matrix = np.zeros((u_fac_matrix.shape), np.float32)
K_matrix = np.ones(u_fac_matrix.shape).astype(np.float32) * K
offsets = [(0, 1), (0, 2), (1, 2), (2, 2), (2, 1), (2, 0), (1, 0), (0, 0)]
count = 1
for offset in offsets:
row_offset, col_offset = offset
sliding_matrix = I[row_offset:row_offset + m, col_offset:col_offset + n].astype(np.float32) - u_fac_matrix.astype(np.float32)
slid_factor = np.maximum(sliding_matrix, 0).astype(np.float32)
k_norm = K_matrix.astype(np.float32) - u_fac_matrix.astype(np.float32)
k_norm_nonzero = np.where(k_norm == 0, 1e-10, k_norm)
A_factor = np.where(k_norm != 0, slid_factor / k_norm_nonzero, 0)
ED_LBP_matrix = (A_factor.astype(np.float32) * C_list[count - 1]) + np.ones(A_factor.shape).astype(np.float32)
ED_LBP = ED_LBP + np.where(sliding_matrix >= 0, 2**((count - 1) * ED_LBP_matrix.astype(np.float32)), 0)
count = count + 1
ED_LBP = np.where(ED_LBP > 255, 255, np.round(ED_LBP))
return ED_LBP.astype(int)
def compute_histogram(image, num_bins):
hist = cv2.calcHist([image], [0], None, [num_bins], [0, num_bins])
hist = hist / hist.sum() # Normalize the histogram
return hist
def spatial_pyramid(image, num_bins):
ED_LBP_image = np.zeros((image.shape), np.int16)
num_channels = image.shape[2]
histograms = []
for channel in range(num_channels):
ED_LBP_image[:, :, channel] = ED_LBP_Sliding_Matrix(image[:, :, channel].astype(np.int16), 8)
# Level 0: Compute histogram for the entire channel
H1_channel = compute_histogram(ED_LBP_image[:, :, channel].astype(np.uint8), num_bins).ravel()
# Level 2: Compute histograms for 4x4 grids
grid_size = 4
H2_channel = np.empty((grid_size, grid_size, num_bins))
grid_height, grid_width = ED_LBP_image[:, :, channel].shape[0] // grid_size, ED_LBP_image[:, :, channel].shape[1] // grid_size
for m in range(grid_size):
for n in range(grid_size):
grid_image = ED_LBP_image[m * grid_height: (m + 1) * grid_height,
n * grid_width: (n + 1) * grid_width, channel]
H2_channel[m, n] = compute_histogram(grid_image.astype(np.uint8), num_bins).ravel()
H2_channel = H2_channel.reshape(-1)
# Concatenate histograms from level 0 and level 2
Hs_channel = np.concatenate((H1_channel, H2_channel))
histograms.append(Hs_channel)
# Concatenate histograms from all channels
feature_vector = np.concatenate(histograms)
return feature_vector
# Add custom CSS styles
st.markdown(
"""
<style>
.st-title {
font-size: 24px; /* Larger font for the title */
text-align: center;
}
.st-text {
font-size: 16px; /* Smaller font for the text */
text-align: center;
margin: 10px 0;
}
.st-button {
font-size: 20px;
}
.centered {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
</style>
""",
unsafe_allow_html=True
)
# Define the app title
st.markdown("<h1 class='st-title'>نظام كشف التزييف</h1>", unsafe_allow_html=True)
st.markdown("<p class='st-text'>قم بقراءة شروط الاستخدام في الاسفل قبل استخدام النظام</p>", unsafe_allow_html=True)
picture = st.camera_input("Take a picture")
if picture:
bytes_data = picture.getvalue()
frm = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
img_h, img_w, img_c = frm.shape
frm = cv2.flip(frm,1)
features_list=[]
features_list2=[]
# Check if there are face landmarks detected
gray = cv2.cvtColor(frm, cv2.COLOR_BGR2GRAY)
average_brightness = cv2.mean(gray)[0]
# st.text(str(average_brightness))
if average_brightness < 100:
st.text("إضاءة غير جيدة")
st.text("انتقل الى مكان جيد الإضاءة")
else:
# Initialize the face detector
faceDetector = dlib.get_frontal_face_detector()
# Assuming 'frm' is your input image
faces = faceDetector(frm, 0)
# Check if there are any faces detected
if len(faces) > 0:
largest_face = None
max_area = 0
# Find the largest face
for face in faces:
w = face.right() - face.left()
h = face.bottom() - face.top()
area = w * h
if area > max_area:
max_area = area
largest_face = face
# If a largest face is detected
if largest_face is not None:
# Calculate the expanded dimensions
expansion_factor = 1.5
x = largest_face.left()
y = largest_face.top()
w = largest_face.right() - x
h = largest_face.bottom() - y
expanded_x = max(0, int(x - w * (expansion_factor - 1) / 2))
expanded_y = max(0, int(y - h * (expansion_factor - 1) / 2))
expanded_w = min(frm.shape[1], int(w * expansion_factor))
expanded_h = min(frm.shape[0], int(h * expansion_factor))
# Adjust the width and height to avoid going beyond image boundaries
expanded_w = min(expanded_w, frm.shape[1] - expanded_x)
expanded_h = min(expanded_h, frm.shape[0] - expanded_y)
# Extract the expanded face area
# face_img = frm[expanded_y:expanded_y + expanded_h, expanded_x:expanded_x + expanded_w]
face_img = frm[y:y + h, x:x + w]
# Now, 'face_img' contains the largest face. You can process it further as needed.
if np.array(face_img).size != 0 :
target_size = (512, 512)
resized_face = cv2.resize(face_img, target_size)
# Perform spatial pyramid feature extraction
rgb_features = spatial_pyramid(cv2.cvtColor(resized_face, cv2.COLOR_BGR2RGB), num_bins)
hsv_features = spatial_pyramid(cv2.cvtColor(resized_face, cv2.COLOR_BGR2HSV), num_bins)
ycbcr_features = spatial_pyramid(cv2.cvtColor(resized_face, cv2.COLOR_BGR2YCrCb), num_bins)
if rgb_features.size > 0 and hsv_features.size > 0 and ycbcr_features.size > 0:
combined_features = np.concatenate((rgb_features, hsv_features, ycbcr_features))
features_list.append(combined_features)
if len(features_list) > 0:
X_array = np.array(features_list)
# print(X_array.shape)
X_test_array_reshaped = np.expand_dims(X_array, axis=-1)
# prediction = model3.predict(X_test_array_reshaped)
# prediction2 = model2.predict(X_test_array_reshaped)
prediction3 = model.predict(X_test_array_reshaped)
if prediction3 >=0.5:
st.text("Real")
st.text(str(prediction3[0]))
else:
st.text("Fack")
st.text(str(prediction3[0]))
# if prediction >= 0.85 and prediction2 >= 0.0000000000001:
# st.text("صورة حقيقية")
# # st.text(str(prediction[0]))
# # st.text(str(prediction2[0]))
# else:
# st.text("صورة مزيفة")
# st.text(str(prediction[0]))
# # st.text(str(prediction2[0]))
else:
st.text("لا يوجد وجه")
else:
st.text("لا يوجد وجه")
# Provide guidance for users
st.markdown("<p class='st-text'>في حالة حصلت على نتيجة غير حقيقية احرص على تحقيق الشروط في الاسفل</p>", unsafe_allow_html=True)
st.image(picture)
# Define the app title
st.markdown("<h1 class='st-title'>شروط الاستخدام</h1>", unsafe_allow_html=True)
# Add informative text with centered alignment
st.markdown("<p class='st-text'>يجب توفر إضاءة جيدة</p>", unsafe_allow_html=True)
st.markdown("<p class='st-text'>يجب استخدام كاميرا هاتف بدقة جيدة</p>", unsafe_allow_html=True)
st.markdown("<p class='st-text'>يجب ان يكون الوجه مقابلا للشاشة بشكل مستقيم</p>", unsafe_allow_html=True)
st.markdown("<p class='st-text'>التركيز على مكان الكاميرا عند الالتقاط</p>", unsafe_allow_html=True)
st.markdown("<p class='st-text'>الحرص على ان لا يكون خلفك خلفية تعكس الضوء مثل الزجاج</p>", unsafe_allow_html=True)
st.markdown("<p class='st-text'>يفضل ان يكون خلفك خلفية صلدة مثل الجدار</p>", unsafe_allow_html=True) |