diff --git a/Deep Learning/Moving Object Detection using opencv/Moving_Object_Detection.ipynb b/Deep Learning/Moving Object Detection using opencv/Moving_Object_Detection.ipynb new file mode 100644 index 0000000..ce0ca2d --- /dev/null +++ b/Deep Learning/Moving Object Detection using opencv/Moving_Object_Detection.ipynb @@ -0,0 +1,236 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Imoprting Libraries" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import cv2\n", + "import time\n", + "import imutils" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Importing Camera" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "cam = cv2.VideoCapture(0)\n", + "time.sleep(1)\n", + "firstFrame = None\n", + "area = 600" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Programming" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n", + "Moving Object Detector\n" + ] + } + ], + "source": [ + "while True:\n", + " _, img = cam.read()\n", + " text = \"Moving Object Detector\"\n", + " img = imutils.resize(img, width=600)\n", + " grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n", + " gaussianImg = cv2.GaussianBlur(grayImg, (29,29),0)\n", + " if firstFrame is None:\n", + " firstFrame = gaussianImg\n", + " continue\n", + " imgDiff = cv2.absdiff(firstFrame, gaussianImg)\n", + " threshImg = cv2.threshold(imgDiff, 20, 229, cv2.THRESH_BINARY)[1]\n", + " threshImg = cv2.dilate(threshImg, None, iterations=2)\n", + " cnts = cv2.findContours(threshImg.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)\n", + " cnts = imutils.grab_contours(cnts)\n", + " for c in cnts:\n", + " if cv2.contourArea(c)< area:\n", + " continue\n", + " (x,y,w,h) = cv2.boundingRect(c)\n", + " cv2.rectangle(img, (x,y), (x+w, y+h), (0,0,255), 2)\n", + " text = \"Moving Object Detected\"\n", + " print(text)\n", + " cv2.putText(img, text, (15,15), cv2.FONT_ITALIC, 0.7, (0,0,255),2)\n", + " cv2.imshow('Camera Feed', img)\n", + " key = cv2.waitKey(1) & 0xFF\n", + " if key == ord(\"q\"):\n", + " break\n", + "cam.release()\n", + "cv2.destroyAllWindows()" + ] + } + ], + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}