-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
246 lines (179 loc) · 7.33 KB
/
Copy pathdb.py
File metadata and controls
246 lines (179 loc) · 7.33 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
from bs4 import BeautifulSoup
from flask import jsonify
from psycopg2.extras import DictCursor
import os
import psycopg2
import re
BASE_URL = "https://videos.breathecam.org/labeling"
BASE_DIR = "/workspace/projects/videos.breathecam.org/www/vids/labeling"
CLASSIFICATION_OPTIONS = [
"smoke",
"steam",
"shadow",
"other_motion",
"none"
]
def _parse_metadata(metadata):
video_num = int(re.search(r'Video (\d+)', metadata).group(1))
frames_info = re.search(r'(\d+) frames starting at (\d+)', metadata)
size_info = re.search(r'Size: (\d+)x(\d+)', metadata)
if frames_info:
start_frame = int(frames_info.group(2))
nframes = int(frames_info.group(1))
else:
frames_info = re.search(r'Start Frame: (\d+)', metadata)
start_frame = frames_info.group(1)
size_info = re.search(r'Dimensions: (\d+)[x ](\d+)x(\d+)', metadata)
nframes = int(size_info.group(1))
view = re.search(r'View: \((\d+, \d+, \d+, \d+)\)', metadata)
bbox = {}
if view:
l, t, r, b = list(map(int, view.group(1).split(", ")))
bbox = {
"left": l,
"top": t,
"right": r,
"bottom": b,
"top_left": [t, l],
"bottom_right": [b, r],
"height": b - t,
"width": r - l,
}
return {
"index": video_num,
"start_frame": start_frame,
"nframes": nframes,
"bounding_box": bbox
}
class AutoFEDDatabase:
def __init__(self):
self.conn = psycopg2.connect(dbname="smoke_detect")
self.conn.autocommit = True
def all_videos_for_run(self, table: str, run: str):
"""Get all videos for a specific run from the database"""
videos = []
classifications = {}
cursor = self.conn.cursor(cursor_factory=DictCursor)
try:
cursor.execute(f"SELECT * FROM {table} WHERE run_name = %s ORDER BY id", (run,))
for row in cursor.fetchall():
classifications = row['classifications']
metadata = row['metadata']
video_data = _parse_metadata(metadata)
video_data["src"] = row["video_url"]
video_data["record_id"] = row["id"]
for opt in CLASSIFICATION_OPTIONS:
if opt not in classifications:
classifications[opt] = False
video_data["classifications"] = classifications
videos.append(video_data)
except Exception as e:
print(f"Database error: {e}")
finally:
cursor.close()
videos.sort(key=lambda v: v["index"])
return videos
def available_runs(self, table: str):
runs = []
cursor = self.conn.cursor()
try:
cursor.execute(f"SELECT DISTINCT run_name FROM {table} ORDER BY run_name")
runs = [row[0] for row in cursor.fetchall()]
except Exception as e:
print(f"Database error: {e}")
finally:
cursor.close()
return runs
def close(self):
self.conn.close()
def create_table(self, table: str):
cursor = self.conn.cursor()
cursor.execute(f"""
CREATE TABLE IF NOT EXISTS {table} (
id SERIAL PRIMARY KEY,
run_name TEXT NOT NULL,
video_url TEXT NOT NULL,
metadata TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
""")
self.conn.commit()
cursor.close()
def delete_run(self, table: str, run: str):
cursor = self.conn.cursor()
cursor.execute("DELETE FROM video_labels WHERE run_name = %s", (run,))
self.conn.commit()
cursor.close()
print(f"Deleted existing records for run_name: {run}")
def drop_table(self, table: str):
cursor = self.conn.cursor()
cursor.execute(f"DROP TABLE IF EXISTS {table}")
self.conn.commit()
cursor.close()
def insert(self, table: str, results):
cursor = self.conn.cursor()
for item in results:
cursor.execute(
f"INSERT INTO {table} (run_name, video_url, metadata) VALUES (%s, %s, %s)",
(item['run_name'], item['video_src'], item['metadata'])
)
self.conn.commit()
cursor.close()
print(f"Successfully inserted {len(results)} video records into the database.")
def insert_from_html(self, table: str, run: str, html_file: str):
soup = BeautifulSoup(open(html_file, "r").read())
video_containers = soup.find_all("div", class_="video-container")
results = []
for container in video_containers: # Process all videos
video_src = os.path.join(self.base_directory, container.find('video')['src'])
video_src = os.path.realpath(video_src)
assert video_src.startswith(self.base_directory)
video_src = f"{BASE_URL}/{video_src[len(self.base_directory):]}"
label = container.find('div', class_='label')
for br in label.find_all('br'):
br.replace_with(' ')
metadata = label.text.strip()
results.append({
'run_name': os.path.splitext(os.path.basename(html_file))[0],
'video_src': video_src,
'metadata': metadata
})
self.insert(table, run, results)
def labeled_videos_for_run(self, run_name, classification_filter=None):
"""Get all videos for a specific run from the database"""
videos = []
classifications = {}
cur = self.conn.cursor(cursor_factory=DictCursor)
classification_filter = classification_filter or (lambda c: len(c) > 0)
try:
cur.execute("SELECT * FROM video_labels WHERE run_name = %s ORDER BY id", (run_name,))
for row in cur.fetchall():
classifications = row['classifications']
if not classification_filter(classifications):
continue
metadata = row['metadata']
video_data = _parse_metadata(metadata)
video_data["src"] = row["video_url"]
video_data["record_id"] = row["id"]
video_data["classifications"] = [k for k in classifications.keys() if classifications.get(k, False)]
videos.append(video_data)
except Exception as e:
print(f"Database error: {e}")
finally:
cur.close()
videos.sort(key=lambda v: v["index"])
return videos
def set_classifications(self, video_id, classifications):
try:
cursor = self.conn.cursor(cursor_factory=DictCursor)
cursor.execute( "UPDATE video_labels SET classifications = %s WHERE id = %s", (classifications, video_id))
self.conn.commit()
except Exception as e:
print(f"Error: {e}")
return jsonify({'message': f'Error: {str(e)}'}), 500
finally:
cursor.close()
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.conn.close()