├── README.md ├── LICENSE └── Magic Pen.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # Magic-Pen 2 | Ever dreamed of drawing stuff on the screen just by waving your pen in the air...Well that is exactly what Magic Pen does using Python(OpenCV and NumPy). 3 | 4 | 5 | ![Demo](https://github.com/Prathyusha-Guduru/Data/blob/master/Magic%20pen.gif) 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Prathyusha Guduru 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Magic Pen.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "#Importing Necessary projects\n", 10 | "import cv2\n", 11 | "import numpy as np" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 4, 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "#Creating the video capture object \n", 21 | "cap = cv2.VideoCapture(0)\n", 22 | "\n", 23 | "#Defining upper and lower ranges for yellow color\n", 24 | "Lower = np.array([20, 100, 100])\n", 25 | "Upper = np.array([30, 255, 255])\n", 26 | "\n", 27 | "#Defining kernel for Morphological operators\n", 28 | "kernel = np.ones((5,5),np.uint8)\n", 29 | "#Defining starting point\n", 30 | "x0,y0 = -1,-1\n", 31 | "\n", 32 | "\n", 33 | "#Creating an empty image / white background with the same frame size\n", 34 | "temp = np.ones((480, 640, 3),dtype = np.uint8)\n", 35 | "temp = temp*255\n", 36 | "\n", 37 | "while True:\n", 38 | " ret,frame = cap.read()\n", 39 | " s = frame.shape\n", 40 | " #Flipping for mirror image\n", 41 | " frame = cv2.flip(frame,1)\n", 42 | " #Getting a hsv version of the frame for easy colour detection and locating the mask\n", 43 | " hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)\n", 44 | " mask = cv2.inRange(hsv,Lower,Upper)\n", 45 | " #Performing morphological operators \n", 46 | " mask = cv2.erode(mask,kernel,iterations = 2)\n", 47 | " final = cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernel)\n", 48 | " final = cv2.dilate(mask,kernel,iterations = 1)\n", 49 | "\n", 50 | " #Finding contours in the mask \n", 51 | " contours,_ = cv2.findContours(final,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)\n", 52 | " #Getting the largest contours assuming it would be the object of interest\n", 53 | " if contours:\n", 54 | " cnt = max(contours,key = cv2.contourArea)\n", 55 | " x,y,width,height = cv2.boundingRect(cnt)\n", 56 | "\n", 57 | " if x0 == -1:\n", 58 | " x0,y0 = x+width//2,y+height//2\n", 59 | " else:\n", 60 | " #Drawing on the temporary masked image\n", 61 | " temp = cv2.line(temp,(x0,y0),(x+width//2,y+height//2),(0,0,255),5)\n", 62 | " # cv2.circle(frame,(x,y),5,2)\n", 63 | " #To track can be removed if necessary \n", 64 | " frame = cv2.line(frame,(x0,y0),(x+width//2,y+height//2),(255,255,255),5)\n", 65 | " x0,y0 = x+width//2,y+height//2\n", 66 | " else:\n", 67 | " x0,y0=-1,-1\n", 68 | " \n", 69 | " # Operations using bitwise functions for the written stuff on the Result image\n", 70 | " temp_gray = cv2.cvtColor(temp,cv2.COLOR_BGR2GRAY) # BLACK FOREGROUND AND WHITE BACKGROUND\n", 71 | " temp_gray_inv = cv2.bitwise_not(temp_gray) #WHITE FOREGROUND AND BLACK BACKGROUND\n", 72 | " white_background = np.full(temp.shape, 255, dtype=np.uint8) #Plain white background\n", 73 | " bk = cv2.bitwise_or(white_background, white_background, mask=temp_gray_inv) #3 channeled temp_gray_inv\n", 74 | " fg = cv2.bitwise_or(temp, temp, mask=temp_gray_inv) #Red foreground and black background\n", 75 | " Result = cv2.bitwise_or(frame,fg)\n", 76 | "# cv2.imshow('Main',frame)\n", 77 | "# cv2.imshow('Mask',mask)\n", 78 | "# cv2.imshow('Temp',temp)\n", 79 | "# cv2.imshow('Tempgrayinv',temp_gray_inv)\n", 80 | " cv2.imshow('Result',Result)\n", 81 | " #To end the program\n", 82 | " key = cv2.waitKey(1) & 0xFF\n", 83 | " if key == 27:\n", 84 | " break\n", 85 | " \n", 86 | " \n", 87 | " \n", 88 | "cap.release()\n", 89 | "cv2.destroyAllWindows()\n" 90 | ] 91 | } 92 | ], 93 | "metadata": { 94 | "kernelspec": { 95 | "display_name": "Python 3", 96 | "language": "python", 97 | "name": "python3" 98 | }, 99 | "language_info": { 100 | "codemirror_mode": { 101 | "name": "ipython", 102 | "version": 3 103 | }, 104 | "file_extension": ".py", 105 | "mimetype": "text/x-python", 106 | "name": "python", 107 | "nbconvert_exporter": "python", 108 | "pygments_lexer": "ipython3", 109 | "version": "3.7.3" 110 | } 111 | }, 112 | "nbformat": 4, 113 | "nbformat_minor": 4 114 | } 115 | --------------------------------------------------------------------------------