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

Assignment2 #49

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
129 changes: 129 additions & 0 deletions data_collection.ipynb
Original file line number Diff line number Diff line change
@@ -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
}