From 9169b73196a48583bacdd6e12b654574007f0492 Mon Sep 17 00:00:00 2001 From: retoor Date: Wed, 4 Dec 2024 20:10:06 +0000 Subject: [PATCH] Update train_model_to_recognize_patterns.md --- train_model_to_recognize_patterns.md | 35 ++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/train_model_to_recognize_patterns.md b/train_model_to_recognize_patterns.md index dddf7c1..525bf28 100644 --- a/train_model_to_recognize_patterns.md +++ b/train_model_to_recognize_patterns.md @@ -57,5 +57,36 @@ model.fit(np.array(X_train), np.array(y_train), epochs=10, batch_size=32) print("Model trained!") ``` - - +## Using the recognition model +```python +import cv2 +from tensorflow.keras.models import load_model + +# Load trained model from disk +model_path = 'path/to/trained/model.h5' +model = load_model(model_path) + +# Capture video from webcam or load pre-existing image +cap = cv2.VideoCapture(0) +while True: + ret, frame = cap.read() + if not ret: + break + + # Convert frame to grayscale and resize to 224x224 pixels + gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + resized_frame = cv2.resize(gray_frame, (224, 224)) + + # Add batch dimension for prediction + input_data = np.array([resized_frame]) + + # Get output probabilities for each facial expression + output_probabilities = model.predict(input_data) + + # Print the top 3 predicted expressions with their probabilities + print(np.argsort(-output_probabilities[0])[:3], output_probabilities[0][:3]) + +# Release^[[A^[[A^[[A^[[A^[[A^[[A^[[A^[[A resources^[[A^[[A^[[A^[[A^[[A^[[A^[[A^[[A^[[A and^[[A^[[A^[[A exit +cap.release() +cv2.destroyAllWindows() +```