Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,55 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from ultralytics import YOLO
|
3 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
def check_acc(box):
|
6 |
res_index_list = box.cls.tolist()
|
@@ -35,11 +84,13 @@ def image_predict(image):
|
|
35 |
res = ""
|
36 |
model_path = "best.pt"
|
37 |
model = YOLO(model_path)
|
38 |
-
results = model.predict(image,conf = 0.
|
39 |
box = results[0].boxes
|
40 |
res = check_acc(box)
|
41 |
annotated_frame = results[0].plot()
|
42 |
if len(res) >0:
|
|
|
|
|
43 |
return (res, annotated_frame)
|
44 |
|
45 |
return ("No Class Detected", None)
|
@@ -62,12 +113,14 @@ def extract_frames(video):
|
|
62 |
if (frame_no % (int(fps / nof))) == 0:
|
63 |
model_path = "best.pt"
|
64 |
model = YOLO(model_path)
|
65 |
-
results = model.predict(image,conf = 0.
|
66 |
box = results[0].boxes
|
67 |
res = check_acc(box)
|
68 |
annotated_frame = results[0].plot()
|
69 |
|
70 |
if len(res) >0:
|
|
|
|
|
71 |
return (res, annotated_frame)
|
72 |
|
73 |
frame_no += 1 # Increment frame number
|
|
|
1 |
+
import os
|
2 |
+
import cv2
|
3 |
+
import smtplib
|
4 |
import gradio as gr
|
5 |
+
from email import encoders
|
6 |
from ultralytics import YOLO
|
7 |
+
from email.mime.text import MIMEText
|
8 |
+
from email.mime.base import MIMEBase
|
9 |
+
from email.mime.multipart import MIMEMultipart
|
10 |
+
|
11 |
+
sender_email = os.environ.get("sender_email")
|
12 |
+
receiver_email = os.environ.get("receiver_email")
|
13 |
+
sender_password = os.environ.get("sender_password")
|
14 |
+
smtp_port = 8080
|
15 |
+
smtp_server = "smtp.gmail.com"
|
16 |
+
subject = "Accident detected"
|
17 |
+
|
18 |
+
|
19 |
+
def send_email(accident_type):
|
20 |
+
body = accident_type
|
21 |
+
|
22 |
+
msg = MIMEMultipart()
|
23 |
+
msg['From'] = sender_email
|
24 |
+
msg['To'] = receiver_email
|
25 |
+
msg['Subject'] = subject
|
26 |
+
|
27 |
+
msg.attach(MIMEText(body, 'plain'))
|
28 |
+
|
29 |
+
filename = "res.png"
|
30 |
+
folder = "./result/"
|
31 |
+
fullFileName = folder + filename
|
32 |
+
attachment = open(fullFileName, 'rb')
|
33 |
+
|
34 |
+
attachment_package = MIMEBase('application', 'octet-stream')
|
35 |
+
attachment_package.set_payload((attachment).read())
|
36 |
+
encoders.encode_base64(attachment_package)
|
37 |
+
attachment_package.add_header('Content-Disposition', "attachment; filename= " + filename)
|
38 |
+
msg.attach(attachment_package)
|
39 |
+
|
40 |
+
text = msg.as_string()
|
41 |
+
|
42 |
+
print("Connecting to server")
|
43 |
+
gmail_server = smtplib.SMTP(smtp_server, smtp_port)
|
44 |
+
gmail_server.starttls()
|
45 |
+
gmail_server.login(sender_email, sender_password)
|
46 |
+
print("Successfully Connected to Server")
|
47 |
+
|
48 |
+
print("Sending email to ", receiver_email)
|
49 |
+
gmail_server.sendmail(sender_email, receiver_email, text)
|
50 |
+
print("Email sent to ", receiver_email)
|
51 |
+
|
52 |
+
gmail_server.quit()
|
53 |
|
54 |
def check_acc(box):
|
55 |
res_index_list = box.cls.tolist()
|
|
|
84 |
res = ""
|
85 |
model_path = "best.pt"
|
86 |
model = YOLO(model_path)
|
87 |
+
results = model.predict(image,conf = 0.6,iou = 0.3,imgsz = 512)
|
88 |
box = results[0].boxes
|
89 |
res = check_acc(box)
|
90 |
annotated_frame = results[0].plot()
|
91 |
if len(res) >0:
|
92 |
+
annotated_frame.save("./result/res.png")
|
93 |
+
send_email(res)
|
94 |
return (res, annotated_frame)
|
95 |
|
96 |
return ("No Class Detected", None)
|
|
|
113 |
if (frame_no % (int(fps / nof))) == 0:
|
114 |
model_path = "best.pt"
|
115 |
model = YOLO(model_path)
|
116 |
+
results = model.predict(image,conf = 0.6,iou = 0.3,imgsz = 512)
|
117 |
box = results[0].boxes
|
118 |
res = check_acc(box)
|
119 |
annotated_frame = results[0].plot()
|
120 |
|
121 |
if len(res) >0:
|
122 |
+
annotated_frame.save("./result/res.png")
|
123 |
+
send_email(res)
|
124 |
return (res, annotated_frame)
|
125 |
|
126 |
frame_no += 1 # Increment frame number
|