Skip to content

Instantly share code, notes, and snippets.

@haseebpvt
Created April 24, 2023 11:49
Show Gist options
  • Select an option

  • Save haseebpvt/6a7a0ac56f1e312be48da7cc748f9f84 to your computer and use it in GitHub Desktop.

Select an option

Save haseebpvt/6a7a0ac56f1e312be48da7cc748f9f84 to your computer and use it in GitHub Desktop.
import os
import cv2
import numpy as np
import speech_recognition as sr
import pyttsx3
from threading import Thread
def load_face_data():
face_data = []
labels = []
people = []
for subdir, dirs, files in os.walk('faces'):
for dir in dirs:
people.append(dir)
person_path = os.path.join(subdir, dir)
for filename in os.listdir(person_path):
img = cv2.imread(os.path.join(person_path, filename), cv2.IMREAD_GRAYSCALE)
face_data.append(img)
labels.append(len(people) - 1)
return face_data, np.array(labels), people
def train_face_recognizer(face_data, labels):
face_recognizer = cv2.face.LBPHFaceRecognizer_create()
face_recognizer.train(face_data, labels)
return face_recognizer
def text_to_speech(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
def speech_to_text():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
return text
except Exception as e:
print(e)
return ""
def main():
face_data, labels, people = load_face_data()
face_recognizer = train_face_recognizer(face_data, labels)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
label, confidence = face_recognizer.predict(gray[y:y+h, x:x+w])
name = people[label]
cv2.putText(frame, name, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2)
text_to_speech("Hello " + name)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
text = speech_to_text()
if text:
print("Recognized Speech:", text)
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment