File size: 1,568 Bytes
87c8b2d
 
 
 
54ebb43
87c8b2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import joblib
import pandas as pd
import numpy as np
from landmarks import normalize_landmarks, calculate_angles
import streamlit as st
@st.cache_resource
def load_model():
    """Load the pre-trained Random Forest model."""
    try:
        return joblib.load('best_random_forest_model.pkl')
    except Exception as e:
        st.error(f"Error loading model: {e}")
        return None

def process_and_predict(image, model):
    """
    Process the uploaded image to extract hand landmarks and predict the ASL sign.
    
    Parameters:
        image (numpy.ndarray): The uploaded image.
        model (sklearn.base.BaseEstimator): The pre-trained model.

    Returns:
        tuple: A tuple containing predicted probabilities and landmarks.
    """
    mp_hands = mp.solutions.hands
    with mp_hands.Hands(static_image_mode=True, max_num_hands=1, min_detection_confidence=0.5) as hands:
        image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        results = hands.process(image_rgb)
        
        if results.multi_hand_landmarks:
            landmarks = np.array([[lm.x, lm.y] for lm in results.multi_hand_landmarks[0].landmark])
            landmarks_normalized = normalize_landmarks(landmarks)
            angles = calculate_angles(landmarks_normalized)
            
            angle_columns = [f'angle_{i}' for i in range(len(angles))]
            angles_df = pd.DataFrame([angles], columns=angle_columns)
            
            probabilities = model.predict_proba(angles_df)[0]
            return probabilities, landmarks
    
    return None, None