|
| 1 | +import numpy as np |
| 2 | +import cv2 |
| 3 | +from collections import deque |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +# default called trackbar function |
| 8 | +def setValues(): |
| 9 | + print(" ") |
| 10 | + |
| 11 | + |
| 12 | +# Creating the trackbars needed for |
| 13 | +# adjusting the marker colour These |
| 14 | +# trackbars will be used for setting |
| 15 | +# the upper and lower ranges of the |
| 16 | +# HSV required for particular colour |
| 17 | +cv2.namedWindow("Color detectors") |
| 18 | +cv2.createTrackbar("Upper Hue", "Color detectors", 153, 180, setValues) |
| 19 | +cv2.createTrackbar("Upper Saturation", "Color detectors", 255, 255, setValues) |
| 20 | +cv2.createTrackbar("Upper Value", "Color detectors", 255, 255, setValues) |
| 21 | +cv2.createTrackbar("Lower Hue", "Color detectors", 64, 180, setValues) |
| 22 | +cv2.createTrackbar("Lower Saturation", "Color detectors", 72, 255, setValues) |
| 23 | +cv2.createTrackbar("Lower Value", "Color detectors", 49, 255, setValues) |
| 24 | + |
| 25 | +# Giving different arrays to handle colour |
| 26 | +# points of different colour These arrays |
| 27 | +# will hold the points of a particular colour |
| 28 | +# in the array which will further be used |
| 29 | +# to draw on canvas |
| 30 | +bpoints = [deque(maxlen=1024)] |
| 31 | +gpoints = [deque(maxlen=1024)] |
| 32 | +rpoints = [deque(maxlen=1024)] |
| 33 | +ypoints = [deque(maxlen=1024)] |
| 34 | + |
| 35 | +# These indexes will be used to mark position |
| 36 | +# of pointers in colour array |
| 37 | +blue_index = 0 |
| 38 | +green_index = 0 |
| 39 | +red_index = 0 |
| 40 | +yellow_index = 0 |
| 41 | + |
| 42 | +# The kernel to be used for dilation purpose |
| 43 | +kernel = np.ones((5, 5), np.uint16) |
| 44 | + |
| 45 | +# The colours which will be used as ink for |
| 46 | +# the drawing purpose |
| 47 | +colors = [(255, 0, 0), (0, 255, 0),(0, 0, 255), (0, 255, 255)] |
| 48 | +colorIndex = 0 |
| 49 | + |
| 50 | +# Here is code for Canvas setup |
| 51 | +paintWindow = np.zeros((471, 636, 3)) + 255 |
| 52 | +paintWindow = cv2.rectangle(paintWindow, (40,1), (140,65), (0,0,0), 2) |
| 53 | +paintWindow = cv2.rectangle(paintWindow, (160,1), (255,65), colors[0], -1) |
| 54 | +paintWindow = cv2.rectangle(paintWindow, (275,1), (370,65), colors[1], -1) |
| 55 | +paintWindow = cv2.rectangle(paintWindow, (390,1), (485,65), colors[2], -1) |
| 56 | +paintWindow = cv2.rectangle(paintWindow, (505,1), (600,65), colors[3], -1) |
| 57 | + |
| 58 | +cv2.putText(paintWindow, "CLEAR", (49, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2, cv2.LINE_AA) |
| 59 | +cv2.putText(paintWindow, "BLUE", (185, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA) |
| 60 | +cv2.putText(paintWindow, "GREEN", (298, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA) |
| 61 | +cv2.putText(paintWindow, "RED", (420, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA) |
| 62 | +cv2.putText(paintWindow, "YELLOW", (520, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (150,150,150), 2, cv2.LINE_AA) |
| 63 | +cv2.namedWindow('Paint', cv2.WINDOW_AUTOSIZE) |
| 64 | + |
| 65 | +# Loading the default webcam of PC. |
| 66 | +cap = cv2.VideoCapture(0) |
| 67 | + |
| 68 | +# Keep looping |
| 69 | +while True: |
| 70 | + |
| 71 | + # Reading the frame from the camera |
| 72 | + ret, frame = cap.read() |
| 73 | + |
| 74 | + # Flipping the frame to see same side of yours |
| 75 | + frame = cv2.flip(frame, 1) |
| 76 | + hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) |
| 77 | + |
| 78 | + # Getting the updated positions of the trackbar |
| 79 | + # and setting the HSV values |
| 80 | + u_hue = cv2.getTrackbarPos("Upper Hue","Color detectors") |
| 81 | + u_saturation = cv2.getTrackbarPos("Upper Saturation","Color detectors") |
| 82 | + u_value = cv2.getTrackbarPos("Upper Value","Color detectors") |
| 83 | + l_hue = cv2.getTrackbarPos("Lower Hue","Color detectors") |
| 84 | + l_saturation = cv2.getTrackbarPos("Lower Saturation","Color detectors") |
| 85 | + l_value = cv2.getTrackbarPos("Lower Value","Color detectors") |
| 86 | + Upper_hsv = np.array([u_hue, u_saturation, u_value]) |
| 87 | + Lower_hsv = np.array([l_hue, l_saturation, l_value]) |
| 88 | + |
| 89 | + # Adding the colour buttons to the live frame |
| 90 | + # for colour access |
| 91 | + frame = cv2.rectangle(frame, (40, 1), (140, 65),(122, 122, 122), -1) |
| 92 | + frame = cv2.rectangle(frame, (160, 1), (255, 65),colors[0], -1) |
| 93 | + frame = cv2.rectangle(frame, (275, 1), (370, 65),colors[1], -1) |
| 94 | + frame = cv2.rectangle(frame, (390, 1), (485, 65),colors[2], -1) |
| 95 | + frame = cv2.rectangle(frame, (505, 1), (600, 65),colors[3], -1) |
| 96 | + |
| 97 | + cv2.putText(frame, "CLEAR ALL", (49, 33),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(255, 255, 255), 2, cv2.LINE_AA) |
| 98 | + |
| 99 | + cv2.putText(frame, "BLUE", (185, 33),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(255, 255, 255), 2, cv2.LINE_AA) |
| 100 | + |
| 101 | + cv2.putText(frame, "GREEN", (298, 33),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(255, 255, 255), 2, cv2.LINE_AA) |
| 102 | + |
| 103 | + cv2.putText(frame, "RED", (420, 33),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(255, 255, 255), 2, cv2.LINE_AA) |
| 104 | + |
| 105 | + cv2.putText(frame, "YELLOW", (520, 33),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(150, 150, 150), 2, cv2.LINE_AA) |
| 106 | + |
| 107 | + # Identifying the pointer by making its |
| 108 | + # mask |
| 109 | + Mask = cv2.inRange(hsv, Lower_hsv, Upper_hsv) |
| 110 | + Mask = cv2.erode(Mask, kernel, iterations=1) |
| 111 | + Mask = cv2.morphologyEx(Mask, cv2.MORPH_OPEN, kernel) |
| 112 | + Mask = cv2.dilate(Mask, kernel, iterations=1) |
| 113 | + |
| 114 | + # Find contours for the pointer after |
| 115 | + # identifying it |
| 116 | + _,cnts,_ = cv2.findContours(Mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
| 117 | + center = None |
| 118 | + |
| 119 | + # If the contours are formed |
| 120 | + if len(cnts) > 0: |
| 121 | + # sorting the contours to find biggest |
| 122 | + cnt = sorted(cnts, key=cv2.contourArea, reverse=True)[0] |
| 123 | + |
| 124 | + # Get the radius of the enclosing circle |
| 125 | + # around the found contour |
| 126 | + ((x, y), radius) = cv2.minEnclosingCircle(cnt) |
| 127 | + |
| 128 | + # Draw the circle around the contour |
| 129 | + cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2) |
| 130 | + |
| 131 | + # Calculating the center of the detected contour |
| 132 | + M = cv2.moments(cnt) |
| 133 | + center = (int(M['m10'] / M['m00']), int(M['m01'] / M['m00'])) |
| 134 | + |
| 135 | + # Now checking if the user wants to click on |
| 136 | + # any button above the screen |
| 137 | + if center[1] <= 65: |
| 138 | + |
| 139 | + # Clear Button |
| 140 | + if 40 <= center[0] <= 140: |
| 141 | + bpoints = [deque(maxlen=512)] |
| 142 | + gpoints = [deque(maxlen=512)] |
| 143 | + rpoints = [deque(maxlen=512)] |
| 144 | + ypoints = [deque(maxlen=512)] |
| 145 | + |
| 146 | + blue_index = 0 |
| 147 | + green_index = 0 |
| 148 | + red_index = 0 |
| 149 | + yellow_index = 0 |
| 150 | + |
| 151 | + paintWindow[67:, :, :] = 255 |
| 152 | + elif 160 <= center[0] <= 255: |
| 153 | + colorIndex = 0 # Blue |
| 154 | + elif 275 <= center[0] <= 370: |
| 155 | + colorIndex = 1 # Green |
| 156 | + elif 390 <= center[0] <= 485: |
| 157 | + colorIndex = 2 # Red |
| 158 | + elif 505 <= center[0] <= 600: |
| 159 | + colorIndex = 3 # Yellow |
| 160 | + else: |
| 161 | + if colorIndex == 0: |
| 162 | + bpoints[blue_index].appendleft(center) |
| 163 | + elif colorIndex == 1: |
| 164 | + gpoints[green_index].appendleft(center) |
| 165 | + elif colorIndex == 2: |
| 166 | + rpoints[red_index].appendleft(center) |
| 167 | + elif colorIndex == 3: |
| 168 | + ypoints[yellow_index].appendleft(center) |
| 169 | + |
| 170 | + # Append the next deques when nothing is |
| 171 | + # detected to avoid messing up |
| 172 | + else: |
| 173 | + bpoints.append(deque(maxlen=512)) |
| 174 | + blue_index += 1 |
| 175 | + gpoints.append(deque(maxlen=512)) |
| 176 | + green_index += 1 |
| 177 | + rpoints.append(deque(maxlen=512)) |
| 178 | + red_index += 1 |
| 179 | + ypoints.append(deque(maxlen=512)) |
| 180 | + yellow_index += 1 |
| 181 | + |
| 182 | + # Draw lines of all the colors on the |
| 183 | + # canvas and frame |
| 184 | + points = [bpoints, gpoints, rpoints, ypoints] |
| 185 | + for i in range(len(points)): |
| 186 | + |
| 187 | + for j in range(len(points[i])): |
| 188 | + |
| 189 | + for k in range(1, len(points[i][j])): |
| 190 | + |
| 191 | + if points[i][j][k - 1] is None or points[i][j][k] is None: |
| 192 | + continue |
| 193 | + |
| 194 | + cv2.line(frame, points[i][j][k - 1], points[i][j][k], colors[i], 2) |
| 195 | + cv2.line(paintWindow, points[i][j][k - 1], points[i][j][k], colors[i], 2) |
| 196 | + |
| 197 | + # Show all the windows |
| 198 | + cv2.imshow("Tracking", frame) |
| 199 | + cv2.imshow("Paint", paintWindow) |
| 200 | + |
| 201 | + if cv2.waitKey(4) & 0xFF == ord("q"): |
| 202 | + break |
| 203 | + |
| 204 | +# Release the camera and all resources |
| 205 | +cap.release() |
| 206 | +cv2.destroyAllWindows() |
| 207 | + |
0 commit comments