Skip to main content

Make a Virtual Pen and Eraser Using Python || Virtual Pen and Eraser Using OpenCV || Draw Using a Virtual Pen || Creation Code



 Introduction


The project aims to create a virtual pen and eraser using OpenCV and python. This project is very simple and easy to implement. The code is given in this article which you can easily copy and paste into a file and run it on your machine. In this article, we will be using OpenCV for detection of the mouse movements, PyQt5 for GUI creation, Numpy for storing data as well as NumPy array manipulation functions such as slicing and indexing along with Pandas Dataframes which are very helpful while working with Numpy arrays.

Virtual Pen and Eraser Using OpenCV

In this section, you will learn how to create a virtual pen and eraser by using OpenCV.

The first step is importing the required Python libraries. We need to import the cv2 module from opencv library

import cv2
import numpy as np

This project aims to create a virtual pen and eraser using OpenCV and Python

The project aims to create a virtual pen and eraser using opencv and python. This is a very interesting topic for the students who are learning computer vision in college or school. OpenCV is a library that works on different platforms like Windows, Linux, MacOS and Android; it provides cutting-edge features including face detection, object detection, text detection etc. Python is a high-level programming language used for general productivity purposes such as web development or scripting applications.

This project has been divided into tow parts
  • First take the range 
  • Image proccessing

Before diving into this project, please make sure that you have installed Python3, OpenCV, and Numpy on your PC.

Before diving into this project, you should make sure that you have installed Python3, OpenCV and Numpy on your PC.

Install Python 3:

To install Python 3 on the the Windows platform, go to python.org/downloads and download the latest version of Python 3 (recommended is 64-bit). A setup wizard will be launched after downloading the file which will guide you through the installation process. You can also check out their official documentation for more information about installation steps at

 https://docs.python.org/3/installation/#installing-on-windows-and-macosx

Install opencv3:

OpenCV library is available both through Conda package manager or pip package manager depending on which way suits you best in installing packages in the python environment

If you want to install them, please follow the below link for step by step installation guide.

  • pip install numpy
  • pip install opencv-python

Installation of Python3, OpenCV3 and NumPy on Ubuntu 16.04 LTS for Python 3.5 

Step 1: Install python 3.5 on Ubuntu 16.04 LTS

sudo apt-get update

sudo apt-get install python3.5 python3-dev python3-pip python3-numpy libhdf5-dev libjpeg62 libpng-dev libtiff5 libtiffxx0c2 libjasper1v5 jasper2 openjpeg2 tbb# mkdir ~/virtual_keyboard# cd virtual_keyboard

Now let’s begin building your virtual pen and eraser.

First, create a file name penrange.py to take the range

import cv2
import numpy as np
cap=cv2.VideoCapture(0)
def nothing(x):
    pass
#Create trackbar to adjust HSV range
cv2.namedWindow("trackbar")
cv2.createTrackbar("L-H","trackbar",0,179,
nothing)
cv2.createTrackbar("L-S","trackbar",0,255,
nothing)
cv2.createTrackbar("L-V","trackbar",0,255,
nothing)
cv2.createTrackbar("U-H","trackbar",179,179,
nothing)
cv2.createTrackbar("U-S","trackbar",255,255,
nothing)
cv2.createTrackbar("U-V","trackbar",255,255,
nothing)
while True:
    ret,frame =cap.read()
    hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)    
     
    l_h=cv2.getTrackbarPos("L-H","trackbar")
    l_s=cv2.getTrackbarPos("L-S","trackbar")
    l_v=cv2.getTrackbarPos("L-V","trackbar")
    h_h=cv2.getTrackbarPos("U-H","trackbar")
    h_s=cv2.getTrackbarPos("U-S","trackbar")
    h_v=cv2.getTrackbarPos("U-V","trackbar")
   
    low=np.array([l_h,l_s,l_v])
    high=np.array([h_h,h_s,h_v])
 
    mask=cv2.inRange(hsv,low,high)
    result=cv2.bitwise_and(frame,frame,mask=
mask)    
    cv2.imshow("result",result)# If the user
presses ESC then exit the program
    key = cv2.waitKey(1)
    # If the user presses `s` then print and
save this array.
    if key == ord('s'):
        thearray = [[l_h,l_s,l_v],
[h_h, h_s, h_v]]
        # Save this array as penrange.npy
        np.save('penrange',thearray)
        break
    #if esc pressed exit
    if key == 27:
        break
 
cap.release()
cv2.destroyAllWindows()

Now create a main.py, this is our main code

import cv2
import numpy as np
class drawingCanvas():
    def __init__(self):
      self.penrange = np.load('penrange.npy')
# load HSV range
      self.cap = cv2.VideoCapture(0)        
 #0 means primary camera .
      self.canvas = None                      
#initialize blank canvas
        #initial position on pen
      self.x1,self.y1=0,0
        # val is used to toggle between pen and eraser mode
      self.val=1
      self.draw()                            
#Finally call the draw function

    def draw(self):
        while True:
          _, self.frame = self.cap.read()      
#read new frame
          self.frame = cv2.flip( self.frame, 1 )
#flip horizontally
   
          if self.canvas is None:
            self.canvas = np.zeros_like(self.frame)
#initialize a black canvas
           
          mask=self.CreateMask()            
#createmask
          contours=self.ContourDetect(mask)  
#detect Contours
          self.drawLine(contours)            
