-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfire.py
197 lines (172 loc) · 6.14 KB
/
fire.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
import cv2
import numpy as np
import smtplib
import pygame
import threading
import vonage
import requests
from math import sqrt
Fire_Reported = 0
Email_Status = False
Alarm_Status = False
wtsp = False
Telegram = False
sms = False
#---------------------------TELEGRAM----------------------------------#
def telegram():
base_url = "https://api.telegram.org/bot<your_bot_token>/sendMessage?chat_id=<chat_id>&text=Incendie"
requests.get(base_url)
print(base_url)
#------------------------------------------SMS MESSAGE----------------------------------#
def send_sms():
client = vonage.Client(key="********", secret="*******")
sms = vonage.Sms(client)
responseData = sms.send_message(
{
"from": "Vonage APIs",
"to": "212767790246",
"text": "Attention!!! Un feu",
}
)
if responseData["messages"][0]["status"] == "0":
print("Message sent successfully.")
else:
print("Message failed with error:", responseData["messages"][0]["error-text"])
#--------------------ALARM SOUND--------------------------------#
def play_alarm_sound_function():
pygame.mixer.init()
s = pygame.mixer.Sound("C:\Users\Abdel\OneDrive\Bureau\Alter Ego - NTO.wav")
s.play()
#----------------------FIRE DETECTION--------------------------------#
def detect_fire(frame):
global Fire_Reported, Alarm_Status, sms, Telegram
frame = cv2.resize(frame, (960, 540))
blur = cv2.GaussianBlur(frame, (21, 21), 0)
hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
lower = [18, 90, 180]
upper = [35, 255, 255]
lower = np.array(lower, dtype="uint8")
upper = np.array(upper, dtype="uint8")
mask = cv2.inRange(hsv, lower, upper)
output = cv2.bitwise_and(frame, hsv, mask=mask)
no_red = cv2.countNonZero(mask)
if int(no_red) > 15000:
Fire_Reported += 1
cv2.imshow("Detection du feu", output)
if Fire_Reported >= 1:
if not Alarm_Status:
threading.Thread(target=play_alarm_sound_function).start()
Alarm_Status = True
if not sms:
send_sms()
sms = True
if not Telegram:
telegram()
Telegram = True
#--------------------------------FIRE PREVISION MODELISATION ----------------------------#
NON_brulee = 1
brulee = 0
def euclidean_distance(V1, V2):
distance = 0.0
for i in range(len(V1) - 1):
distance += (V1[i] - V2[i]) ** 2
return sqrt(distance)
def localisation_voisins(DATA, test_vecteur, num_neighbors):
distances = []
for DATA_vecteur in DATA:
dist = euclidean_distance(test_vecteur, DATA_vecteur)
distances.append((DATA_vecteur, dist))
distances.sort(key=lambda tup: tup[1])
voisins = []
for i in range(num_neighbors):
voisins.append(distances[i][0])
return voisins
def predict_classification(train, test_row, num_voisin):
voisins = localisation_voisins(train, test_row, num_voisin)
classification = [row[-1] for row in voisins]
prediction = max(set(classification), key=classification.count)
return prediction
# Fonction pour saisir les données du dataset
def input_dataset():
dataset = []
while True:
try:
x = float(input("Entrez la valeur de x (ou un caractère pour quitter) : "))
y = float(input("Entrez la valeur de y : "))
etat = int(input("Entrez l'état (0 pour Non Brûlée, 1 pour Brûlée) : "))
data_point = [x, y, etat]
dataset.append(data_point)
except ValueError:
break
return dataset
# Fonction pour tracer une courbe de points
def plot_points(x_values, y_values, color, marker, label):
plt.scatter(x_values, y_values, color=color, marker=marker, label=label)
# Tracer la courbe avant la prédiction
def plot_before_prediction(dataset):
x_non_brulee = []
y_non_brulee = []
x_brulee = []
y_brulee = []
for data_point in dataset:
x, y, etat = data_point
if etat == 0:
x_non_brulee.append(x)
y_non_brulee.append(y)
else:
x_brulee.append(x)
y_brulee.append(y)
plt.subplot(1, 2, 1)
plot_points(x_non_brulee, y_non_brulee, color='blue', marker='o', label='Non Brûlée')
plot_points(x_brulee, y_brulee, color='red', marker='x', label='Brûlée')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Avant la Prédiction')
plt.legend()
# Tracer la courbe après la prédiction
def plot_after_prediction(dataset, num_neighbors):
x_non_brulee_pred = []
y_non_brulee_pred = []
x_brulee_pred = []
y_brulee_pred = []
for data_point in dataset:
x, y, etat = data_point
prediction = predict_classification(dataset, data_point, num_neighbors)
if prediction == 0:
x_non_brulee_pred.append(x)
y_non_brulee_pred.append(y)
else:
x_brulee_pred.append(x)
y_brulee_pred.append(y)
plt.subplot(1, 2, 2)
plot_points(x_non_brulee_pred, y_non_brulee_pred, color='blue', marker='o', label='Non Brûlée (Prédiction)')
plot_points(x_brulee_pred, y_brulee_pred, color='red', marker='x', label='Brûlée (Prédiction)')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Après la Prédiction')
plt.legend()
def main():
CHOICE==(who are you?firfighter or No)
if (choice=="no")
video = cv2.VideoCapture('http://192.168.***:8080/video')
while True:
grabbed, frame = video.read()
if not grabbed:
break
detect_fire(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
pygame.quit()
break
cv2.destroyAllWindows()
video.release()
else:
print("ENTER DATASET OF YOUR LOCALISATION:")
dataset = input_dataset()
num_neighbors = int(input("Enter K: "))
plt.figure(figsize=(12, 6))
plot_before_prediction(dataset)
plot_after_prediction(dataset, num_neighbors)
plt.tight_layout()
plt.show()
if __name__ == "__main__":
main()