· 6 years ago · Mar 12, 2020, 11:26 AM
1import speech_recognition as sr
2import tkinter as tk
3from tkinter import simpledialog
4from tkinter import messagebox
5import cv2
6from shutil import copyfile
7from PIL import Image
8
9
10def recognize_from_mic(r=sr.Recognizer(), m=sr.Microphone()):
11 if not isinstance(r, sr.Recognizer):
12 print("[recognize_from_mic]: 'r' is not an instance of sr.Recognizer")
13 if not isinstance(m, sr.Microphone):
14 print("[recognize_from_mic]: 'm' is not an instance of sr.Microphone")
15
16 with m as source:
17 r.adjust_for_ambient_noise(source)
18 audio = r.listen(source)
19
20 response_state = {
21 "success": False,
22 "status": None,
23 "text": None
24 }
25
26 try:
27 response_state["text"] = r.recognize_google(audio, language="en-US")
28 except sr.RequestError:
29 response_state["status"] = "API Failure"
30 except sr.UnknownValueError:
31 response_state["status"] = "Couldn't understand what you said."
32 else:
33 response_state["status"] = "Success"
34 response_state["success"] = True
35
36 return response_state
37
38
39def recognize_from_file(r=sr.Recognizer(), file=None):
40 if file is None:
41 raise FileExistsError("No error is specified")
42 if not isinstance(r, sr.Recognizer):
43 print("[recognize_from_mic]: 'r' is not an instance of sr.Recognizer")
44
45 with sr.AudioFile(file) as source:
46 r.adjust_for_ambient_noise(source)
47 audio = r.record(source)
48 audio = r.recognize_google(audio, show_all=True)
49
50 return audio
51
52
53w = None
54
55import face_recognition
56import os
57
58
59def facedetect():
60 path = "faces/"
61 face_encodings_d = {}
62
63 for photo in os.scandir(path):
64 if photo.is_file():
65 img = face_recognition.load_image_file(path + photo.name)
66 face_encodings_d[photo.name.split(".")[0]] = face_recognition.face_encodings(img)[0]
67
68 print(face_encodings_d)
69
70 cv2.namedWindow("preview")
71 vc = cv2.VideoCapture(0)
72
73 if vc.isOpened():
74 rval, frame = vc.read()
75 else:
76 rval = False
77
78 while rval:
79 cv2.imshow("preview", frame)
80 rval, frame = vc.read()
81 key = cv2.waitKey(20)
82 if key == 27:
83 im = Image.fromarray(frame)
84 im.save("tmp.jpg")
85 unknown_img = face_recognition.load_image_file("tmp.jpg")
86 break
87
88 cv2.destroyWindow("preview")
89
90 try:
91 unknown_face_encoding = face_recognition.face_encodings(unknown_img)[0]
92 except IndexError:
93 print("I wasnt..")
94 return
95
96 known_faces = list(face_encodings_d.values())
97 results = face_recognition.compare_faces(known_faces, unknown_face_encoding)
98
99 for (i, name) in enumerate(face_encodings_d.keys()):
100 print(f"Is {name}? {results[i]}")
101 if results[i]:
102 messagebox.showinfo("Face detected", f"Hello {name}!")
103
104 if not any(results):
105 answer = simpledialog.askstring("Who are you?", "What is your first name?", parent=w)
106 copyfile("tmp.jpg", path + answer + ".jpg")
107
108
109def button_speak():
110 close_commands = ["close", "stop", "shutdown", "shut", "destroy", "delete", "kill", "yourself", "suicide",
111 "fuck", "quit", "exit"]
112 face_detection_commands = ["detect", "scan", "face"]
113 open_site_commands = ["open", "site", "go"]
114 text = recognize_from_mic()["text"].lower()
115 print(text)
116
117 for sitecmd in open_site_commands:
118 if sitecmd in text:
119 os.system(r"start Chrome test.html")
120
121 for closecmd in close_commands:
122 if closecmd in text:
123 w.destroy()
124 break
125
126 for facedetectcmd in face_detection_commands:
127 if facedetectcmd in text:
128 facedetect()
129 break
130
131
132if __name__ == "__main__":
133 w = tk.Tk()
134 w.title("Activate HiveCommand")
135 w.geometry("400x50")
136
137 speakButton = tk.Button(w, text="Listen!", command=button_speak)
138 facedetect()
139 speakButton.pack()
140
141 w.mainloop()