Update train_model_to_recognize_patterns.md

This commit is contained in:
retoor 2024-12-04 20:10:06 +00:00
parent 9151e3d2ef
commit 9169b73196

View File

@ -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()
```