Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final_Sub #50

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions .ipynb_checkpoints/FinalPrediction-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"id": "bcbffb83",
"metadata": {},
"outputs": [],
"source": [
"from keras.models import model_from_json\n",
"import operator\n",
"import cv2"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "75d163ed",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loaded model from disk\n"
]
}
],
"source": [
"json_file = open(\"model-bw.json\", \"r\")\n",
"model_json = json_file.read()\n",
"json_file.close()\n",
"loaded_model = model_from_json(model_json)\n",
"loaded_model.load_weights(\"model-bw.h5\")\n",
"print(\"Loaded model from disk\")\n",
"cap = cv2.VideoCapture(0)\n",
"categories = {0: 'ZERO', 1: 'ONE', 2: 'TWO', 3: 'THREE', 4: 'FOUR', 5: 'FIVE'}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "51692885",
"metadata": {},
"outputs": [],
"source": [
"while True:\n",
" _, frame = cap.read()\n",
" frame = cv2.flip(frame, 1)\n",
" x1 = int(0.5*frame.shape[1])\n",
" y1 = 10\n",
" x2 = frame.shape[1]-10\n",
" y2 = int(0.5*frame.shape[1])\n",
" cv2.putText(frame, \"ElricPi - TDoC 2021\", (175, 450), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (225,255,0), 3)\n",
" cv2.rectangle(frame, (x1-1, y1-1), (x2+1, y2+1), (255,255,255) ,3)\n",
" roi = frame[y1:y2, x1:x2]\n",
" roi = cv2.resize(roi, (64, 64))\n",
" roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)\n",
" cv2.putText(frame, \"R.O.I\", (440, 350), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,225,0), 3)\n",
" _, test_image = cv2.threshold(roi, 120, 255, cv2.THRESH_BINARY)\n",
" cv2.imshow(\"ROI\", test_image)\n",
" result = loaded_model.predict(test_image.reshape(1, 64, 64, 1))\n",
" prediction = {'ZERO': result[0][0],\n",
" 'ONE': result[0][1],\n",
" 'TWO': result[0][2],\n",
" 'THREE': result[0][3],\n",
" 'FOUR': result[0][4],\n",
" 'FIVE': result[0][5]}\n",
" prediction = sorted(prediction.items(), key=operator.itemgetter(1), reverse=True)\n",
" cv2.putText(frame, \"PREDICTION:\", (30, 90), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)\n",
" cv2.putText(frame, prediction[0][0], (80, 130), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)\n",
" cv2.imshow(\"Frame\", frame)\n",
" interrupt = cv2.waitKey(10)\n",
" if interrupt & 0xFF == 27:\n",
" break\n",
"cap.release()\n",
"cv2.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "73bcac5d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
137 changes: 137 additions & 0 deletions .ipynb_checkpoints/TrainingOI-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "faae96f7",
"metadata": {},
"outputs": [],
"source": [
"from keras.models import Sequential\n",
"from keras.layers import Convolution2D, MaxPooling2D, Flatten, Dense\n",
"from keras.preprocessing.image import ImageDataGenerator\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "3c6dab91",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found 865 images belonging to 6 classes.\n",
"Found 30 images belonging to 6 classes.\n",
"Epoch 1/24\n",
"145/145 [==============================] - 6s 33ms/step - loss: 1.8881 - accuracy: 0.3503 - val_loss: 2.0199 - val_accuracy: 0.3667\n",
"Epoch 2/24\n",
"145/145 [==============================] - 4s 26ms/step - loss: 0.7299 - accuracy: 0.7555 - val_loss: 1.9466 - val_accuracy: 0.4000\n",
"Epoch 3/24\n",
"145/145 [==============================] - 4s 26ms/step - loss: 0.4640 - accuracy: 0.8614 - val_loss: 1.6799 - val_accuracy: 0.3667\n",
"Epoch 4/24\n",
"145/145 [==============================] - 4s 25ms/step - loss: 0.4190 - accuracy: 0.8388 - val_loss: 2.0598 - val_accuracy: 0.4333\n",
"Epoch 5/24\n",
"145/145 [==============================] - 4s 26ms/step - loss: 0.2567 - accuracy: 0.9080 - val_loss: 2.2239 - val_accuracy: 0.4000\n",
"Epoch 6/24\n",
"145/145 [==============================] - 4s 25ms/step - loss: 0.2608 - accuracy: 0.9103 - val_loss: 2.7712 - val_accuracy: 0.4333\n",
"Epoch 7/24\n",
"145/145 [==============================] - 4s 25ms/step - loss: 0.2445 - accuracy: 0.9337 - val_loss: 2.1731 - val_accuracy: 0.4667\n",
"Epoch 8/24\n",
"145/145 [==============================] - 4s 26ms/step - loss: 0.1987 - accuracy: 0.9225 - val_loss: 1.8919 - val_accuracy: 0.3667\n",
"Epoch 9/24\n",
"145/145 [==============================] - 4s 26ms/step - loss: 0.1457 - accuracy: 0.9532 - val_loss: 2.4349 - val_accuracy: 0.4000\n",
"Epoch 10/24\n",
"145/145 [==============================] - 4s 26ms/step - loss: 0.0940 - accuracy: 0.9771 - val_loss: 2.3527 - val_accuracy: 0.4333\n",
"Epoch 11/24\n",
"145/145 [==============================] - 4s 27ms/step - loss: 0.1143 - accuracy: 0.9605 - val_loss: 2.4939 - val_accuracy: 0.4333\n",
"Epoch 12/24\n",
"145/145 [==============================] - 4s 27ms/step - loss: 0.1251 - accuracy: 0.9553 - val_loss: 3.1054 - val_accuracy: 0.4333\n",
"Epoch 13/24\n",
"145/145 [==============================] - 4s 27ms/step - loss: 0.1081 - accuracy: 0.9637 - val_loss: 2.7446 - val_accuracy: 0.4667\n",
"Epoch 14/24\n",
"145/145 [==============================] - 4s 27ms/step - loss: 0.1183 - accuracy: 0.9587 - val_loss: 3.0388 - val_accuracy: 0.4333\n",
"Epoch 15/24\n",
"145/145 [==============================] - 4s 26ms/step - loss: 0.1070 - accuracy: 0.9617 - val_loss: 3.1897 - val_accuracy: 0.4667\n",
"Epoch 16/24\n",
"145/145 [==============================] - 4s 27ms/step - loss: 0.0689 - accuracy: 0.9797 - val_loss: 2.7554 - val_accuracy: 0.4333\n",
"Epoch 17/24\n",
"145/145 [==============================] - 4s 27ms/step - loss: 0.1332 - accuracy: 0.9504 - val_loss: 3.6933 - val_accuracy: 0.3333\n",
"Epoch 18/24\n",
"145/145 [==============================] - 4s 27ms/step - loss: 0.0743 - accuracy: 0.9757 - val_loss: 2.9300 - val_accuracy: 0.5333\n",
"Epoch 19/24\n",
"145/145 [==============================] - 4s 27ms/step - loss: 0.0304 - accuracy: 0.9944 - val_loss: 2.6740 - val_accuracy: 0.4333\n",
"Epoch 20/24\n",
"145/145 [==============================] - 4s 27ms/step - loss: 0.0336 - accuracy: 0.9927 - val_loss: 3.8067 - val_accuracy: 0.5000\n",
"Epoch 21/24\n",
"145/145 [==============================] - 4s 27ms/step - loss: 0.0506 - accuracy: 0.9823 - val_loss: 3.4033 - val_accuracy: 0.4333\n",
"Epoch 22/24\n",
"145/145 [==============================] - 4s 26ms/step - loss: 0.0354 - accuracy: 0.9879 - val_loss: 2.9072 - val_accuracy: 0.5000\n",
"Epoch 23/24\n",
"145/145 [==============================] - 4s 26ms/step - loss: 0.0413 - accuracy: 0.9833 - val_loss: 3.5864 - val_accuracy: 0.4667\n",
"Epoch 24/24\n",
"145/145 [==============================] - 4s 27ms/step - loss: 0.0105 - accuracy: 0.9997 - val_loss: 3.6084 - val_accuracy: 0.4333\n"
]
}
],
"source": [
"classifier = Sequential()\n",
"classifier.add(Convolution2D(32, (3, 3), input_shape=(64, 64, 1), activation='tanh'))\n",
"classifier.add(MaxPooling2D(pool_size=(2, 2)))\n",
"classifier.add(Convolution2D(32, (3, 3), activation='tanh'))\n",
"classifier.add(MaxPooling2D(pool_size=(2, 2)))\n",
"classifier.add(Flatten())\n",
"classifier.add(Dense(units=128, activation='tanh'))\n",
"classifier.add(Dense(units=6, activation='softmax'))\n",
"classifier.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n",
"\n",
"train_datagen = ImageDataGenerator( rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)\n",
"\n",
"test_datagen = ImageDataGenerator(rescale=1./255)\n",
"\n",
"training_set = train_datagen.flow_from_directory(r\"C:\\Users\\TISYAGUPTA PYNE\\tdoc21\\data\\train\", target_size=(64, 64), batch_size=6, color_mode='grayscale', class_mode='categorical')\n",
"\n",
"test_set = test_datagen.flow_from_directory(r\"C:\\Users\\TISYAGUPTA PYNE\\tdoc21\\data\\test\", target_size=(64, 64), batch_size=6, color_mode='grayscale', class_mode='categorical')\n",
"\n",
"\n",
"classifier.fit_generator(training_set, epochs=24, validation_data=test_set)\n",
"\n",
"model_json = classifier.to_json()\n",
"\n",
"with open(\"model-bw.json1\", \"w\") as json_file:\n",
" json_file.write(model_json)\n",
"classifier.save_weights('model-bw1.h5')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1ec4a6ae",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
110 changes: 110 additions & 0 deletions FinalPrediction.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"id": "bcbffb83",
"metadata": {},
"outputs": [],
"source": [
"from keras.models import model_from_json\n",
"import operator\n",
"import cv2"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "75d163ed",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loaded model from disk\n"
]
}
],
"source": [
"json_file = open(\"model-bw1.json\", \"r\")\n",
"model_json = json_file.read()\n",
"json_file.close()\n",
"loaded_model = model_from_json(model_json)\n",
"loaded_model.load_weights(\"model-bw1.h5\")\n",
"print(\"Loaded model from disk\")\n",
"cap = cv2.VideoCapture(0)\n",
"categories = {0: 'ZERO', 1: 'ONE', 2: 'TWO', 3: 'THREE', 4: 'FOUR', 5: 'FIVE'}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "51692885",
"metadata": {},
"outputs": [],
"source": [
"while True:\n",
" _, frame = cap.read()\n",
" frame = cv2.flip(frame, 1)\n",
" x1 = int(0.5*frame.shape[1])\n",
" y1 = 10\n",
" x2 = frame.shape[1]-10\n",
" y2 = int(0.5*frame.shape[1])\n",
" cv2.putText(frame, \"ElricPi - TDoC 2021\", (175, 450), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (225,255,0), 3)\n",
" cv2.rectangle(frame, (x1-1, y1-1), (x2+1, y2+1), (255,255,255) ,3)\n",
" roi = frame[y1:y2, x1:x2]\n",
" roi = cv2.resize(roi, (64, 64))\n",
" roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)\n",
" cv2.putText(frame, \"R.O.I\", (440, 350), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,225,0), 3)\n",
" _, test_image = cv2.threshold(roi, 120, 255, cv2.THRESH_BINARY)\n",
" cv2.imshow(\"ROI\", test_image)\n",
" result = loaded_model.predict(test_image.reshape(1, 64, 64, 1))\n",
" prediction = {'ZERO': result[0][0],\n",
" 'ONE': result[0][1],\n",
" 'TWO': result[0][2],\n",
" 'THREE': result[0][3],\n",
" 'FOUR': result[0][4],\n",
" 'FIVE': result[0][5]}\n",
" prediction = sorted(prediction.items(), key=operator.itemgetter(1), reverse=True)\n",
" cv2.putText(frame, \"PREDICTION:\", (30, 90), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)\n",
" cv2.putText(frame, prediction[0][0], (80, 130), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)\n",
" cv2.imshow(\"Frame\", frame)\n",
" interrupt = cv2.waitKey(10)\n",
" if interrupt & 0xFF == 27:\n",
" break\n",
"cap.release()\n",
"cv2.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "73bcac5d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading