-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetection.py
179 lines (129 loc) · 4.37 KB
/
detection.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
import pyrebase
import time
from datetime import datetime
import json
import os
import pickle
import os.path
import requests
import json
from geopy.geocoders import Nominatim
import math
from gtts import gTTS
from dotenv import load_dotenv
from playsound import playsound
import sys
aed_location = "seoul "
address = "서울 중랑구 "
detect = sys.argv[1]
# 주소를 위도, 경도로 반환하는 함수
def geocoding(address):
try:
geo_local = Nominatim(user_agent='South Korea') # 지역설정
location = geo_local.geocode(address)
geo = [location.latitude, location.longitude]
return geo
except:
return [0, 0]
# 두 지점 사이의 거리 계산 (Haversine 공식 사용)
def calculate_distance(lat1, lon1, lat2, lon2):
# 지구의 반경 (단위: km)
radius = 6371.0
# 라디안 변환
lat1_rad = math.radians(lat1)
lon1_rad = math.radians(lon1)
lat2_rad = math.radians(lat2)
lon2_rad = math.radians(lon2)
# 좌표 간의 차이 계산
dlon = lon2_rad - lon1_rad
dlat = lat2_rad - lat1_rad
# Haversine 공식 적용
a = math.sin(dlat / 2)**2 + math.cos(lat1_rad) * \
math.cos(lat2_rad) * math.sin(dlon / 2)**2
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
# 거리 계산
distance = radius * c
return distance
# 주어진 위도, 경도에서 가장 가까운 장소 찾기
def find_nearest_place(latitude, longitude, places):
min_distance = float('inf')
nearest_place = None
for place in places:
place_latitude = float(place['WGS84LON'])
place_longitude = float(place['WGS84LAT'])
distance = calculate_distance(
latitude, longitude, place_latitude, place_longitude)
if distance < min_distance:
min_distance = distance
nearest_place = place
return nearest_place
# AED API 호출을 위한 기본 정보
var = "67565a74456b69363936614c56504f"
base_url = f"http://openapi.seoul.go.kr:8088/{var}/json/tbEmgcAedInfo"
start_index = 1
end_index = 1000
total_data = []
# AED API를 반복 호출하여 데이터 받아오기
while True:
# URL 설정
url = f"{base_url}/{start_index}/{end_index}/"
# API 호출
response = requests.get(url)
data = response.json()
# 받아온 데이터 처리
aed_list = data['tbEmgcAedInfo']['row']
total_data.extend(aed_list)
# 다음 페이지가 있는지 확인
if len(aed_list) < end_index - start_index + 1:
break
# 다음 페이지로 이동
start_index += 1000
end_index += 1000
# 전체 데이터 출력
keys = ["BUILDADDRESS", "BUILDPLACE", "WGS84LON", "WGS84LAT"]
extracted_data = [{key: item[key] for key in keys} for item in total_data]
config={
"apiKey": "AIzaSyAX_Bgfl03nCEfCNOh1uiuxphveVAzNMxU", #webkey
"authDomain": "alpha-92011.firebaseapp.com",
"databaseURL": "https://alpha-92011-default-rtdb.firebaseio.com/", #database url
"storageBucket": "alpha-92011.appspot.com" #storage
}
firebase = pyrebase.initialize_app(config)
uploadfile = sys.argv[3]
s = os.path.splitext(uploadfile)[1]
now = sys.argv[2]
filename = now + s
#Upload files to Firebase
storage = firebase.storage()
storage.child(filename).put(uploadfile)
fileUrl = storage.child(filename).get_url(1)
#save files info in database
db = firebase.database()
address = db.child("cam1_address").get().val()
print(address)
if (detect =="2" or detect ==2) :
geo = geocoding(address)
my_longitude = geo[0]
my_latitude = geo[1]
# 가장 가까운 장소 찾기
nearest_place = find_nearest_place(my_latitude, my_longitude, extracted_data)
aed_location = nearest_place['BUILDADDRESS'] + nearest_place['BUILDPLACE']
data= {"AED" : aed_location ,
"address" : address ,
"detect " : detect ,
"time" : filename[:-4] ,
"fileUrl" : fileUrl }
db.child(str(filename).replace('.mp4', '')).set(data)
text = f"심정지 환자가 발생했습니다. AED의 위치는 {aed_location}입니다 빠르게 이동하세요 "
text_to_voice = gTTS(text = text , lang="ko")
text_to_voice.save("text.mp3")
playsound("siren.mp3")
for i in range(0,5):
playsound("text.mp3")
else :
data= {
"address" : address ,
"detect " : detect ,
"time" : filename[:-4] ,
"fileUrl" : fileUrl }
db.child(str(filename).replace('.mp4', '')).set(data)