-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNhanDienQuiz.py
184 lines (161 loc) · 6.23 KB
/
NhanDienQuiz.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import speech_recognition as sr
import tkinter as jra
import cv2
import playsound
import os
import pyautogui
import tkinter.font as tkFont
class Application(jra.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master.title("Phần Mềm Hỗ Trợ Quiz")
self.master.minsize(500, 200)
self.sound = jra.Button(self)
self.sound["text"] = "Hỗ Trợ Bằng Âm Thanh"
self.sound["command"] = self.nhanDienGiongNoi
self.sound.pack()
self.cam = jra.Button(self)
self.cam["text"] = "Hỗ Trợ Bằng Hình Ảnh"
self.cam["command"] = self.chuongTrinhNhanDien
self.cam.pack()
self.quit = jra.Button(self, text="QUIT", command=root.destroy)
self.quit.pack()
self.var = jra.StringVar()
self.label = jra.Label(self, text=0, textvariable=self.var, font=tkFont.Font(family="Lucida Grande", size=40,), foreground='green')
self.label.pack()
self.pack()
self.mainloop()
def chuongTrinhNhanDien(self):
orb = cv2.ORB_create(nfeatures=1000)
path = "quiz"
# path = "bienbao/demo"
listAnh = []
listNameAnh = []
myList = os.listdir(path)
checkA,checkB,checkC,checkD,checkEnd = 0, 0, 0, 0 , 0
for name in myList:
imgNow = cv2.imread(f'{path}/{name}', 0)
listAnh.append(imgNow)
listNameAnh.append(os.path.splitext(name)[0])
print(listNameAnh)
def timDacDiem(listPic):
NhanDang = []
for anh in listPic:
diemKhac, dinhDanh = orb.detectAndCompute(anh, None)
NhanDang.append(dinhDanh)
return NhanDang
def timID(trucTiep, NhanDang, phucTap):
diemKhac2, dinhDanh2 = orb.detectAndCompute(trucTiep, None)
bf = cv2.BFMatcher()
listGiong = []
ketLuan = -1
try:
for des in NhanDang:
matches = bf.knnMatch(des, dinhDanh2, k=2) # 2 value can compare on
good = []
for m, n in matches:
if m.distance < 0.75 * n.distance:
good.append([m])
listGiong.append(len(good))
except:
pass
if len(listGiong) != 0:
if (max(listGiong)) > phucTap:
ketLuan = listGiong.index(max(listGiong))
return ketLuan
Nhandang = timDacDiem(listAnh)
print(len(Nhandang))
quay = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while True:
success, live = quay.read()
liveMauSac = live.copy()
live = cv2.cvtColor(live, cv2.COLOR_BGR2GRAY)
tenCuaAnh = timID(live, Nhandang, 10)
if id != -1:
tempText = listNameAnh[tenCuaAnh]
cv2.putText(liveMauSac, tempText, (35, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (250, 86, 206), 2)
self.var.set(tempText)
if tempText == "Dap An A":
checkA += 1
if checkA >= 10:
pyautogui.click(670, 504)
checkA =0
if tempText == "Dap An B":
checkB += 1
if checkB >= 10:
pyautogui.click(1237, 507)
checkB =0
if tempText == "Dap An C":
checkC += 1
if checkC >= 10:
pyautogui.click(667, 581)
checkC =0
if tempText == "Dap An D":
checkD += 1
if checkD >= 10:
pyautogui.click(1232, 578)
checkD =0
if tempText == "Ket Thuc":
checkEnd += 1
if checkEnd >= 10:
pyautogui.click(1232, 578)
checkEnd =0
playsound.playsound('thankfor.m4a')
cv2.destroyAllWindows()
pyautogui.keyDown('alt')
pyautogui.press('f4')
pyautogui.keyUp('alt')
print(tempText)
diemKhacBiet, dinhDanhBiet = orb.detectAndCompute(live, None)
keypoint = cv2.drawKeypoints(live, diemKhacBiet, None)
cv2.imshow("key point", keypoint)
cv2.imshow("Phan Mem Ho Tro Quiz", liveMauSac)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
return 0
def nhanDienGiongNoi(self):
def switchAnswer(argument):
tempText = argument.lower()
switcher = {
"đáp án a": [670, 504],
"đáp án b": [1237, 507],
"đáp án c": [667, 581],
"đáp án cuối": [1232, 578],
"đóng chương trình": [11, 11],
"kết thúc": [5, 5]
}
print("Đã Chọn ", tempText)
self.var.set(tempText)
return switcher.get(tempText, [69, 69])
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Tôi: ", end='')
audio = r.listen(source, phrase_time_limit=5)
try:
text = r.recognize_google(audio, language="vi")
print(text)
return text
except:
print("Phú không thể nghe thấy gì ")
return 0
playsound.playsound('welcome.mp3')
while True:
playsound.playsound('xacNhan.mp3')
temp = get_audio()
temp = format(temp)
temp = switchAnswer(temp)
print(temp)
if temp != [69, 69]:
if temp == [5, 5]:
break
if temp == [11, 11]:
pyautogui.keyDown('alt')
pyautogui.press('f4')
pyautogui.keyUp('alt')
break
pyautogui.click(temp)
playsound.playsound('thankfor.m4a')
return 1
root = jra.Tk()
app = Application(master=root)