Shahid0812 commited on
Commit
377ccda
·
verified ·
1 Parent(s): 1005293

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import face_recognition
3
+ import requests
4
+ import os
5
+ from deepface import DeepFace
6
+
7
+ # --- CONFIGURATION ---
8
+ KOKORO_API_URL = "https://shahid202-kokoro-api.hf.space/generate" # Adjust endpoint if different
9
+ OWNER_IMAGE_PATH = "faces/owner.jpg"
10
+ TEMP_AUDIO = "response.wav"
11
+
12
+ # Load Owner Face
13
+ owner_image = face_recognition.load_image_file(OWNER_IMAGE_PATH)
14
+ owner_encoding = face_recognition.face_encodings(owner_image)[0]
15
+
16
+ def get_bot_response(user_msg, mood):
17
+ """
18
+ Simulates your bot logic.
19
+ It combines your message with the detected mood.
20
+ """
21
+ # Integrate your existing bot code here
22
+ return f"I see you are feeling {mood}. You said: {user_msg}"
23
+
24
+ def speak(text):
25
+ """Sends text to Kokoro API and plays the result."""
26
+ payload = {
27
+ "text": text,
28
+ "voice": "af_bella", # Or your preferred voice ID
29
+ "speed": 1.0
30
+ }
31
+ try:
32
+ response = requests.post(KOKORO_API_URL, json=payload)
33
+ if response.status_code == 200:
34
+ with open(TEMP_AUDIO, "wb") as f:
35
+ f.write(response.content)
36
+ # Use OS command to play audio (Windows: start, Mac: afplay, Linux: aplay)
37
+ os.system(f"start {TEMP_AUDIO}")
38
+ except Exception as e:
39
+ print(f"Speech Error: {e}")
40
+
41
+ def run_space():
42
+ video_capture = cv2.VideoCapture(0)
43
+ print("System Active. Press 'q' to quit.")
44
+
45
+ while True:
46
+ ret, frame = video_capture.read()
47
+ if not ret: break
48
+
49
+ # 1. Face Recognition
50
+ rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
51
+ face_locations = face_recognition.face_locations(rgb_frame)
52
+ face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
53
+
54
+ status = "No Face"
55
+ mood = "Unknown"
56
+
57
+ for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
58
+ matches = face_recognition.compare_faces([owner_encoding], face_encoding)
59
+
60
+ if True in matches:
61
+ status = "Owner recognized"
62
+ # 2. Mood Detection (DeepFace)
63
+ try:
64
+ analysis = DeepFace.analyze(frame, actions=['emotion'], enforce_detection=False)
65
+ mood = analysis[0]['dominant_emotion']
66
+ except:
67
+ mood = "Neutral"
68
+
69
+ color = (0, 255, 0) # Green for Owner
70
+ else:
71
+ status = "Stranger"
72
+ mood = "N/A"
73
+ color = (0, 0, 255) # Red for Stranger
74
+
75
+ # Draw Box
76
+ cv2.rectangle(frame, (left, top), (right, bottom), color, 2)
77
+ cv2.putText(frame, f"{status} ({mood})", (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
78
+
79
+ cv2.imshow('AI Assistant Camera', frame)
80
+
81
+ # 3. Input Handling (Press 's' to Send Message while looking at camera)
82
+ key = cv2.waitKey(1) & 0xFF
83
+ if key == ord('s'):
84
+ if status == "Owner recognized":
85
+ msg = input("Enter message for Bot: ")
86
+ reply = get_bot_response(msg, mood)
87
+ print(f"Bot: {reply}")
88
+ speak(reply)
89
+ elif status == "Stranger":
90
+ speak("Stranger detected. Access denied.")
91
+
92
+ if key == ord('q'):
93
+ break
94
+
95
+ video_capture.release()
96
+ cv2.destroyAllWindows()
97
+
98
+ if __name__ == "__main__":
99
+ run_space()
100
+