-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
47 lines (40 loc) · 1.38 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
import base64
import json
from ultralytics import YOLO
import cvzone
import cv2
import math
import requests
import os
from dotenv import load_dotenv
load_dotenv()
count = 0
cap = cv2.VideoCapture(0)
model = YOLO('best2.pt')
classnames = ['fire']
while True:
ret, frame = cap.read()
frame = cv2.resize(frame, (640, 480))
retval, buffer = cv2.imencode('.jpg', frame)
jpg_as_text = base64.b64encode(buffer).decode('utf-8')
result = model(frame, stream=True)
for info in result:
boxes = info.boxes
for box in boxes:
confidence = box.conf[0]
confidence = math.ceil(confidence * 100)
Class = int(box.cls[0])
if confidence > 97:
count += 1
x1, y1, x2, y2 = box.xyxy[0]
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 5)
cvzone.putTextRect(frame, f'{classnames[Class]} {confidence}%', [x1 + 8, y1 + 100],
scale=1.5, thickness=2)
if count == 5:
print('image ready to be sent')
response = requests.post('http://localhost:5000/send-alert', json={'image_data': jpg_as_text})
print('notification sent:', response.json())
count = 0
cv2.imshow('frame', frame)
cv2.waitKey(1)