-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.Draw.py
79 lines (67 loc) · 2.63 KB
/
2.Draw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import cv2
import numpy as np
class drawingCanvas():
def __init__(self):
self.penrange = np.load('penrange.npy')
self.cap = cv2.VideoCapture(0)
self.canvas = None
#initial position on pen
self.x1,self.y1=0,0
#used to toggle between pen and eraser mode
self.val=1
self.draw()
def draw(self):
while True:
_, self.frame = self.cap.read()
self.frame = cv2.flip( self.frame, 1 )
if self.canvas is None:
self.canvas = np.zeros_like(self.frame)
mask=self.CreateMask()
contours=self.ContourDetect(mask)
self.drawLine(contours)
self.display()
k = cv2.waitKey(1) & 0xFF
self.takeAction(k)
#if esc key is pressed exit
if k == 27:
break
def CreateMask(self):
hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV)
lower_range = self.penrange[0]
upper_range = self.penrange[1]
mask = cv2.inRange(hsv, lower_range, upper_range)
return mask
def ContourDetect(self,mask):
# Find Contours
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:
c = max(contours, key = cv2.contourArea)
x2,y2,w,h = cv2.boundingRect(c)
if self.x1 == 0 and self.y1 == 0:
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)
if __name__ == '__main__':
drawingCanvas()
cv2.destroyAllWindows()