-
Notifications
You must be signed in to change notification settings - Fork 0
/
tornado_detection.py
37 lines (29 loc) · 1.22 KB
/
tornado_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
import cv2
from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator, colors
model = YOLO("best.pt") # segmentation model
# model = YOLO("yolov8n-seg.pt") # segmentation model
names = model.model.names
cap = cv2.VideoCapture("rtsp://localhost:8554/tornadoes")
#cap = cv2.VideoCapture("tornadoes.mp4")
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
out = cv2.VideoWriter('tornado-detection.avi', cv2.VideoWriter_fourcc(*'MJPG'), fps, (w, h))
while True:
ret, im0 = cap.read()
if not ret:
print("Video frame is empty or video processing has been successfully completed.")
break
results = model.predict(im0)
results = model(im0)
annotator = Annotator(im0, line_width=2)
if results[0].masks is not None and results[0].boxes is not None:
clss = results[0].boxes.cls.cpu().tolist()
masks = results[0].masks.xy
for mask, cls in zip(masks, clss):
if mask.size != 0:
annotator.seg_bbox(mask=mask,
mask_color=colors(int(cls), True),
det_label=names[int(cls)])
out.write(im0)
out.release()
cap.release()