Spaces:
Sleeping
Sleeping
Update pages/2_Player_Comparison_Throw_Image.py
Browse files
pages/2_Player_Comparison_Throw_Image.py
CHANGED
|
@@ -29,28 +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):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 45 |
-
resized_face = cv2.resize(face, (
|
| 46 |
flattened = resized_face.flatten().reshape(1, -1)
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
expected_features =
|
| 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 |
|
|
@@ -61,6 +67,7 @@ def detect_and_predict_face(image_file):
|
|
| 61 |
|
| 62 |
|
| 63 |
|
|
|
|
| 64 |
# Get player details from the dataset
|
| 65 |
def get_player_details(df, player_name):
|
| 66 |
return df[df['Player'] == player_name].iloc[0]
|
|
|
|
| 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 |
+
# Load image and convert to grayscale
|
| 38 |
image = Image.open(image_file).convert("RGB")
|
| 39 |
img_np = np.array(image)
|
| 40 |
gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
|
| 41 |
|
| 42 |
+
# Detect face
|
| 43 |
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
| 44 |
if len(faces) == 0:
|
| 45 |
+
return None, "⚠️ No face detected!"
|
| 46 |
|
| 47 |
+
# Crop face
|
| 48 |
x, y, w, h = faces[0]
|
| 49 |
face = gray[y:y+h, x:x+w]
|
| 50 |
|
| 51 |
+
# Resize to match training dimensions: 113x109
|
| 52 |
+
resized_face = cv2.resize(face, (113, 109)) # width x height = 12289
|
| 53 |
flattened = resized_face.flatten().reshape(1, -1)
|
| 54 |
|
| 55 |
+
# Ensure shape matches exactly
|
| 56 |
+
expected_features = 12289
|
| 57 |
if flattened.shape[1] > expected_features:
|
| 58 |
flattened = flattened[:, :expected_features]
|
| 59 |
elif flattened.shape[1] < expected_features:
|
|
|
|
| 60 |
padding = expected_features - flattened.shape[1]
|
| 61 |
flattened = np.pad(flattened, ((0, 0), (0, padding)), mode='constant')
|
| 62 |
|
|
|
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
+
|
| 71 |
# Get player details from the dataset
|
| 72 |
def get_player_details(df, player_name):
|
| 73 |
return df[df['Player'] == player_name].iloc[0]
|