Skip to content

Commit 320a4bf

Browse files
Add files via upload
1 parent 687d6f1 commit 320a4bf

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed

GAMES/VirtualHandPainter/main.py

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import time
2+
3+
start = time.time()
4+
from cv2 import VideoCapture, flip, COLOR_BGR2RGB, cvtColor, circle, FILLED, destroyAllWindows, imshow, CAP_PROP_FRAME_WIDTH, CAP_PROP_FRAME_HEIGHT
5+
print("Importing cv stuff")
6+
import mediapipe.python.solutions.drawing_utils as mp_drawing
7+
print("Importing hand module stuff")
8+
9+
import mediapipe.python.solutions.hands as mp_hands
10+
print("Importing more hand module stuff")
11+
12+
import pygame
13+
print("Importing pygame")
14+
15+
from math import sqrt
16+
print("Importing math")
17+
18+
import pyautogui
19+
import os
20+
print("Importing other stuff")
21+
22+
23+
end = time.time()
24+
print(end - start)
25+
26+
# 0.75s initial
27+
# 0.73s final
28+
# 3.5 x faster
29+
30+
31+
# 640, 480
32+
WIDTH, HEIGHT = 1000, 1000
33+
34+
# COLOURS
35+
RED = (255, 0, 0)
36+
VIOLET = (148, 0, 211)
37+
INDIGO = (75, 0, 130)
38+
BLUE = (0, 0, 255)
39+
GREEN = (0, 255, 0)
40+
YELLOW = (255, 255, 0)
41+
ORANGE = (255, 127, 0)
42+
WHITE = (255, 255, 255)
43+
44+
color = RED
45+
rainbow = [VIOLET, INDIGO, BLUE, GREEN, YELLOW, ORANGE, RED]
46+
47+
desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
48+
ss_count = 1
49+
50+
run = True
51+
52+
pygame.init()
53+
54+
window = pygame.display.set_mode((WIDTH, HEIGHT))
55+
pygame.display.set_caption("Drawing Board")
56+
57+
hands = mp_hands.Hands(max_num_hands=1, min_tracking_confidence=0.9)
58+
camera = VideoCapture(0)
59+
60+
camera.set(CAP_PROP_FRAME_WIDTH, WIDTH)
61+
camera.set(CAP_PROP_FRAME_HEIGHT, HEIGHT)
62+
63+
64+
65+
66+
def generate_rainbow(rainbow):
67+
it = iter(rainbow)
68+
while True:
69+
yield next(it)
70+
try:
71+
yield next(it)
72+
except StopIteration:
73+
it = iter(rainbow)
74+
75+
76+
def convert_to_pixel_coordinates():
77+
normalized_landmark = handlms.landmark[id]
78+
pixel_coordinate = mp_drawing._normalized_to_pixel_coordinates(normalized_landmark.x,
79+
normalized_landmark.y, width, height)
80+
return pixel_coordinate
81+
82+
83+
gen_rainbow = generate_rainbow(rainbow)
84+
window.fill(WHITE)
85+
86+
while run:
87+
ret, frame = camera.read()
88+
frame = flip(frame, 1)
89+
imgRGB = cvtColor(frame, COLOR_BGR2RGB)
90+
results = hands.process(imgRGB)
91+
hand = results.multi_hand_landmarks
92+
93+
for event in pygame.event.get():
94+
if event.type == pygame.QUIT:
95+
run = False
96+
keys = pygame.key.get_pressed()
97+
if keys[pygame.K_q]:
98+
run = False
99+
if keys[pygame.K_c]:
100+
window.fill(WHITE)
101+
if keys[pygame.K_r]:
102+
color = RED
103+
if keys[pygame.K_b]:
104+
color = BLUE
105+
if keys[pygame.K_y]:
106+
color = YELLOW
107+
if keys[pygame.K_g]:
108+
color = GREEN
109+
if keys[pygame.K_o]:
110+
color = ORANGE
111+
if keys[pygame.K_l]:
112+
color = next(gen_rainbow)
113+
114+
if hand:
115+
for handlms in hand:
116+
for id, lm in enumerate(handlms.landmark):
117+
height, width, c = frame.shape
118+
cx, cy = int(lm.x * width), int(lm.y * height)
119+
120+
if id == 4:
121+
circle(frame, (cx, cy), 15, (255, 0, 0), FILLED)
122+
thumb_coordinates = convert_to_pixel_coordinates()
123+
124+
if id == 8:
125+
circle(frame, (cx, cy), 15, (255, 0, 255), FILLED)
126+
index_coordinates = convert_to_pixel_coordinates()
127+
128+
try:
129+
x_distance = index_coordinates[0] - thumb_coordinates[0]
130+
y_distance = index_coordinates[1] - thumb_coordinates[1]
131+
distance = sqrt((x_distance ** 2) + (y_distance ** 2))
132+
133+
# try:
134+
if 0 <= distance <= 20:
135+
pygame.draw.circle(window, color,
136+
(index_coordinates[0], index_coordinates[1]), 5)
137+
except TypeError:
138+
print("ERROR")
139+
140+
if id == 12:
141+
circle(frame, (cx, cy), 15, (255, 255, 0), FILLED)
142+
middle_tip_coordinates = convert_to_pixel_coordinates()
143+
144+
try:
145+
146+
x_middle_distance = index_coordinates[0] - middle_tip_coordinates[0]
147+
y_middle_distance = index_coordinates[1] - middle_tip_coordinates[1]
148+
distance_middle_to_index = distance = sqrt(
149+
(x_middle_distance ** 2) + (y_middle_distance ** 2))
150+
151+
if 0 <= distance_middle_to_index <= 20:
152+
print(desktop)
153+
image = pyautogui.screenshot()
154+
image.save(desktop + f"/screenshot{ss_count}.png")
155+
ss_count += 1
156+
157+
except TypeError:
158+
print("error")
159+
160+
mp_drawing.draw_landmarks(frame, handlms, mp_hands.HAND_CONNECTIONS)
161+
162+
pygame.display.update()
163+
imshow("Window", frame)
164+
165+
camera.release()
166+
destroyAllWindows()
167+
pygame.quit()

0 commit comments

Comments
 (0)