-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
145 lines (113 loc) · 3.59 KB
/
utils.py
File metadata and controls
145 lines (113 loc) · 3.59 KB
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
"""
Utilities Module
Helper functions for the danger zone alert system.
"""
import cv2
from datetime import datetime
class VideoWriter:
"""Handle video file writing"""
def __init__(self, output_path, fps, frame_width, frame_height, codec='mp4v'):
"""
Initialize video writer.
Args:
output_path (str): Path to save the video
fps (float): Frames per second
frame_width (int): Frame width
frame_height (int): Frame height
codec (str): Video codec
"""
self.output_path = output_path
fourcc = cv2.VideoWriter_fourcc(*codec)
self.writer = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
self.frame_count = 0
def write(self, frame):
"""
Write a frame to the video.
Args:
frame (np.ndarray): Video frame
Returns:
bool: True if successful
"""
success = self.writer.write(frame)
if success:
self.frame_count += 1
return success
def release(self):
"""Release the video writer"""
self.writer.release()
def __del__(self):
"""Cleanup on deletion"""
try:
self.release()
except:
pass
class Logger:
"""Simple logging utility"""
def __init__(self, log_file=None):
"""
Initialize logger.
Args:
log_file (str): Path to log file (optional)
"""
self.log_file = log_file
self.logs = []
def log(self, message, level="INFO"):
"""
Log a message.
Args:
message (str): Message to log
level (str): Log level (INFO, WARNING, ERROR, ALERT)
"""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_message = f"[{timestamp}] [{level}] {message}"
print(log_message)
self.logs.append(log_message)
if self.log_file:
with open(self.log_file, 'a') as f:
f.write(log_message + "\n")
def info(self, message):
"""Log info message"""
self.log(message, "INFO")
def warning(self, message):
"""Log warning message"""
self.log(message, "WARNING")
def error(self, message):
"""Log error message"""
self.log(message, "ERROR")
def alert(self, message):
"""Log alert message"""
self.log(message, "ALERT")
def save_to_file(self, filepath):
"""
Save all logs to a file.
Args:
filepath (str): Path to save logs
"""
with open(filepath, 'w') as f:
for log_message in self.logs:
f.write(log_message + "\n")
def get_frame_info(cap):
"""
Get video frame information.
Args:
cap (cv2.VideoCapture): Video capture object
Returns:
dict: Frame information (fps, width, height, total_frames)
"""
return {
'fps': cap.get(cv2.CAP_PROP_FPS),
'width': int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
'height': int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)),
'total_frames': int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
}
def format_time(seconds):
"""
Format seconds to readable time format.
Args:
seconds (float): Seconds
Returns:
str: Formatted time (HH:MM:SS)
"""
minutes, secs = divmod(int(seconds), 60)
hours, minutes = divmod(minutes, 60)
return f"{hours:02d}:{minutes:02d}:{secs:02d}"