-
Notifications
You must be signed in to change notification settings - Fork 0
/
all_in_one.py
287 lines (276 loc) · 10.7 KB
/
all_in_one.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import cv2
import time
import numpy as np
import time
import smtplib
import os
import imghdr
import ssl
import sys
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import pygame
from pygame import mixer
# import RPi.GPIO as GPIO
def load_photo():
mixer.init()
cv2.namedWindow('QR_Code', cv2.WINDOW_NORMAL)
cv2.setWindowProperty('QR_Code', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
path = ("/home/kai/Thermal_imaging/welcome.png")
qr_code = cv2.imread(path)
cv2.imshow("QR_Code", qr_code)
def ultrasonic():
while 1:
GPIO.setmode(GPIO.BCM)
TRIG = 23
ECHO = 24
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO)==0:
pulse_start = time.time()
while GPIO.input(ECHO)==1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration*17150
distance = round(distance, 2)
print ("Distance:",distance,"cm")
GPIO.cleanup()
def video():
while 1:
# GPIO.setmode(GPIO.BCM)
# TRIG = 23
# ECHO = 24
# GPIO.setup(TRIG,GPIO.OUT)
# GPIO.setup(ECHO,GPIO.IN)
# GPIO.output(TRIG, True)
# time.sleep(0.00001)
# GPIO.output(TRIG, False)
# while GPIO.input(ECHO)==0:
# pulse_start = time.time()
# while GPIO.input(ECHO)==1:
# pulse_end = time.time()
# pulse_duration = pulse_end - pulse_start
# distance = pulse_duration*17150
# distance = round(distance, 2)
# print ("Distance:",distance,"cm")
cv2.namedWindow('Capturing_Feedback', cv2.WINDOW_NORMAL)
cv2.setWindowProperty('Capturing_Feedback', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
mixer.init()
def change_res(cap, width, height):
cap.set(3, width)
cap.set(4, height)
STD_DIMENSIONS = {
"480p": (640, 480),
"720p": (1280, 720),
"1080p": (1920, 1080),
"4k": (3840, 2160),
}
def get_dims(cap, res='1080p'):
width, height = STD_DIMENSIONS["480p"]
if res in STD_DIMENSIONS:
width,height = STD_DIMENSIONS[res]
change_res(cap, width, height)
return width, height
VIDEO_TYPE = {
'avi': cv2.VideoWriter_fourcc(*'XVID'),
'mp4': cv2.VideoWriter_fourcc(*'XVID'),
}
def get_video_type(filename):
filename, ext = os.path.splitext(filename)
if ext in VIDEO_TYPE:
return VIDEO_TYPE[ext]
return VIDEO_TYPE['avi']
TIMER = int(10)
i=0
date_string = time.strftime("%Y-%m-%d-%H:%M")
frames_per_second = 29.0
res = '720p'
filename = "/home/kai/Thermal_imaging/video.avi"
capture = cv2.VideoCapture(0)
out = cv2.VideoWriter(filename, get_video_type(filename), 25, get_dims(capture, res))
result = True
while (result):
ret, frame = capture.read()
out.write(frame)
cv2.imshow('Capturing_Feedback',frame)
cv2.waitKey(100)
# GPIO.setmode(GPIO.BCM)
# TRIG = 23
# ECHO = 24
# GPIO.setup(TRIG,GPIO.OUT)
# GPIO.setup(ECHO,GPIO.IN)
# GPIO.output(TRIG, True)
# time.sleep(0.00001)
# GPIO.output(TRIG, False)
# while GPIO.input(ECHO)==0:
# pulse_start = time.time()
# while GPIO.input(ECHO)==1:
# pulse_end = time.time()
# pulse_duration = pulse_end - pulse_start
# distance = pulse_duration*17150
# distance = round(distance, 2)
#print ("Distance:",distance,"cm")
prev = time.time()
distance = 14
if distance < 50:
time.sleep(4)
if distance < 15:
while TIMER >= 0:
ret, frame = capture.read()
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, str(TIMER),
(200, 250), font,
7, (0, 255, 255),
4, cv2.LINE_AA)
out.write(frame)
cv2.imshow('Capturing_Feedback',frame)
cv2.waitKey(125)
cur = time.time()
if cur-prev >= 1:
prev = cur
TIMER = TIMER-1
else:
msg = MIMEMultipart()
msg['From'] = "[email protected]"
msg['To'] = "[email protected]"
msg['Subject'] = 'Python Mail'
body = 'Automated mail, image captured from the webcam'
msg.attach(MIMEText(body, 'plain'))
filename = "/home/kai/Thermal_imaging/video.avi"
attachment = open(filename, 'rb')
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename=" +filename)
msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("[email protected]", "Stumbleupon01")
server.sendmail("[email protected]", "[email protected]", text)
server.quit()
capture.release()
out.release()
cv2.destroyWindow('Capturing_Feedback')
break
capture.release()
out.release()
cv2.destroyWindow('Capturing_Feedback')
os.remove("/home/kai/Thermal_imaging/video.avi")
def image():
while 1:
# GPIO.setmode(GPIO.BCM)
# TRIG = 23
# ECHO = 24
# GPIO.setup(TRIG,GPIO.OUT)
# GPIO.setup(ECHO,GPIO.IN)
# GPIO.output(TRIG, True)
# time.sleep(0.00001)
# GPIO.output(TRIG, False)
# while GPIO.input(ECHO)==0:
# pulse_start = time.time()
# while GPIO.input(ECHO)==1:
# pulse_end = time.time()
# pulse_duration = pulse_end - pulse_start
# distance = pulse_duration*17150
# distance = round(distance, 2)
# print ("Distance:",distance,"cm")
distance = 14
if distance < 15:
mixer.music.load("/home/kai/Thermal_imaging/photo.mpeg")
mixer.music.set_volume(0.7)
mixer.music.play()
cv2.namedWindow('Capturing Photo', cv2.WINDOW_NORMAL)
cv2.setWindowProperty('Capturing Photo', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
TIMER = int(3)
date_string = time.strftime("%Y-%m-%d-%H:%M")
mixer.init()
i = 0
capture = cv2.VideoCapture(0)
ret, frame = capture.read()
cv2.imshow('Capturing Photo', frame)
cv2.waitKey(10)
mixer.music.load("/home/kai/Thermal_imaging/cheese.mp3")
mixer.music.set_volume(0.7)
prev = time.time()
while TIMER >= 0:
ret, frame = capture.read()
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, str(TIMER),
(200, 250), font,
7, (0, 255, 255),
4, cv2.LINE_AA)
cv2.imshow('Capturing Photo', frame)
cv2.waitKey(125)
cur = time.time()
if cur-prev >= 1:
prev = cur
TIMER = TIMER-1
else:
ret, frame = capture.read()
cv2.imshow('Capturing Photo', frame)
cv2.waitKey(2000)
image_cap = "/home/kai/Thermal_imaging/Robot_image" + date_string + ".jpg".format(i)
cv2.imwrite(image_cap, frame)
i+=1
msg = MIMEMultipart()
msg['From'] = "[email protected]"
msg['To'] = "[email protected]"
msg['Subject'] = 'Python Mail'
body = 'Automated mail, image captured from the webcam'
msg.attach(MIMEText(body, 'plain'))
filename = "/home/kai/Thermal_imaging/Robot_image" + date_string + ".jpg".format(i)
attachment = open(filename, 'rb')
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename=" +filename)
msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("[email protected]", "Stumbleupon01")
server.sendmail("[email protected]", "[email protected]", text)
server.quit()
# capture.release()
# cv2.destroyWindow('Capturing Photo')
capture.release()
cv2.destroyWindow('Capturing Photo')
os.remove("/home/kai/Thermal_imaging/Robot_image" + date_string + ".jpg".format(i))
break
if __name__ == '__main__':
i = 0
while i <= 10:
load_photo()
time.sleep(.1)
# GPIO.setmode(GPIO.BCM)
# TRIG = 23
# ECHO = 24
# GPIO.setup(TRIG,GPIO.OUT)
# GPIO.setup(ECHO,GPIO.IN)
# GPIO.output(TRIG, True)
# time.sleep(0.00001)
# GPIO.output(TRIG, False)
# while GPIO.input(ECHO)==0:
# pulse_start = time.time()
# while GPIO.input(ECHO)==1:
# pulse_end = time.time()
# pulse_duration = pulse_end - pulse_start
# distance = pulse_duration*17150
# distance = round(distance, 2)
# print ("Distance:",distance,"cm")
distance = 49
if distance<50:
mixer.music.load("/home/kai/Thermal_imaging/welcome.mp3")
mixer.music.set_volume(0.7)
mixer.music.play()
cv2.waitKey(300)
cv2.destroyWindow("QR_Code")
image()
time.sleep(.1)
import video