DOMMETI commited on
Commit
48127bc
·
verified ·
1 Parent(s): 8c89d37

Update pages/2_Player_Comparison_Throw_Image.py

Browse files
pages/2_Player_Comparison_Throw_Image.py CHANGED
@@ -29,46 +29,34 @@ def get_target_shape(expected_features):
29
  return None
30
 
31
  # Predict player name from image
32
- def detect_and_predict_face(image_file, model):
33
- import cv2
34
- import numpy as np
35
- from PIL import Image
36
-
37
  image = Image.open(image_file).convert("RGB")
38
  img_np = np.array(image)
39
  gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
40
 
41
  faces = face_cascade.detectMultiScale(gray, 1.3, 5)
42
  if len(faces) == 0:
43
- return None, "⚠️ No face detected!"
44
 
45
  x, y, w, h = faces[0]
46
  face = gray[y:y+h, x:x+w]
47
 
48
- expected_features = model.named_steps['scaler'].n_features_in_
49
-
50
- # Resize to shape close to the expected features
51
- resized_face = cv2.resize(face, (113, 109)) # width x height
52
-
53
  flattened = resized_face.flatten().reshape(1, -1)
54
 
55
- # 🔧 Fix: if extra pixels due to rounding, trim them
 
56
  if flattened.shape[1] > expected_features:
57
  flattened = flattened[:, :expected_features]
58
  elif flattened.shape[1] < expected_features:
59
- return None, f"❌ Still too small after resizing. Got {flattened.shape[1]}"
60
-
61
- # Final sanity check
62
- if flattened.shape[1] != expected_features:
63
- return None, f"❌ Mismatch in feature count. Got {flattened.shape[1]}, expected {expected_features}"
64
-
65
- pred_label = model.predict(flattened)[0]
66
- return pred_label, None
67
-
68
-
69
-
70
-
71
 
 
 
 
72
 
73
 
74
 
 
29
  return None
30
 
31
  # Predict player name from image
32
+ def detect_and_predict_face(image_file):
 
 
 
 
33
  image = Image.open(image_file).convert("RGB")
34
  img_np = np.array(image)
35
  gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
36
 
37
  faces = face_cascade.detectMultiScale(gray, 1.3, 5)
38
  if len(faces) == 0:
39
+ return None, "No face detected!"
40
 
41
  x, y, w, h = faces[0]
42
  face = gray[y:y+h, x:x+w]
43
 
44
+ # Resize to 64x64 (what model expects)
45
+ resized_face = cv2.resize(face, (64, 64)) # width=64, height=64
 
 
 
46
  flattened = resized_face.flatten().reshape(1, -1)
47
 
48
+ # Force to 4096 features
49
+ expected_features = 64 * 64
50
  if flattened.shape[1] > expected_features:
51
  flattened = flattened[:, :expected_features]
52
  elif flattened.shape[1] < expected_features:
53
+ # pad with zeros
54
+ padding = expected_features - flattened.shape[1]
55
+ flattened = np.pad(flattened, ((0, 0), (0, padding)), mode='constant')
 
 
 
 
 
 
 
 
 
56
 
57
+ # Predict
58
+ pred_name = model.predict(flattened)[0]
59
+ return pred_name, None
60
 
61
 
62