-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
95 lines (80 loc) · 3.19 KB
/
Copy pathtest.py
File metadata and controls
95 lines (80 loc) · 3.19 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
import cv2
from cvzone.HandTrackingModule import HandDetector
import numpy as np
from pynput.keyboard import Controller
cap = cv2.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)
detector = HandDetector(detectionCon=0.8)
keys = [["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
["A", "S", "D", "F", "G", "H", "J", "K", "L", ";"],
["Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"]]
finalText = ""
keyboard = Controller()
class Button():
def __init__(self, pos, text, size = [80,80]):
self.pos = pos
self.size = size
self.text = text
def draw(self, img):
x, y = self.pos
w, h = self.size
# Draw the rectangle and put the text
cv2.rectangle(img, self.pos, (x + w, y + h), (255, 0, 255), cv2.FILLED)
cv2.putText(img, self.text, (x + 20, y + 65),
cv2.FONT_HERSHEY_PLAIN, 4, (255, 255, 255), 4)
return img
def drawALL(img, buttonList):
for button in buttonList:
img = button.draw(img)
return img
buttonList = []
for i in range(len(keys)):
for x, key in enumerate(keys[i]):
buttonList.append(Button([100 * x + 50, 100 + i * 100], key))
button_clicked = False
while True:
success, img = cap.read()
if success:
hands, img = detector.findHands(img) # Get the modified image
result = detector.findPosition(img)
lmList = [] # Define lmList here
img = drawALL(img, buttonList)
if result is not None:
lmList, bboxInfo = result
print(lmList)
else:
print("No position found in the image.")
if lmList: # Now you can use lmList here
for button in buttonList:
x, y = button.pos
w,h = button.size
if x < lmList[8][0] < x + w and y < lmList[8][1] < y + h:
cv2.rectangle(img, button.pos, (x + w, y + h), (175, 0, 175), cv2.FILLED)
cv2.putText(img, button.text, (x + 20, y + 65),
cv2.FONT_HERSHEY_PLAIN, 4, (255, 255, 255), 4)
l, _, _ = detector.findDistance(8, 12, img, draw = False)
print(l)
if l<30 and not button_clicked:
cv2.rectangle(img, button.pos, (x + w, y + h), (0, 255, 0), cv2.FILLED)
cv2.putText(img, button.text, (x + 20, y + 65),
cv2.FONT_HERSHEY_PLAIN, 4, (255, 255, 255), 4)
finalText += button.text
keyboard.press(button.text)
keyboard.release(button.text)
button_clicked = True
elif l>30:
button_clicked = False
cv2.rectangle(img, (50, 450), (700, 550), (175, 0, 175), cv2.FILLED)
cv2.putText(img, finalText, (60, 525),
cv2.FONT_HERSHEY_PLAIN, 5, (255, 255, 255), 5)
cv2.imshow('Image', img) # Display the frame
# Exit on pressing 'x'
if cv2.waitKey(1) == ord('x'):
break
else:
print("Failed to capture a frame.")
break
# Release the camera and close the window
cap.release()
cv2.destroyAllWindows()