ryefoxlime commited on
Commit
51ef5ad
·
1 Parent(s): 6e9c870

Final Version of the Project

Browse files
Files changed (5) hide show
  1. FER/detectfaces.py +1 -31
  2. FER/sever.py +94 -0
  3. FER/test.py +31 -0
  4. Output.py +4 -2
  5. app.py +16 -0
FER/detectfaces.py CHANGED
@@ -1,39 +1,14 @@
1
- from models.PosterV2_7cls import pyramid_trans_expr2
2
  import cv2
3
  import torch
4
  import os
5
  import time
6
  from PIL import Image
7
- from main import RecorderMeter1, RecorderMeter # noqa: F401
8
 
9
- script_dir = os.path.dirname(os.path.abspath(__file__))
10
-
11
- # Construct the full path to the model file
12
- model_path = os.path.join(script_dir,"models","checkpoints","raf-db-model_best.pth")
13
-
14
- # Determine the available device for model execution
15
- if torch.backends.mps.is_available():
16
- device = "mps"
17
- elif torch.cuda.is_available():
18
- device = "cuda"
19
- else:
20
- device = "cpu"
21
-
22
- # Initialize the model with specified image size and number of classes
23
- model = pyramid_trans_expr2(img_size=224, num_classes=7)
24
-
25
- # Wrap the model with DataParallel for potential multi-GPU usage
26
- model = torch.nn.DataParallel(model)
27
-
28
- # Move the model to the chosen device
29
- model = model.to(device)
30
-
31
- # Print the current time
32
  currtime = time.strftime("%H:%M:%S")
33
  print(currtime)
34
 
35
 
36
- def main():
37
  # Load the model checkpoint if it exists
38
  if model_path is not None:
39
  if os.path.isfile(model_path):
@@ -121,8 +96,3 @@ def imagecapture(model):
121
  # Release webcam resources and close OpenCV windows
122
  cap.release()
123
  cv2.destroyAllWindows()
124
-
125
-
126
- # Execute the main function if the script is run directly
127
- if __name__ == "__main__":
128
- main()
 
 
1
  import cv2
2
  import torch
3
  import os
4
  import time
5
  from PIL import Image
 
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  currtime = time.strftime("%H:%M:%S")
8
  print(currtime)
9
 
10
 
11
+ def fer(model_path, device, model):
12
  # Load the model checkpoint if it exists
13
  if model_path is not None:
14
  if os.path.isfile(model_path):
 
96
  # Release webcam resources and close OpenCV windows
97
  cap.release()
98
  cv2.destroyAllWindows()
 
 
 
 
 
