Commit aa33152d authored by Mahinsha Ramyathilake's avatar Mahinsha Ramyathilake

Pre-process

parent 4d79851e
{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"id": "aab590e1",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"from glob import glob\n",
"import os\n",
"import math\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "e0adeb34",
"metadata": {},
"outputs": [],
"source": [
"cap = cv2.VideoCapture(0)\n",
"frameRate = cap.get(5)\n",
"\n",
"count = 0\n",
"\n",
"files = glob('D:/dataset/temp/*')\n",
"for f in files:\n",
" os.remove(f)\n",
" \n",
"while (True):\n",
" frameId = cap.get(1)\n",
" ret, frame = cap.read()\n",
" \n",
" #cv2.imshow('frame', frame)\n",
" \n",
" # Display original image\n",
" cv2.imshow('Original', frame)\n",
"\n",
" # Convert to graycsale\n",
" img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n",
" # Convert to HSV\n",
" img_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)\n",
" # Blur the image for better edge detection\n",
" img_blur1 = cv2.GaussianBlur(img_gray, (3,3), 0) \n",
" # Blur the image for better edge detection\n",
" img_blur2 = cv2.GaussianBlur(img_hsv, (3,3), 0)\n",
"\n",
" # Sobel Edge Detection\n",
" sobelx1 = cv2.Sobel(src=img_blur1, ddepth=cv2.CV_64F, dx=1, dy=0, ksize=5) # Sobel Edge Detection on the X axis\n",
" sobely1 = cv2.Sobel(src=img_blur1, ddepth=cv2.CV_64F, dx=0, dy=1, ksize=5) # Sobel Edge Detection on the Y axis\n",
" sobelxy1 = cv2.Sobel(src=img_blur1, ddepth=cv2.CV_64F, dx=1, dy=1, ksize=5) # Combined X and Y Sobel Edge Detection\n",
" \n",
" # Sobel Edge Detection\n",
" sobelx2 = cv2.Sobel(src=img_blur2, ddepth=cv2.CV_64F, dx=1, dy=0, ksize=5) # Sobel Edge Detection on the X axis\n",
" sobely2 = cv2.Sobel(src=img_blur2, ddepth=cv2.CV_64F, dx=0, dy=1, ksize=5) # Sobel Edge Detection on the Y axis\n",
" sobelxy2 = cv2.Sobel(src=img_blur2, ddepth=cv2.CV_64F, dx=1, dy=1, ksize=5) # Combined X and Y Sobel Edge Detection\n",
" \n",
" # Display Sobel Edge Detection Images\n",
" #cv2.imshow('Sobel X', sobelx)\n",
" #cv2.waitKey(0)\n",
" cv2.imshow('Sobel Y1', sobely1)\n",
" cv2.imshow('Sobel Y2', sobely2)\n",
" #cv2.waitKey(0)\n",
" #cv2.imshow('Sobel X Y using Sobel() function', sobelxy)\n",
" #cv2.waitKey(0)\n",
"\n",
" if (cv2.waitKey(1) & 0xFF == ord('q')):\n",
" break\n",
" if (ret != True):\n",
" break\n",
" if(frameId % math.floor(frameRate) == 0):\n",
" filename = 'D:/dataset/temp/' + \"_frame%d.jpg\" % count;count+=1\n",
" cv2.imwrite(filename, sobely)\n",
"\n",
"cap.release()\n",
"cv2.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "e08c53fc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1280, 720)\n",
"(1280, 720)\n"
]
}
],
"source": [
"img_array=[]\n",
"sizes = (0,0)\n",
"for filename in glob('D:/dataset/temp/*.jpg'):\n",
" img = cv2.imread(filename)\n",
" height, width, layers = img.shape\n",
" size = (width,height)\n",
" print(size)\n",
" sizes = size\n",
" print(sizes)\n",
" img_array.append(img)\n",
"\n",
"out = cv2.VideoWriter('D:/dataset/project.mp4',cv2.VideoWriter_fourcc(*'DIVX'), 15, sizes)\n",
" \n",
"for i in range(len(img_array)):\n",
" out.write(img_array[i])\n",
"out.release()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "01052d28",
"metadata": {},
"outputs": [],
"source": [
"cap = cv2.VideoCapture(0)\n",
"frameRate = cap.get(5)\n",
"\n",
"count = 0\n",
"\n",
"files = glob('D:/dataset/temp/*')\n",
"for f in files:\n",
" os.remove(f)\n",
" \n",
"while (True):\n",
" frameId = cap.get(1)\n",
" ret, frame = cap.read()\n",
" \n",
" #cv2.imshow('frame', frame)\n",
" \n",
" # Display original image\n",
" cv2.imshow('Original', frame)\n",
"\n",
" # Convert to graycsale\n",
" img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n",
" # Blur the image for better edge detection\n",
" img_blur = cv2.GaussianBlur(img_gray, (3,3), 0) \n",
"\n",
" # Canny Edge Detection\n",
" edges = cv2.Canny(image=img_blur, threshold1=100, threshold2=200) # Canny Edge Detection\n",
" # Display Canny Edge Detection Image\n",
" cv2.imshow('Canny Edge Detection', edges)\n",
"\n",
" if (cv2.waitKey(1) & 0xFF == ord('q')):\n",
" break\n",
" if (ret != True):\n",
" break\n",
" if(frameId % math.floor(frameRate) == 0):\n",
" filename = 'D:/dataset/temp/' + \"_frame%d.jpg\" % count;count+=1\n",
" #cv2.imwrite(filename, sobelxy)\n",
" \n",
"cap.release()\n",
"cv2.destroyAllWindows()\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4d9df1a1",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment