From a391af2f379b5b91e8c8f9d5ecc55be013ddb637 Mon Sep 17 00:00:00 2001 From: Tisyagupta Pyne Date: Wed, 27 Oct 2021 19:16:18 +0530 Subject: [PATCH] Assignment2 --- data_collection.ipynb | 129 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 data_collection.ipynb diff --git a/data_collection.ipynb b/data_collection.ipynb new file mode 100644 index 0000000..73a59bb --- /dev/null +++ b/data_collection.ipynb @@ -0,0 +1,129 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 5, + "id": "a334ae83", + "metadata": {}, + "outputs": [], + "source": [ + "import cv2\n", + "import os\n", + "\n", + "if not os.path.exists(\"data\"): #True\n", + " os.makedirs(\"data\")\n", + " os.makedirs(\"data/train\")\n", + " os.makedirs(\"data/test\")\n", + " \n", + " os.makedirs(\"data/train/0\")\n", + " os.makedirs(\"data/train/1\")\n", + " os.makedirs(\"data/train/2\")\n", + " os.makedirs(\"data/train/3\")\n", + " os.makedirs(\"data/train/4\")\n", + " os.makedirs(\"data/train/5\")\n", + " \n", + " os.makedirs(\"data/test/0\")\n", + " os.makedirs(\"data/test/1\")\n", + " os.makedirs(\"data/test/2\")\n", + " os.makedirs(\"data/test/3\")\n", + " os.makedirs(\"data/test/4\")\n", + " os.makedirs(\"data/test/5\")\n", + " \n", + "\n", + "mode = 'training'\n", + "directory = 'data/'+mode+'/' #data/train\n", + "\n", + "cap=cv2.VideoCapture(0)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "1a49db1a", + "metadata": {}, + "outputs": [], + "source": [ + "while True:\n", + " _, frame = cap.read()\n", + " frame = cv2.flip(frame, 1)\n", + " cv2.putText(frame, \"ElricPi - TenDaysofCode 2021\", (175, 450), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (225,255,255), 3)\n", + " count = {'zero': len(os.listdir(directory+\"/0\")),\n", + " 'one': len(os.listdir(directory+\"/1\")),\n", + " 'two': len(os.listdir(directory+\"/2\")),\n", + " 'three': len(os.listdir(directory+\"/3\")),\n", + " 'four': len(os.listdir(directory+\"/4\")),\n", + " 'five': len(os.listdir(directory+\"/5\"))}\n", + " \n", + " cv2.putText(frame, \"MODE : \"+mode, (30, 50), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (225,255,255), 1)\n", + " cv2.putText(frame, \"IMAGE COUNT\", (10, 100), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (225,255,255), 1)\n", + " cv2.putText(frame, \"ZERO : \"+str(count['zero']), (10, 120), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,255), 1)\n", + " cv2.putText(frame, \"ONE : \"+str(count['one']), (10, 140), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,255), 1)\n", + " cv2.putText(frame, \"TWO : \"+str(count['two']), (10, 160), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,255), 1)\n", + " cv2.putText(frame, \"THREE : \"+str(count['three']), (10, 180), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,255), 1)\n", + " cv2.putText(frame, \"FOUR : \"+str(count['four']), (10, 200), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,255), 1)\n", + " cv2.putText(frame, \"FIVE : \"+str(count['five']), (10, 220), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,255), 1)\n", + " \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.rectangle(frame, (x1-1, y1-1), (x2+1, y2+1), (255,0,0) ,3)\n", + " roi = frame[y1:y2, x1:x2]\n", + " roi = cv2.resize(roi, (200, 200))\n", + " cv2.putText(frame, \"R.O.I\", (440, 350), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,225,0), 3)\n", + " cv2.imshow(\"Frame\", frame)\n", + " roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)\n", + " _, roi = cv2.threshold(roi, 120, 255, cv2.THRESH_BINARY)\n", + " cv2.imshow(\"ROI\", roi)\n", + " interrupt = cv2.waitKey(10)\n", + " \n", + " if interrupt & 0xFF == 27:\n", + " break\n", + " if interrupt & 0xFF == ord('0'):\n", + " cv2.imwrite(directory+'0/'+str(count['zero'])+'.jpg', roi)\n", + " if interrupt & 0xFF == ord('1'):\n", + " cv2.imwrite(directory+'1/'+str(count['one'])+'.jpg', roi)\n", + " if interrupt & 0xFF == ord('2'):\n", + " cv2.imwrite(directory+'2/'+str(count['two'])+'.jpg', roi)\n", + " if interrupt & 0xFF == ord('3'):\n", + " cv2.imwrite(directory+'3/'+str(count['three'])+'.jpg', roi)\n", + " if interrupt & 0xFF == ord('4'):\n", + " cv2.imwrite(directory+'4/'+str(count['four'])+'.jpg', roi)\n", + " if interrupt & 0xFF == ord('5'):\n", + " cv2.imwrite(directory+'5/'+str(count['five'])+'.jpg', roi)\n", + " \n", + "cap.release()\n", + "cv2.destroyAllWindows()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a18f5ec8", + "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 +}