File size: 3,780 Bytes
dcb3bcc | 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | {
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"id": "4e992bb3",
"metadata": {},
"outputs": [
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[6], line 29\u001b[0m\n\u001b[0;32m 26\u001b[0m cap \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mVideoCapture(\u001b[38;5;241m0\u001b[39m)\n\u001b[0;32m 28\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m---> 29\u001b[0m ret, frame \u001b[38;5;241m=\u001b[39m \u001b[43mcap\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mread\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# Read a frame from the webcam\u001b[39;00m\n\u001b[0;32m 31\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m ret:\n\u001b[0;32m 32\u001b[0m \u001b[38;5;28;01mcontinue\u001b[39;00m\n",
"\u001b[1;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"import cv2\n",
"import numpy as np\n",
"from tensorflow import keras\n",
"from keras.preprocessing.image import img_to_array\n",
"\n",
"# Load your pre-trained model\n",
"model = keras.models.load_model('D:\\Trained model\\sign_language_model.h5') \n",
"\n",
"# Define a mapping from class indices to text labels\n",
"class_to_text_mapping = {\n",
" 0: 'A',\n",
" 1: 'B',\n",
" # Add more mapping as needed\n",
"}\n",
"\n",
"# Create a function to preprocess frames from the camera\n",
"def preprocess_frame(frame):\n",
" # Resize and normalize the frame\n",
" frame = cv2.resize(frame, (200, 200))\n",
" frame = frame.astype(\"float\") / 255.0\n",
" frame = img_to_array(frame)\n",
" frame = np.expand_dims(frame, axis=0)\n",
" return frame\n",
"\n",
"# Start capturing video from your webcam (you may need to change the index depending on your setup)\n",
"cap = cv2.VideoCapture(0)\n",
"\n",
"while True:\n",
" ret, frame = cap.read() # Read a frame from the webcam\n",
"\n",
" if not ret:\n",
" continue\n",
"\n",
" # Preprocess the frame\n",
" processed_frame = preprocess_frame(frame)\n",
"\n",
" # Make a prediction\n",
" predictions = model.predict(processed_frame)\n",
" predicted_class = np.argmax(predictions)\n",
"\n",
" # Get the corresponding text label\n",
" predicted_text = class_to_text_mapping[predicted_class]\n",
"\n",
" # Display the frame and predicted text on the screen\n",
" cv2.putText(frame, predicted_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)\n",
" cv2.imshow('Sign Language Detection', frame)\n",
"\n",
" # Break the loop if the 'q' key is pressed\n",
" if cv2.waitKey(1) & 0xFF == ord('q'):\n",
" break\n",
"\n",
"# Release the camera and close the OpenCV windows\n",
"cap.release()\n",
"cv2.destroyAllWindows()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|