-
Notifications
You must be signed in to change notification settings - Fork 0
/
6).Virtual_Keyboard2.py
96 lines (81 loc) · 2.45 KB
/
6).Virtual_Keyboard2.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
import cv2
import numpy as np
keyboard = np.zeros((600,1000,3),np.uint8)
#dictionary containing the letters, each one associated with an index.
keys_set_1 = {0: "Q", 1: "W", 2: "E", 3: "R", 4: "T",
5: "A", 6: "S", 7: "D", 8: "F", 9: "G",
10: "Z", 11: "X", 12: "C", 13: "V", 14: "B"}
def letter(letter_index, text, letter_light):
# Keys
# Each key is simply a rectangle containing some text. So we define the sizes and we draw the rectangle.
if letter_index == 0:
x = 0
y = 0
elif letter_index == 1:
x = 200
y = 0
elif letter_index == 2:
x = 400
y = 0
elif letter_index == 3:
x = 600
y = 0
elif letter_index == 4:
x = 800
y = 0
elif letter_index == 5:
x = 0
y = 200
elif letter_index == 6:
x = 200
y = 200
elif letter_index == 7:
x = 400
y = 200
elif letter_index == 8:
x = 600
y = 200
elif letter_index == 9:
x = 800
y = 200
elif letter_index == 10:
x = 0
y = 400
elif letter_index == 11:
x = 200
y = 400
elif letter_index == 12:
x = 400
y = 400
elif letter_index == 13:
x = 600
y = 400
elif letter_index == 14:
x = 800
y = 400
width = 200
height = 200
th = 3 # thickness
if letter_light == True:
cv2.rectangle(keyboard, (x + th, y + th), (x + width - th, y + height - th), (255, 255, 255), -1)
else:
cv2.rectangle(keyboard, (x + th, y + th), (x + width - th, y + height - th), (255, 0, 0), th)
# Inside the rectangle now we put the letter. So we define the sizes and style of the text and we center it.
# Text settings
font_letter = cv2.FONT_HERSHEY_PLAIN
font_scale = 10
font_th = 4
text_size = cv2.getTextSize(text, font_letter, font_scale, font_th)[0]
width_text, height_text = text_size[0], text_size[1]
text_x = int((width - width_text) / 2) + x
text_y = int((height + height_text) / 2) + y
cv2.putText(keyboard, text, (text_x, text_y), font_letter, font_scale, (255, 0, 0), font_th)
for i in range(15):
if i == 5:
light = True
else:
light = False
letter(i, keys_set_1[i], light)
cv2.imshow("KEYBOARD",keyboard)
cv2.waitKey(0)
cv2.destroyAllWindows()