-
Notifications
You must be signed in to change notification settings - Fork 1
/
multithreading.py
150 lines (126 loc) · 3.97 KB
/
multithreading.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
#!/usr/bin/python
from _common import get_api
import time
import tweetpony
import twitter
from chatterbotapi import ChatterBotFactory, ChatterBotType
import cv2
from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth
import thread
import time
import os
import signal
import sys
import time
from espeak import espeak
import record_audio
os.chdir('/home/nayan/Downloads/Desktop')
import serial
from time import sleep
import sys
number = 0;
factory = ChatterBotFactory()
bot2 = factory.create(ChatterBotType.PANDORABOTS, 'b0dafd24ee35a477')
bot2session = bot2.create_session()
#connect to arduino by trying the following list of serial ports, mac need to be added
locations=['/dev/ttyACM0','/dev/ttyACM1','/dev/ttyACM2','/dev/ttyACM3','/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3',
'/dev/ttyS0','/dev/ttyS1','/dev/ttyS2','/dev/ttyS3','COM6','COM4','COM3']
connected =False #set connected to be false
#try each serial port, exit loop if one connects and set connected to True, else desplay failed to connect to any arduino
for device in locations:
#try to connect to current device
try:
#print ("Trying..."+device)
arduino = serial.Serial(device, 115200)
connected=True
break
except:
print ("Failed to connect on "+device)
if connected ==False:
print ("\nFailed to connect on to any arduino")
else:
print ("Successfully connected to arduino\n")
# Define a function for the thread
def face_tracking():
counter=0
num=0
cap = cv2.VideoCapture(1)
while (cap.isOpened()):
ret,image = cap.read()
# Create the haar cascade
faceCascade = cv2.CascadeClassifier('defaul.xml')
time.sleep(0.005)
# Read the image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
if(h>=80 and w>=80):
if(x>=200 and x<=280):
if(not (counter==1)):
arduino.write('s')
print 'center'
counter=1
elif(x>300):
if(not (counter==2)):
arduino.write('l')
#sleep(.05)
print 'left'
counter=2
else:
if(not (counter==3)):
arduino.write('r')
#sleep(.05)
print 'right'
counter=3
cv2.imshow("Faces found", image)
k=cv2.waitKey(10)
if k==27:
break
cap.release()
cv2.destroyAllWindows
def signal_handler(signal, frame):
global number
g = record_audio.main(number);
if 'normal' in g:
arduino.write('a')
elif 'unhappy' in g:
arduino.write('b')
elif 'angry' in g:
arduino.write('c')
elif 'funny' in g:
arduino.write('f')
else:
g = bot2session.think(g);
print g
i=0
spaces=0
while(i<len(g)):
if g[i]==' ':
spaces=spaces+1
i=i+1
arduino.write('p')
espeak.synth(g)
sleep((spaces+1)/3)
arduino.write('q')
# Create two threads as follows
try:
thread.start_new_thread( face_tracking,() )
thread.start_new_thread( twitter.twitter,() )
print 'hi'
except:
print "Error: unable to start thread"
signal.signal(signal.SIGINT, signal_handler)
print 'Press Ctrl+C'
while True:
time.sleep(1)
while 1:
pass