-
Notifications
You must be signed in to change notification settings - Fork 0
/
paint.py
174 lines (128 loc) · 5.47 KB
/
paint.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from mimetypes import init
from cv2 import VideoCapture, flip, COLOR_BGR2RGB, FONT_HERSHEY_COMPLEX, putText, cvtColor, circle, FILLED, destroyAllWindows, imshow, CAP_PROP_FRAME_WIDTH, CAP_PROP_FRAME_HEIGHT
import mediapipe.python.solutions.drawing_utils as mp_drawing
import mediapipe.python.solutions.hands as mp_hands
import pygame
import math
import pyautogui
import os
import time
WIDTH, HEIGHT = 640, 480
# COLOURS
RED = (255, 0, 0)
VIOLET = (148, 0, 211)
INDIGO = (75, 0, 130)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
YELLOW = (255, 255, 0)
ORANGE = (255, 127, 0)
WHITE = (255, 255, 255)
color = RED
rainbow = [VIOLET, INDIGO, BLUE, GREEN, YELLOW, ORANGE, RED]
desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
print(desktop)
ss_count = 1
run = True
pygame.init()
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Drawing Board")
hands = mp_hands.Hands(max_num_hands=1, min_tracking_confidence=0.9)
camera = VideoCapture(0)
camera.set(CAP_PROP_FRAME_WIDTH, WIDTH)
camera.set(CAP_PROP_FRAME_HEIGHT, HEIGHT)
def generate_rainbow(rainbow):
it = iter(rainbow)
while True:
yield next(it)
try:
yield next(it)
except StopIteration:
it = iter(rainbow)
def convert_to_pixel_coordinates():
normalized_landmark = handlms.landmark[id]
pixel_coordinate = mp_drawing._normalized_to_pixel_coordinates(normalized_landmark.x,
normalized_landmark.y, width, height)
return pixel_coordinate
gen_rainbow = generate_rainbow(rainbow)
window.fill(WHITE)
# start_frame = 0
# start_time = 0
inital_time = 0
while run:
ret, frame = camera.read()
frame = flip(frame, 1)
imgRGB = cvtColor(frame, COLOR_BGR2RGB)
results = hands.process(imgRGB)
hand = results.multi_hand_landmarks
# delta_time = time.time() - start_time
# fps = 1/delta_time
# start_time = time.time()
# putText(frame, "f FPS: {fps}", (5, 40), FONT_HERSHEY_COMPLEX, 1, GREEN)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_q]:
run = False
elif keys[pygame.K_c]:
window.fill(WHITE)
elif keys[pygame.K_r]:
color = RED
elif keys[pygame.K_b]:
color = BLUE
elif keys[pygame.K_y]:
color = YELLOW
elif keys[pygame.K_g]:
color = GREEN
elif keys[pygame.K_o]:
color = ORANGE
elif keys[pygame.K_l]:
color = next(gen_rainbow)
elif keys[pygame.K_e]:
color = WHITE
if hand:
for handlms in hand:
for id, lm in enumerate(handlms.landmark):
height, width, c = frame.shape
cx, cy = int(lm.x * width), int(lm.y * height)
if id == 4:
circle(frame, (cx, cy), 15, (255, 0, 0), FILLED)
thumb_coordinates = convert_to_pixel_coordinates()
if id == 8:
circle(frame, (cx, cy), 15, (255, 0, 255), FILLED)
index_coordinates = convert_to_pixel_coordinates()
try:
x_distance = index_coordinates[0] - thumb_coordinates[0]
y_distance = index_coordinates[1] - thumb_coordinates[1]
distance = math.sqrt((x_distance ** 2) + (y_distance ** 2))
if 0 <= distance <= 20:
pygame.draw.circle(window, color,
(index_coordinates[0], index_coordinates[1]), 5)
except TypeError:
print("Your hand is out of bounds!")
if id == 12:
circle(frame, (cx, cy), 15, (255, 255, 0), FILLED)
middle_tip_coordinates = convert_to_pixel_coordinates()
try:
x_middle_distance = index_coordinates[0] - middle_tip_coordinates[0]
y_middle_distance = index_coordinates[1] - middle_tip_coordinates[1]
distance_middle_to_index = distance = math.sqrt((x_middle_distance ** 2) + (y_middle_distance ** 2))
if 0 <= distance_middle_to_index <= 20:
print(f"CURRENT TIME {time.time()}")
delta = time.time() - inital_time
if delta >= 5:
print("DELTA IS GREATER THAN 5")
image = pyautogui.screenshot()
image.save(desktop + f'screenshot{ss_count}.png')
print(desktop)
ss_count += 1
inital_time = time.time()
print(f"Updated initial time to {inital_time}")
except TypeError:
print("Error when taking screenshot or your hand is still out of bounds")
mp_drawing.draw_landmarks(frame, handlms, mp_hands.HAND_CONNECTIONS)
pygame.display.update()
imshow("Window", frame)
camera.release()
destroyAllWindows()
pygame.quit()