akhfzl commited on
Commit
65217d6
·
1 Parent(s): 60b019f

update-logic

Browse files
.gitignore CHANGED
@@ -1,3 +1,2 @@
1
  __pycache__/
2
- /users/*
3
- !/users/face_features.csv
 
1
  __pycache__/
2
+ /users/*
 
faceVerificationUtilization/__pycache__/setConfig.cpython-313.pyc CHANGED
Binary files a/faceVerificationUtilization/__pycache__/setConfig.cpython-313.pyc and b/faceVerificationUtilization/__pycache__/setConfig.cpython-313.pyc differ
 
faceVerificationUtilization/__pycache__/utils.cpython-313.pyc CHANGED
Binary files a/faceVerificationUtilization/__pycache__/utils.cpython-313.pyc and b/faceVerificationUtilization/__pycache__/utils.cpython-313.pyc differ
 
faceVerificationUtilization/setConfig.py CHANGED
@@ -44,6 +44,4 @@ transform = transforms.Compose([
44
  transforms.ToTensor(),
45
  transforms.Normalize([0.485, 0.456, 0.406],
46
  [0.229, 0.224, 0.225])
47
- ])
48
-
49
- faiss_index, labels, db = load_db("users/face_features.csv")
 
44
  transforms.ToTensor(),
45
  transforms.Normalize([0.485, 0.456, 0.406],
46
  [0.229, 0.224, 0.225])
47
+ ])
 
 
faceVerificationUtilization/utils.py CHANGED
@@ -3,7 +3,7 @@ import numpy as np
3
  from PIL import Image
4
  import torch
5
  import pandas as pd
6
- from .setConfig import efficientnet_model, face_detector, transform, pca_xgb, faiss, faiss_index, labels, db
7
  import os
8
 
9
  def ImgPreprocessing(img):
@@ -17,7 +17,8 @@ def ImgPreprocessing(img):
17
  return img
18
 
19
  def YoloFaceDetection(img):
20
- results = face_detector.predict(img, conf=0.8)
 
21
  keyReturn = {
22
  'message': "",
23
  'status': False,
@@ -28,8 +29,11 @@ def YoloFaceDetection(img):
28
  keyReturn['message'] = "Tidak ada wajah terdeteksi"
29
  return keyReturn
30
 
31
- box = results[0].boxes[0]
32
- x1, y1, x2, y2 = map(int, box.xyxy[0].cpu().numpy())
 
 
 
33
 
34
  keyReturn['message'] = "Face detected"
35
  keyReturn['status'] = True
@@ -41,9 +45,10 @@ def FaceValidationPredict(**face_crop):
41
  x1, y1, x2, y2 = face_crop['coordinate']
42
  face_crop = face_crop['img'][y1:y2, x1:x2]
43
 
44
- face_crop = cv.cvtColor(face_crop, cv.COLOR_BGR2RGB)
45
-
46
  face_pil = Image.fromarray(face_crop)
 
47
  face_tensor = transform(face_pil).unsqueeze(0)
48
 
49
  with torch.no_grad():
@@ -51,7 +56,7 @@ def FaceValidationPredict(**face_crop):
51
 
52
  pred = pca_xgb.predict(features)[0]
53
 
54
- return pred, features
55
 
56
  def FaceValidation(frame: np.ndarray):
57
  if frame is None:
@@ -70,7 +75,7 @@ def FaceValidation(frame: np.ndarray):
70
  results = keyReturn['coordinate']
71
  x1, y1, x2, y2 = results
72
 
73
- validation, features = FaceValidationPredict(coordinate=[x1, y1, x2, y2], img=img)
74
  pred = 'Wajah Valid' if validation == 0 else 'Wajah Tidak Valid'
75
 
76
  return f"Predicted class: {pred}"
@@ -90,13 +95,13 @@ def FaceRecord(img, name, photo_idx):
90
  return keyReturn['message']
91
 
92
  x1, y1, x2, y2 = keyReturn['coordinate']
93
- pred, features = FaceValidationPredict(coordinate=[x1, y1, x2, y2], img=img)
94
 
95
  if pred == 1:
96
  return f"[Foto {photo_idx}] Gagal: wajah tidak valid"
97
 
98
  save_path = os.path.join(user_dir, f"photo_{photo_idx}.jpg")
99
- cv.imwrite(save_path, img)
100
 
101
  csv_path = "users/face_features.csv"
102
  row = pd.DataFrame({
@@ -130,12 +135,14 @@ def Recognize(frame: np.ndarray):
130
 
131
  results = keyReturn['coordinate']
132
  x1, y1, x2, y2 = results
133
- pred, features = FaceValidationPredict(coordinate=[x1, y1, x2, y2], img=img)
134
  faiss.normalize_L2(features)
135
 
136
  if pred == 1:
137
  return 'Wajah tidak terdeteksi'
138
 
 
 
139
  if faiss_index is None:
140
  return "Database kosong"
141
 
@@ -143,7 +150,7 @@ def Recognize(frame: np.ndarray):
143
  score = float(D[0][0])
144
  idx = int(I[0][0])
145
 
146
- if score < 0.7:
147
  return f"Tidak dikenali"
148
  else:
149
  return f"Terkenali sebagai: {labels[idx]} - (score={score:.2f})"
 
3
  from PIL import Image
4
  import torch
5
  import pandas as pd
6
+ from .setConfig import efficientnet_model, face_detector, transform, pca_xgb, faiss, load_db
7
  import os
8
 
9
  def ImgPreprocessing(img):
 
17
  return img
18
 
19
  def YoloFaceDetection(img):
20
+ results = face_detector.predict(img, conf=0.7)
21
+
22
  keyReturn = {
23
  'message': "",
24
  'status': False,
 
29
  keyReturn['message'] = "Tidak ada wajah terdeteksi"
30
  return keyReturn
31
 
32
+ boxes = results[0].boxes.xyxy.cpu().numpy()
33
+ confs = results[0].boxes.conf.cpu().numpy()
34
+
35
+ max_idx = confs.argmax()
36
+ x1, y1, x2, y2 = map(int, boxes[max_idx])
37
 
38
  keyReturn['message'] = "Face detected"
39
  keyReturn['status'] = True
 
45
  x1, y1, x2, y2 = face_crop['coordinate']
46
  face_crop = face_crop['img'][y1:y2, x1:x2]
47
 
48
+ face_crop = cv.cvtColor(face_crop, cv.COLOR_BGR2GRAY)
49
+ face_crop = cv.cvtColor(face_crop, cv.COLOR_GRAY2RGB)
50
  face_pil = Image.fromarray(face_crop)
51
+
52
  face_tensor = transform(face_pil).unsqueeze(0)
53
 
54
  with torch.no_grad():
 
56
 
57
  pred = pca_xgb.predict(features)[0]
58
 
59
+ return pred, features, face_crop
60
 
61
  def FaceValidation(frame: np.ndarray):
62
  if frame is None:
 
75
  results = keyReturn['coordinate']
76
  x1, y1, x2, y2 = results
77
 
78
+ validation, features, face_crop = FaceValidationPredict(coordinate=[x1, y1, x2, y2], img=img)
79
  pred = 'Wajah Valid' if validation == 0 else 'Wajah Tidak Valid'
80
 
81
  return f"Predicted class: {pred}"
 
95
  return keyReturn['message']
96
 
97
  x1, y1, x2, y2 = keyReturn['coordinate']
98
+ pred, features, face_crop = FaceValidationPredict(coordinate=[x1, y1, x2, y2], img=img)
99
 
100
  if pred == 1:
101
  return f"[Foto {photo_idx}] Gagal: wajah tidak valid"
102
 
103
  save_path = os.path.join(user_dir, f"photo_{photo_idx}.jpg")
104
+ cv.imwrite(save_path, face_crop)
105
 
106
  csv_path = "users/face_features.csv"
107
  row = pd.DataFrame({
 
135
 
136
  results = keyReturn['coordinate']
137
  x1, y1, x2, y2 = results
138
+ pred, features, face_crop = FaceValidationPredict(coordinate=[x1, y1, x2, y2], img=img)
139
  faiss.normalize_L2(features)
140
 
141
  if pred == 1:
142
  return 'Wajah tidak terdeteksi'
143
 
144
+ faiss_index, labels, db = load_db("users/face_features.csv")
145
+
146
  if faiss_index is None:
147
  return "Database kosong"
148
 
 
150
  score = float(D[0][0])
151
  idx = int(I[0][0])
152
 
153
+ if score < 0.65:
154
  return f"Tidak dikenali"
155
  else:
156
  return f"Terkenali sebagai: {labels[idx]} - (score={score:.2f})"
users/face_features.csv CHANGED
The diff for this file is too large to render. See raw diff