├── README.md
├── images
├── .keep
├── Screenshot (33).png
└── Screenshot (34).png
└── onairdrawing.py
/README.md:
--------------------------------------------------------------------------------
1 | # On-Air-Drawing
2 | On Air Drawing using opencv.
3 | You can draw on air, using an object of a specific color and we have set the default to YELLOW.
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/images/.keep:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/images/Screenshot (33).png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CRYPTOcoderAS/On-Air-Drawing/49126f1981dfb4c40891cbd521a8a7c6b4c7f098/images/Screenshot (33).png
--------------------------------------------------------------------------------
/images/Screenshot (34).png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CRYPTOcoderAS/On-Air-Drawing/49126f1981dfb4c40891cbd521a8a7c6b4c7f098/images/Screenshot (34).png
--------------------------------------------------------------------------------
/onairdrawing.py:
--------------------------------------------------------------------------------
1 | import cv2
2 | import numpy as np
3 | import argparse
4 | #using YELLOW color
5 | ap = argparse.ArgumentParser()
6 | ap.add_argument("-i", "--image", type=str, default="",
7 | help="path to input image")
8 | args = vars(ap.parse_args())
9 |
10 | drawing = False # true if mouse is pressed #
11 | ix,iy = -1,-1
12 |
13 | # mouse callback function
14 | def draw_circle(event,x,y,flags,param):
15 | global ix,iy,drawing,mode
16 |
17 | if event == cv2.EVENT_LBUTTONDOWN:
18 | drawing = True
19 | ix,iy = x,y
20 |
21 | elif event == cv2.EVENT_MOUSEMOVE:
22 | if drawing == True:
23 | cv2.circle(imAux,(x,y),thick,color,-1)
24 |
25 | elif event == cv2.EVENT_LBUTTONUP:
26 | drawing = False
27 | cv2.circle(imAux,(x,y),thick,color,-1)
28 | cv2.namedWindow('imAux')
29 | cv2.setMouseCallback('imAux',draw_circle)
30 |
31 | cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
32 |
33 | color_low = np.array([25, 52, 72], np.uint8)
34 | color_up = np.array([50, 255, 255], np.uint8)
35 |
36 | # Colors to paint
37 | color_blue = (255,113,82)
38 | color_yellow = (89,222,255)
39 | color_pink = (128,0,255)
40 | color_green = (0,255,36)
41 | clear_screen = (29,112,246) # Will only be used for the upper box of 'Clean Screen'
42 | # Line thickness upper left boxes (color to draw)
43 | thickness_blue = 6
44 | thickness_yellow = 2
45 | thickness_pink = 2
46 | thickness_green = 2
47 |
48 | # Line thickness upper right boxes (thickness of the marker to draw)
49 | small_thickness = 6
50 | medium_thickness = 1
51 | large_thickness = 1
52 |
53 | # --------------------- Variables for the marker / virtual pen --------------------- ----
54 | color = color_blue # Input color, and variable that will assign the marker color
55 | thick = 3 # Thickness that the marker will have
56 | #------------------------------------------------------------------------------------------
57 |
58 | x1 = None
59 | y1 = None
60 |
61 | imAux = cv2.imread(args["image"]) # Reloading work
62 | while True:
63 |
64 | ret,frame = cap.read()
65 | if ret==False: break
66 |
67 | frame = cv2.flip(frame,1)
68 | frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
69 | if imAux is None: imAux = np.zeros(frame.shape,dtype=np.uint8)
70 |
71 | # ------------------------ Top Section ----------------------- -------------------
72 | # Squares drawn in the upper left (represent the color to be drawn)
73 | cv2.rectangle(frame,(0,0),(50,50),color_yellow,thickness_yellow)
74 | cv2.rectangle(frame,(50,0),(100,50),color_pink,thickness_pink)
75 | cv2.rectangle(frame,(100,0),(150,50),color_green,thickness_green)
76 | cv2.rectangle(frame,(150,0),(200,50),color_blue,thickness_blue)
77 |
78 | # Upper central rectangle, which will help us clean the screen
79 | cv2.rectangle(frame,(300,0),(400,50),clear_screen,1)
80 | cv2.putText(frame,'Clear',(320,20),6,0.6,clear_screen,1,cv2.LINE_AA)
81 | cv2.putText(frame,'Screen',(320,40),6,0.6,clear_screen,1,cv2.LINE_AA)
82 |
83 | # Squares drawn at the top right (marker thickness for drawing)
84 | cv2.rectangle(frame,(490,0),(540,50),(0,0,0),small_thickness)
85 | cv2.circle(frame,(515,25),3,(0,0,0),-1)
86 | cv2.rectangle(frame,(540,0),(590,50),(0,0,0),medium_thickness)
87 | cv2.circle(frame,(565,25),7,(0,0,0),-1)
88 | cv2.rectangle(frame,(590,0),(640,50),(0,0,0),large_thickness)
89 | cv2.circle(frame,(615,25),11,(0,0,0),-1)
90 | #-----------------------------------------------------------------------------------
91 |
92 | # Light blue color detection
93 | mask_out = cv2.inRange(frameHSV, color_low, color_up)
94 | mask_out = cv2.erode(mask_out,None,iterations = 1)
95 | mask_out = cv2.dilate(mask_out,None,iterations = 2)
96 | mask_out = cv2.medianBlur(mask_out, 13)
97 | cnts,_ = cv2.findContours(mask_out, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
98 | cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:1]
99 |
100 | for c in cnts:
101 | area = cv2.contourArea(c)
102 | if area > 1000:
103 | x,y2,w,h = cv2.boundingRect(c)
104 | x2 = x + w//2
105 |
106 | if x1 is not None:
107 | if 0 < x2 < 50 and 0 < y2 < 50:
108 | color = color_yellow # Color of virtual pen / marker
109 | thickness_yellow = 6
110 | thickness_pink = 2
111 | thickness_green = 2
112 | thickness_blue = 2
113 | if 50 < x2 < 100 and 0 < y2 < 50:
114 | color = color_pink # Color of virtual pen / marker
115 | thickness_yellow = 2
116 | thickness_pink = 6
117 | thickness_green = 2
118 | thickness_blue = 2
119 | if 100 < x2 < 150 and 0 < y2 < 50:
120 | color = color_green # Color of virtual pen / marker
121 | thickness_yellow = 2
122 | thickness_pink = 2
123 | thickness_green = 6
124 | thickness_blue = 2
125 | if 150 < x2 < 200 and 0 < y2 < 50:
126 | color = color_blue # Color of virtual pen / marker
127 | thickness_yellow = 2
128 | thickness_pink = 2
129 | thickness_green = 2
130 | thickness_blue = 6
131 | if 490 < x2 < 540 and 0 < y2 < 50:
132 | thick = 3 # Thickness of virtual pen / marker
133 | small_thickness = 6
134 | medium_thickness = 1
135 | large_thickness = 1
136 | if 540 < x2 < 590 and 0 < y2 < 50:
137 | thick = 7 # Thickness of virtual pen / marker
138 | small_thickness = 1
139 | medium_thickness = 6
140 | large_thickness = 1
141 | if 590 < x2 < 640 and 0 < y2 < 50:
142 | thick = 11 # Thickness of virtual pen / marker
143 | small_thickness = 1
144 | medium_thickness = 1
145 | large_thickness = 6
146 | if 300 < x2 < 400 and 0 < y2 < 50:
147 | cv2.rectangle(frame,(300,0),(400,50),clear_screen,2)
148 | cv2.putText(frame,'Clear',(320,20),6,0.6,clear_screen,3,cv2.LINE_AA)
149 | cv2.putText(frame,'Screen',(320,40),6,0.6,clear_screen,3,cv2.LINE_AA)
150 | imAux = np.zeros(frame.shape,dtype=np.uint8)
151 | if 0 < y2 < 60 or 0 < y1 < 60 :
152 | imAux = imAux
153 | else:
154 | imAux = cv2.line(imAux,(x1,y1),(x2,y2),color,thick)
155 | cv2.circle(frame,(x2,y2),thick,color,3)
156 | x1 = x2
157 | y1 = y2
158 | else:
159 | x1, y1 = None, None
160 |
161 | imAuxGray = cv2.cvtColor(imAux,cv2.COLOR_BGR2GRAY)
162 | _, th = cv2.threshold(imAuxGray,10,255,cv2.THRESH_BINARY)
163 | thInv = cv2.bitwise_not(th)
164 | frame = cv2.bitwise_and(frame,frame,mask=thInv)
165 | frame = cv2.add(frame,imAux)
166 |
167 |
168 | cv2.imshow('imAux',imAux) #drawing
169 | cv2.imshow('frame', frame) #input
170 | cv2.imshow('mask_out', mask_out) #countour
171 |
172 | k = cv2.waitKey(1)
173 | if k == 27 or k==113:
174 | break #exit
175 | if k == 115:
176 | cv2.imwrite("output.png",imAux) #saving image
177 |
178 | cap.release()
179 | cv2.destroyAllWindows()
180 |
--------------------------------------------------------------------------------