-
Notifications
You must be signed in to change notification settings - Fork 8
/
UI.py
174 lines (139 loc) · 5.76 KB
/
UI.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
import streamlit as st
import cv2
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import time
import getpass
import smtplib
import glob
from datetime import datetime
now = datetime.now().time()
class YOLO:
def __init__(self, config, model, labels, size=416, confidence=0.5, threshold=0.3):
self.confidence = confidence
self.threshold = threshold
self.size = size
self.labels = labels
self.net = cv2.dnn.readNetFromDarknet(config, model)
def inference(self, image):
ih, iw = image.shape[:2]
ln = self.net.getLayerNames()
ln = [ln[i[0] - 1] for i in self.net.getUnconnectedOutLayers()]
blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (self.size, self.size), swapRB=True, crop=False)
self.net.setInput(blob)
start = time.time()
layerOutputs = self.net.forward(ln)
end = time.time()
inference_time = end - start
boxes = []
confidences = []
classIDs = []
for output in layerOutputs:
for detection in output:
scores = detection[5:]
classID = np.argmax(scores)
confidence = scores[classID]
if confidence > self.confidence:
box = detection[0:4] * np.array([iw, ih, iw, ih])
(centerX, centerY, width, height) = box.astype("int")
x = int(centerX - (width / 2))
y = int(centerY - (height / 2))
boxes.append([x, y, int(width), int(height)])
confidences.append(float(confidence))
classIDs.append(classID)
idxs = cv2.dnn.NMSBoxes(boxes, confidences, self.confidence, self.threshold)
results = []
if len(idxs) > 0:
for i in idxs.flatten():
x, y = (boxes[i][0], boxes[i][1])
w, h = (boxes[i][2], boxes[i][3])
id = classIDs[i]
confidence = confidences[i]
results.append((id, self.labels[id], confidence, x, y, w, h))
return iw, ih, inference_time, results
class Foo(object):
counter = 0
def __call__(self):
Foo.counter += 1
return(Foo.counter)
def email_alert():
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
username = '[email protected]'
receiver = username
passwd = getpass.getpass()
server.login(username,passwd)
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from datetime import datetime
msg= MIMEMultipart()
msg['from'] = username
msg['to'] = receiver
msg['subject'] = "Images"
text = "Found Fire, have a look at sample images"
msg.attach(MIMEText(text))
F = glob.glob("detected-images/*")
count = 0
for i in F:
with open(i,'rb') as f:
part = MIMEApplication(f.read())
part.add_header('content-Disposition','attachment',filename='{}.jpg'.format(count+1))
msg.attach(part)
server.sendmail(username,receiver,msg.as_string())
def detect_objects(our_image):
st.set_option('deprecation.showPyplotGlobalUse', False)
flag = 0
col1, col2 = st.beta_columns(2)
yolo = YOLO("yolov3/yolov3_custom.cfg", "yolov3/yolov3_custom1_1000.weights", ["Fire"])
st.success("Model Loaded!!")
col1.subheader("Original Image")
st.text("")
plt.figure(figsize = (15,15))
plt.imshow(cv2.cvtColor(our_image, cv2.COLOR_BGR2RGB))
col1.pyplot(use_column_width=True)
width, height, inference_time, results = yolo.inference(our_image)
if(results==[]):
cv2.putText(our_image, 'No Fire', (0, 160), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 255, 255), 2)
st.write("Yolo Model is struggling to find FIRE in this image.")
else:
flag=1
for detection in results:
id, name, confidence, x, y, w, h = detection
color = (0, 255, 255)
cv2.rectangle(our_image, (x, y), (x + w, y + h), color, 2)
cv2.imwrite('detected-images/{}.jpg'.format(now),our_image)
st.text("")
col2.subheader("Object-Detected Image")
st.text("")
plt.figure(figsize = (15,15))
plt.imshow(cv2.cvtColor(our_image, cv2.COLOR_BGR2RGB))
col2.pyplot(use_column_width=True)
if(flag==1):
with st.spinner('Enter Password in terminal to alert Admin...'):
try:
email_alert()
st.success('Alerting Email Sent!')
st.balloons()
except:
st.warning('Something went wrong in emailing.\
Allow Less Secure Apps [here](https://myaccount.google.com/lesssecureapps?pli=1&rapt=AEjHL4NXGrcebt5B8To-QcTqB2d55-ZxnZyOHZfeX3URdTb-0Wy4Xw-aUcEpm0ynhZechyIjhquO7BfA04Yp_WBSNh9fh5kkqQ)')
def object_main():
st.title("Object Detection")
choice = st.radio("", ("Show Demo", "Browse an Image"))
st.write()
if choice == "Browse an Image":
st.set_option('deprecation.showfileUploaderEncoding', False)
image_file = st.file_uploader("Upload Image", type=['jpg','png','jpeg'])
if image_file is not None:
our_image = Image.open(image_file)
## our_image = cv2.imread(image_file)
detect_objects(our_image)
elif choice == "Show Demo":
## our_image = Image.open("images/fire7.jpg")
our_image = cv2.imread("images/fire8.jpg")
detect_objects(our_image)
if __name__ == '__main__':
object_main()