FER/sever.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import socket
2
+ import os
3
+ import torch
4
+ from threading import Timer
5
+ import pyttsx3
6
+ import speech_recognition as sr
7
+ from detectfaces import fer
8
+ from models.PosterV2_7cls import pyramid_trans_expr2
9
+ from main import RecorderMeter1, RecorderMeter # noqa: F401
10
+ import time
11
+
12
+ script_dir = os.path.dirname(os.path.abspath(__file__))
13
+
14
+ # Construct the full path to the model file
15
+ model_path = os.path.join(script_dir,"models","checkpoints","raf-db-model_best.pth")
16
+
17
+ # Determine the available device for model execution
18
+ if torch.backends.mps.is_available():
19
+ device = "mps"
20
+ elif torch.cuda.is_available():
21
+ device = "cuda"
22
+ else:
23
+ device = "cpu"
24
+
25
+ # Initialize the model with specified image size and number of classes
26
+ model = pyramid_trans_expr2(img_size=224, num_classes=7)
27
+
28
+ # Wrap the model with DataParallel for potential multi-GPU usage
29
+ model = torch.nn.DataParallel(model)
30
+
31
+ # Move the model to the chosen device
32
+ model = model.to(device)
33
+ fer(model_path=model_path, device=device, model=model)
34
+
35
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
36
+ s.bind(('', 5001))
37
+ s.listen(5)
38
+ print("Bot is Running")
39
+
40
+ def handle_client(clientsocket):
41
+ r = sr.Recognizer()
42
+ m = sr.Microphone()
43
+
44
+ try:
45
+ while True:
46
+ prompt = ''
47
+ print("Speak now:")
48
+ sent = False
49
+ with m as source:
50
+ audio = r.listen(source)
51
+
52
+ try:
53
+ prompt = r.recognize_google(audio)
54
+ print("Tadbot Thinks you said:", prompt)
55
+ sent = True
56
+ except sr.UnknownValueError:
57
+ print("Tadbot could not understand audio. Try Again")
58
+ except sr.RequestError as e:
59
+ print(f"Could not request results from Google Speech Recognition service: {e}")
60
+
61
+ if sent:
62
+ print("please Wait!")
63
+ try:
64
+ clientsocket.send(bytes(prompt, 'utf-8'))
65
+ response = clientsocket.recv(1024).decode("utf-8")
66
+ engine = pyttsx3.init('espeak')
67
+ voices = engine.getProperty('voices')
68
+ engine.setProperty('voice', voices[26].id)
69
+ engine.setProperty('rate', 145)
70
+ engine.say(response)
71
+ engine.runAndWait()
72
+ print("TADBot:", response)
73
+ except (socket.error, ConnectionResetError) as e:
74
+ print(f"Connection error: {e}")
75
+ break # Exit loop if connection breaks
76
+ time.sleep(60) # Wait for 60 seconds before listening again
77
+
78
+ finally:
79
+ clientsocket.close()
80
+ print("Connection Closed")
81
+
82
+
83
+ while True:
84
+ try:
85
+ clientsocket, address = s.accept()
86
+ print(f"Accepted connection from {address}")
87
+ handle_client(clientsocket) #Handle each client in a separate function
88
+ except KeyboardInterrupt:
89
+ print("Server shutting down...")
90
+ break
91
+ except Exception as e:
92
+ print(f"An error occurred: {e}")
93
+
94
+ s.close()
FER/test.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from detectfaces import fer
2
+ from models.PosterV2_7cls import pyramid_trans_expr2
3
+ import os
4
+ import torch
5
+ from main import RecorderMeter1, RecorderMeter # noqa: F401
6
+ script_dir = os.path.dirname(os.path.abspath(__file__))
7
+
8
+ # Construct the full path to the model file
9
+ model_path = os.path.join(script_dir,"models","checkpoints","raf-db-model_best.pth")
10
+
11
+ # Determine the available device for model execution
12
+ if torch.backends.mps.is_available():
13
+ device = "mps"
14
+ elif torch.cuda.is_available():
15
+ device = "cuda"
16
+ else:
17
+ device = "cpu"
18
+
19
+ # Initialize the model with specified image size and number of classes
20
+ model = pyramid_trans_expr2(img_size=224, num_classes=7)
21
+
22
+ # Wrap the model with DataParallel for potential multi-GPU usage
23
+ model = torch.nn.DataParallel(model)
24
+
25
+ # Move the model to the chosen device
26
+ model = model.to(device)
27
+ def main():
28
+ fer(model_path=model_path, device=device, model=model)
29
+
30
+ if __name__ == "__main__":
31
+ main()
Output.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import pyttsx3
2
 
3
  engine = pyttsx3.init('espeak')
@@ -5,6 +6,7 @@ voices = engine.getProperty('voices')
5
  print("Available voices:")
6
  for voice in voices:
7
  print(f"- {voice.id}")
8
- engine.setProperty('voice', voices[14].id) # Use the first available voice
9
- engine.say("Hello from Raspberry Pi")
 
10
  engine.runAndWait()
 
1
+
2
  import pyttsx3
3
 
4
  engine = pyttsx3.init('espeak')
 
6
  print("Available voices:")
7
  for voice in voices:
8
  print(f"- {voice.id}")
9
+ engine.setProperty('voice', voices[29].id)
10
+ engine.setProperty('rate', 145) # Use the first available voice
11
+ engine.say("Hi, I AM A DONKEY")
12
  engine.runAndWait()
app.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from FER import detectfaces
2
+ from sever import botFunction
3
+ import socket
4
+ from threading import Timer
5
+ # Settin up connection to the server
6
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
7
+ s.bind(('',5001))
8
+ s.listen(5)
9
+
10
+ # Checking if connection exists then perform stuff
11
+ while True:
12
+ clientsocket, address = s.accept()
13
+ detectfaces()
14
+ botFunction()
15
+ print("Connection Closed")
16
+ break