-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
83 lines (65 loc) · 2.35 KB
/
main.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
import cv2
import shutil
import os
import time
import keyboard
import tomtom
import watson_api
import location
import whatsapp
count = 0
run = True
pathOut='C:/Users/rrohi/OneDrive/Documents/Frames'
urlMain = 'https://www.google.com/maps/dir/?api=1&destination='
myCoords = (12.892122, 77.565379)
#Gets coordinates of closest hospital, using the tomtom api, which
#is used to find ETA between any two coordinates, and location which stores a dictionary of all hospitals
def getLocation():
global myCoords
coords = ()
eta_min = 100000000000
for place in location.d.keys():
eta = tomtom.get_ETA(myCoords, location.d[place])
if eta <= eta_min:
eta_min = eta
coords = location.d[place][0], location.d[place][1]
return coords
#Starts webcam
cap = cv2.VideoCapture(0)
if not cap.isOpened():
raise IOError("Cannot open webcam")
#Creates a new folder. If the folder already exists it deletes it and creates a new one in its place
try:
os.mkdir(pathOut)
except:
shutil.rmtree(pathOut)
os.mkdir(pathOut)
# Main loop
while run:
# A frame from the webcam video is saved in the folder created
ret, frame = cap.read()
frame = cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
cv2.imshow('Input', frame)
if ret == True:
print('Read %d frame: ' % count, ret)
cv2.imwrite(os.path.join(pathOut, "frame{:d}.jpg".format(count)), frame)
# The newly created is sent to the watson api to get a result
# if it is an accident, a google maps link to the accident location is generated and sent to
# the phone number of the hospital
if watson_api.watson(pathOut+'/frame{:d}.jpg'.format(count)):
destCoords = getLocation()
string_coords = str(destCoords[0]) + ',' + str(destCoords[1])
string_coords = urlMain + string_coords
print(string_coords)
whatsapp.send_message(string_coords)
count += 1
# there is a time gap of 5 seconds between every frame that is saved
# during these 5 seconds we can pressed 'q' to quit the program
t_end = time.time()+5
while time.time() < t_end:
if keyboard.is_pressed('q'):
print('Quitting...')
run = False
cap.release()
cv2.destroyAllWindows()
break