Download infrerence_examples/keras_inference_example.py from RunningPie/CLARIS: direct link, hf CLI and curl.
- Browser
- Download file 2.03 kB
-
https://huggingface.co/RunningPie/CLARIS/resolve/main/infrerence_examples/keras_inference_example.py
- Command line
-
hf download hf://RunningPie/CLARIS/infrerence_examples/keras_inference_example.py
-
curl -L -o keras_inference_example.py https://huggingface.co/RunningPie/CLARIS/resolve/main/infrerence_examples/keras_inference_example.py
2.03 kB
| import tensorflow as tf | |
| import pickle | |
| import numpy as np | |
| import pandas as pd | |
| def load_model_and_processor(): | |
| """Load the trained model and processor.""" | |
| # Load the complete model | |
| model = tf.keras.models.load_model('model.h5') | |
| # Load the processor | |
| with open('processor.pkl', 'rb') as f: | |
| processor = pickle.load(f) | |
| return model, processor | |
| def predict_sign(model, processor, landmark_data): | |
| """ | |
| Predict sign from landmark data. | |
| Args: | |
| model: Loaded Keras model | |
| processor: SignLanguageProcessor instance | |
| landmark_data: DataFrame with columns ['frame', 'row_id', 'x', 'y', 'z'] | |
| Returns: | |
| predicted_class: Predicted sign class | |
| confidence: Prediction confidence | |
| """ | |
| # Process the landmark data | |
| X, _ = processor.process_dataset(landmark_data) | |
| if len(X) == 0: | |
| return None, 0.0 | |
| # Make prediction | |
| predictions = model.predict(X) | |
| predicted_class = np.argmax(predictions, axis=1)[0] | |
| confidence = np.max(predictions, axis=1)[0] | |
| # Convert back to sign name if mapping exists | |
| if hasattr(processor, 'index_to_sign'): | |
| sign_name = processor.index_to_sign[predicted_class] | |
| return sign_name, confidence | |
| return predicted_class, confidence | |
| # Example usage | |
| if __name__ == "__main__": | |
| # Load model and processor | |
| model, processor = load_model_and_processor() | |
| # Example landmark data (replace with your actual data) | |
| # landmark_data = pd.read_csv('your_landmark_data.csv') | |
| # Make prediction | |
| # predicted_sign, confidence = predict_sign(model, processor, landmark_data) | |
| # print(f"Predicted sign: {predicted_sign}, Confidence: {confidence:.3f}") | |
| print("Model and processor loaded successfully!") | |
| print(f"Model input shape: {model.input_shape}") | |
| print(f"Model output shape: {model.output_shape}") | |
| print(f"Number of classes: {processor.sign_count}") |