-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
133 lines (108 loc) · 4.48 KB
/
main.py
File metadata and controls
133 lines (108 loc) · 4.48 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
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
import cv2
import os
import numpy as np
from HandTracker import HandDetector
from dottedline import drawrect
# ── Config ────────────────────────────────────────────────────────────────
width, height = 1280, 720
frames_folder = "Images"
slide_num = 0
# size of the small camera inset
hs, ws = int(120 * 1.2), int(213 * 1.2)
# gesture-zone (upper-right) for slide controls
ge_thresh_y = 400
ge_thresh_x = 750
# debounce & annotation state
gest_done = False
gest_counter = 0
delay = 15
annotations = [[]]
annot_num = 0
annot_start = False
# ── Load slides ───────────────────────────────────────────────────────────
path_imgs = sorted(os.listdir(frames_folder), key=len)
print("Slides:", path_imgs)
# ── Camera & detector setup ───────────────────────────────────────────────
cap = cv2.VideoCapture(0)
cap.set(3, width)
cap.set(4, height)
detector = HandDetector(detectionCon=0.8, maxHands=1)
# ── Main loop ─────────────────────────────────────────────────────────────
while True:
success, frame = cap.read()
if not success:
break
frame = cv2.flip(frame, 1)
# load & resize current slide
slide_path = os.path.join(frames_folder, path_imgs[slide_num])
slide = cv2.imread(slide_path)
slide = cv2.resize(slide, (width, height))
h, w, _ = slide.shape
# draw the gesture-zone box on the camera feed
drawrect(frame, (width, 0), (ge_thresh_x, ge_thresh_y), (0, 255, 0), 5, 'dotted')
# detect hands & landmarks
hands, frame = detector.findHands(frame)
if hands:
hand = hands[0]
cx, cy = hand["center"]
lm_list = hand["lmList"]
fingers = detector.fingersUp(hand)
# map the index-finger tip into slide coords
x_val = int(np.interp(lm_list[8][0], [width//2, w], [0, width]))
y_val = int(np.interp(lm_list[8][1], [150, height-150], [0, height]))
index_fing = (x_val, y_val)
# ── SLIDE CONTROLS (only in upper-right zone) ─────────────
if not gest_done and cy < ge_thresh_y and cx > ge_thresh_x:
# Next Slide: Palm (all 5 fingers up)
if fingers == [1, 1, 1, 1, 1]:
if slide_num < len(path_imgs) - 1:
gest_done = True
slide_num += 1
annotations = [[]]
annot_num = 0
# Previous Slide: Thumb up only
elif fingers == [1, 0, 0, 0, 0]:
if slide_num > 0:
gest_done = True
slide_num -= 1
annotations = [[]]
annot_num = 0
# Clear/Undo: C-Sign ([0,1,1,1,1])
elif fingers == [0, 1, 1, 1, 1]:
if len(annotations) > 0:
gest_done = True
annotations.pop()
annot_num = max(annot_num - 1, 0)
# ── POINTER MODE (anywhere): L-Sign (thumb+index)
if fingers == [1, 1, 0, 0, 0]:
cv2.circle(slide, index_fing, 6, (0, 0, 255), cv2.FILLED)
annot_start = False
# ── DRAW MODE (anywhere): Index up only
elif fingers == [0, 1, 0, 0, 0]:
if not annot_start:
annot_start = True
annot_num += 1
annotations.append([])
annotations[annot_num].append(index_fing)
cv2.circle(slide, index_fing, 6, (0, 0, 255), cv2.FILLED)
else:
annot_start = False
# debounce slide gestures
if gest_done:
gest_counter += 1
if gest_counter > delay:
gest_done = False
gest_counter = 0
# draw all annotations on the slide
for stroke in annotations:
for i in range(1, len(stroke)):
cv2.line(slide, stroke[i-1], stroke[i], (0, 0, 255), 6)
# overlay camera feed in bottom-right
cam_small = cv2.resize(frame, (ws, hs))
slide[h-hs:h, w-ws:w] = cam_small
cv2.imshow("Slides", slide)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# clean up
cap.release()
cv2.destroyAllWindows()