#draw lines
          self.display()                    
#display results
          k = cv2.waitKey(1) & 0xFF          
#wait for keyboard input
          self.takeAction(k)                
#take action based on k value
       
          if k == 27:                        
#if esc key is pressed exit
            break      

    def CreateMask(self):
      hsv = cv2.cvtColor(self.frame,
cv2.COLOR_BGR2HSV) #convert from BGR to HSV color range
      lower_range = self.penrange[0]                  
 #load  HSV lower range
      upper_range = self.penrange[1]                  
 #load  HSV upper range
      mask = cv2.inRange(hsv, lower_range,
upper_range)
#Create binary mask
      return mask

    def ContourDetect(self,mask):
        # Find Contours based on the mask created.
      contours, hierarchy = cv2.findContours(mask,
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
      return contours

    def drawLine(self,contours):
        #if contour area is not none and is greater than 100 draw the line
      if contours and cv2.contourArea(max(contours,
key = cv2.contourArea)) > 100:  #100 is required min contour area              
          c = max(contours, key = cv2.contourArea)    
          x2,y2,w,h = cv2.boundingRect(c)
   
      if self.x1 == 0 and self.y1 == 0:  
 #this will we true only for the first time marker is detected
        self.x1,self.y1= x2,y2
      else:
                # Draw the line on the canvas
          self.canvas = cv2.line(self.canvas,
(self.x1,self.y1),(x2,y2), [255*self.val,0,0], 10)
            #New point becomes the previous point
            self.x1,self.y1= x2,y2
      else:
            # If there were no contours detected then make x1,y1 = 0 (reset)
          self.x1,self.y1 =0,0  

    def display(self):
        # Merge the canvas and the frame.
        self.frame = cv2.add(self.frame,
self.canvas)    
        cv2.imshow('frame',self.frame)
        cv2.imshow('canvas',self.canvas)

    def takeAction(self,k):
        # When c is pressed clear the entire canvas
        if k == ord('c'):
            self.canvas = None
        #press e to change between eraser mode and writing mode
        if k==ord('e'):
            self.val= int(not self.val) # toggle
val value between 0 and 1 to change marker color.
                   
if __name__ == '__main__':
    drawingCanvas()
     
cv2.destroyAllWindows()

You Can Clone This Project From here

Conclusion

I hope you enjoyed this tutorial about creating a virtual pen and eraser using OpenCV and python. If you have any questions, please comment below and I will try to get back to you as soon as possible.

Comments

Popular posts from this blog

Create Ping Pong Game in Python

  Ping Pong Game: Table tennis , also known as  ping-pong  and  whiff-whaff , is a sport in which two or four players hit a lightweight ball, also known as the ping-pong ball, back and forth across a table using small rackets. The game takes place on a hard table divided by a net. Except for the initial serve, the rules are generally as follows: players must allow a ball played toward them to bounce once on their side of the table and must return it so that it bounces on the opposite side at least once. A point is scored when a player fails to return the ball within the rules. Play is fast and demands quick reactions. Spinning the ball alters its trajectory and limits an opponent's options, giving the hitter a great advantage. We can make it using pygame but I keep it more simple we will create this game using only the turtle module so let's drive into the code without wasting any time Code: # Import required library import turtle # Create screen sc = turtle . Screen () sc .

Draw Minecraft Charater in Python

  Minecraft  is a  sandbox video game  developed by the Swedish video game developer  Mojang Studios . The game was created by  Markus "Notch" Persson  in the  Java programming language . Following several early private testing versions, it was first made public in May 2009 before fully releasing in November 2011, with  Jens Bergensten  then taking over development.  Minecraft  has since been ported to several other platforms and is the  best-selling video game of all time , with over 238 million copies sold and nearly 140 million  monthly active users  as of 2021 . We gonna build a character of Minecraft using our creativity and coding skills so let's drive into the code: Code: import turtle as t def eye ( r , a ):     t . fillcolor ( 'brown' )     t . begin_fill ()     t . circle ( r , a )     t . end_fill () t . begin_fill () t . fillcolor ( '#8a00e6' ) t . penup () t . goto ( 0 ,- 50 ) t . pendown () t . right ( 90 ) t . f

How To Draw BMW Logo - In Python

 I know I don't need to introduce BMW as it is a very popular luxury car. Today we gonna draw the BMW logo in python. I know that you can draw it using a pencil and other tools like AutoCAD etc. But we are programmers we talk with computers so let's tell our computer to draw this logo for use with the help of python. Module The only module we will use is that turtle Code: import turtle as t t.begin_fill() t.fillcolor( '#008ac9' ) for i in range ( 50 ):     t.forward( 4 )     t.left( 2 ) t.right(- 80 ) t.forward( 116 ) t.right(- 90 ) t.forward( 132 ) t.end_fill() t.penup() t.pendown() t.right( 90 ) for i in range ( 50 ):     t.forward( 4 )     t.left(- 2 ) t.right( 80 ) t.forward( 116 ) t.forward(- 116 ) t.right( 90 ) t.begin_fill() t.fillcolor( '#008ac9' ) for j in range ( 45 ):     t.forward(- 4 )     t.left(- 2 ) t.right(- 90 ) t.forward( 116 ) t.end_fill() t.right( 180 ) t.forward( 116 ) t.right( 90 ) for i in range ( 47 ):     t.forward( 4 )     t.