-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
91 lines (86 loc) · 2.83 KB
/
main.py
File metadata and controls
91 lines (86 loc) · 2.83 KB
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
from draw import *
import time
import mediapipe as mp
import cv2
import datetime
import warnings
import sys
warnings.filterwarnings("ignore")
try:
print(ascii_art)
print(f"FingerDraw v{version}\n")
print(f"New in v1.1:\n{patches}")
print("")
mp_hands = mp.solutions.hands
hands = mp_hands.Hands()
print("Starting Camera...")
cap = cv2.VideoCapture(1)
cv2.startWindowThread()
text = "Black"
# Initialize drawing utils
pen = Pen()
pen.coordinate_list.append([23456, 23456, pen.colors])
array = []
if not cap.isOpened():
print("Error: Camera not accessible")
raise Exception("Camera not accessible")
finger_detected = True
while cap.isOpened():
suc, img = cap.read()
if(suc != True):
print("camera not opening")
continue
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
res = hands.process(img2)
if res.multi_hand_landmarks:
pen.get_dist(res)
if pen.dist < 0.065:
pen.update_current_location(res)
array = pen.coordinate_list[-1]
finger_detected = False
if finger_detected:
if len(pen.coordinate_list) >= 1:
if pen.coordinate_list[-1][0] != 100000:
pen.separate_plot()
finger_detected = True
else:
if finger_detected and pen.coordinate_list[-1][0] != 100000:
pen.separate_plot()
finger_detected = False
pen.draw(img)
img = cv2.flip(img, 1)
key = cv2.waitKey(1) & 0xFF
if key == ord('e'):
pen.clear_canvas()
elif chr(key) in color_map:
pen.colors, text = color_map[chr(key)]
pen.separate_plot()
elif key == ord('p'):
print(pen.coordinate_list)
elif key == ord('q'):
break
elif key == ord('z'):
pen.undo()
elif key == ord('s'):
imname = f"{datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")}.png"
cv2.imwrite(imname, img)
cv2.putText(img,f"Image saved as{imname}",(0,200),cv2.FONT_HERSHEY_PLAIN, 8, pen.colors, 5)
cv2.putText(img, text, (0,130), cv2.FONT_HERSHEY_PLAIN, 12, pen.colors, 5)
cv2.imshow('FingerDraw', img)
except Exception as e:
print('\033[91m' + f"ERROR OCCURED: {e}" + '\033[0m')
print("Doing cleanup job...")
imname = f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}.png"
print("Saving your drawings as png")
cv2.imwrite(imname, img)
cap.release()
cv2.destroyAllWindows()
for i in range(1, 5):
cv2.waitKey(1)
sys.exit(250)
finally:
cap.release()
cv2.destroyAllWindows()
for i in range(1, 5):
cv2.waitKey(1)
print('\033[92m' + "Done." + '\033[0m')