EngAbod commited on
Commit
fc084ee
·
1 Parent(s): bcc1613

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -51
app.py CHANGED
@@ -16,9 +16,6 @@ from sklearn.metrics import accuracy_score
16
  # VECTORIZATION the u factor
17
  import matplotlib.pyplot as plt
18
  import os
19
- import torch
20
- import torchvision.transforms as transforms
21
- import torchvision.models as models
22
  from PIL import Image
23
  from tensorflow.keras.models import Sequential
24
  from tensorflow.keras.layers import Dense, Conv1D, MaxPooling1D, Flatten, Dropout
@@ -165,6 +162,7 @@ st.markdown("<p class='st-text'>قم بقراءة شروط الاستخدام ف
165
 
166
  picture = st.camera_input("Take a picture")
167
 
 
168
  if picture:
169
  bytes_data = picture.getvalue()
170
  frm = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
@@ -176,58 +174,64 @@ if picture:
176
  gray = cv2.cvtColor(frm, cv2.COLOR_BGR2GRAY)
177
  average_brightness = cv2.mean(gray)[0]
178
  # st.text(str(average_brightness))
 
179
  if average_brightness < 100:
180
  st.text("إضاءة غير جيدة")
181
  st.text("انتقل الى مكان جيد الإضاءة")
182
  else:
183
-
184
- # Detect faces using cascade classifier
185
- face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
186
- faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
187
- expansion_factor = 1.5
188
- num_bins = 256
189
- biggest_face = None
190
- biggest_area = 0
191
- target_size = (512,512)
192
- for (x, y, w, h) in faces:
193
- # Calculate the expanded dimensions
194
- expanded_x = max(0, int(x - (w * (expansion_factor - 1) / 2)))
195
- expanded_y = max(0, int(y - (h * (expansion_factor - 1) / 2)))
196
- expanded_w = min(img_w, int(w * expansion_factor))
197
- expanded_h = min(img_h, int(h * expansion_factor))
198
-
199
- # Crop the expanded face region from the frame
200
- current_area = expanded_w * expanded_h
201
- if current_area > biggest_area:
202
- biggest_area = current_area
203
- biggest_face = frm[expanded_y:expanded_y + expanded_h, expanded_x:expanded_x + expanded_w]
204
- # biggest_face = frm[y:y + h, x:x + w]
205
- resized_face = cv2.resize(biggest_face, target_size)
206
- if biggest_face is not None:
207
-
208
- # Perform spatial pyramid feature extraction
209
- rgb_features = spatial_pyramid(cv2.cvtColor(resized_face, cv2.COLOR_BGR2RGB), num_bins)
210
- hsv_features = spatial_pyramid(cv2.cvtColor(resized_face, cv2.COLOR_BGR2HSV), num_bins)
211
- ycbcr_features = spatial_pyramid(cv2.cvtColor(resized_face, cv2.COLOR_BGR2YCrCb), num_bins)
212
-
213
-
214
- if rgb_features.size > 0 and hsv_features.size > 0 and ycbcr_features.size > 0:
215
- combined_features = np.concatenate((rgb_features, hsv_features, ycbcr_features))
216
- features_list.append(combined_features)
217
- if len(features_list) > 0:
218
- X_array = np.array(features_list)
219
- # print(X_array.shape)
220
- X_test_array_reshaped = np.expand_dims(X_array, axis=-1)
221
- prediction = model.predict(X_test_array_reshaped)
222
- prediction2 = model2.predict(X_test_array_reshaped)
223
- if prediction >= 0.80 and prediction2 >= 0.00000000000001:
224
- st.text("صورة حقيقية")
225
- # st.text(str(prediction[0]))
226
- # st.text(str(prediction2[0]))
227
- else:
228
- st.text("صورة مزيفة")
229
- st.text(str(prediction[0]))
230
- # st.text(str(prediction2[0]))
 
 
 
 
 
231
  else:
232
  st.text("لا يوجد وجه")
233
  # Provide guidance for users
 
16
  # VECTORIZATION the u factor
17
  import matplotlib.pyplot as plt
18
  import os
 
 
 
19
  from PIL import Image
20
  from tensorflow.keras.models import Sequential
21
  from tensorflow.keras.layers import Dense, Conv1D, MaxPooling1D, Flatten, Dropout
 
162
 
163
  picture = st.camera_input("Take a picture")
164
 
165
+
166
  if picture:
167
  bytes_data = picture.getvalue()
168
  frm = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
 
174
  gray = cv2.cvtColor(frm, cv2.COLOR_BGR2GRAY)
175
  average_brightness = cv2.mean(gray)[0]
176
  # st.text(str(average_brightness))
177
+
178
  if average_brightness < 100:
179
  st.text("إضاءة غير جيدة")
180
  st.text("انتقل الى مكان جيد الإضاءة")
181
  else:
182
+ # Initialize the face detector
183
+ faceDetector = dlib.get_frontal_face_detector()
184
+
185
+ # Assuming 'frm' is your input image
186
+ faces = faceDetector(frm, 0)
187
+
188
+ # Check if there are any faces detected
189
+ if len(faces) > 0:
190
+ largest_face = None
191
+ max_area = 0
192
+
193
+ # Find the largest face
194
+ for face in faces:
195
+ w = face.right() - face.left()
196
+ h = face.bottom() - face.top()
197
+ area = w * h
198
+ if area > max_area:
199
+ max_area = area
200
+ largest_face = face
201
+
202
+ # If a largest face is detected
203
+ if largest_face is not None:
204
+ # Extract the largest face
205
+ face_img = frm[largest_face.top():largest_face.bottom(), largest_face.left():largest_face.right()]
206
+ # Now, 'face_img' contains the largest face. You can process it further as needed.
207
+ if np.array(face_img).size != 0 :
208
+ target_size = (512, 512)
209
+ resized_face = cv2.resize(face_img, target_size)
210
+ # Perform spatial pyramid feature extraction
211
+ rgb_features = spatial_pyramid(cv2.cvtColor(resized_face, cv2.COLOR_BGR2RGB), num_bins)
212
+ hsv_features = spatial_pyramid(cv2.cvtColor(resized_face, cv2.COLOR_BGR2HSV), num_bins)
213
+ ycbcr_features = spatial_pyramid(cv2.cvtColor(resized_face, cv2.COLOR_BGR2YCrCb), num_bins)
214
+
215
+
216
+ if rgb_features.size > 0 and hsv_features.size > 0 and ycbcr_features.size > 0:
217
+ combined_features = np.concatenate((rgb_features, hsv_features, ycbcr_features))
218
+ features_list.append(combined_features)
219
+ if len(features_list) > 0:
220
+ X_array = np.array(features_list)
221
+ # print(X_array.shape)
222
+ X_test_array_reshaped = np.expand_dims(X_array, axis=-1)
223
+ prediction = model.predict(X_test_array_reshaped)
224
+ prediction2 = model2.predict(X_test_array_reshaped)
225
+ if prediction >= 0.80 and prediction2 >= 0.00000000000001:
226
+ st.text("صورة حقيقية")
227
+ # st.text(str(prediction[0]))
228
+ # st.text(str(prediction2[0]))
229
+ else:
230
+ st.text("صورة مزيفة")
231
+ st.text(str(prediction[0]))
232
+ # st.text(str(prediction2[0]))
233
+ else:
234
+ st.text("لا يوجد وجه")
235
  else:
236
  st.text("لا يوجد وجه")
237
  # Provide guidance for users