-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
125 lines (101 loc) · 3.96 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
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
import os
import json
import math
import warnings
import os.path as osp
from tqdm import tqdm
from dotenv import load_dotenv
from uuid import uuid4
from utils.db import DB
from utils.parse import Parse
from utils.s3 import S3Uploader
from utils.extractor import FrameExtractor
load_dotenv()
warnings.filterwarnings("ignore")
class App:
def __init__(self, path) -> None:
self.path = path
self.videos = self.getVideos()
self.videoPoints = []
self.getParsedData()
self.s3 = S3Uploader()
self.db = DB()
self.processing()
def getVideos(self):
videos = []
for root, _, files in os.walk(self.path):
for file in files:
if file.lower().endswith(".mp4"):
videos.append(osp.join(root, file))
return videos
def loadParsedData(self):
with open("parsed.json", "r") as f:
self.videoPoints = json.loads(f.read())
def getParsedData(self):
if osp.exists("parsed.json"):
confirm = input(
"Do you want to use the existing parsed data? (Y/n): "
)
if confirm == "y" or confirm == "":
self.loadParsedData()
return
for video in tqdm(self.videos, desc="Parsing videos", unit="video"):
parse = Parse(video)
# 초당 하나씩 필터링
filteredData = []
currentSecond = -1
for entry in parse.gpxData["points"]:
durationSecond = int(entry["duration"])
if durationSecond > currentSecond:
filteredData.append(entry)
currentSecond = durationSecond
# 좌표, 속도, 영상 정보 등
self.videoPoints.append({parse.gpxData["video"]: filteredData})
self.saveParsedData()
def saveParsedData(self):
with open("parsed.json", "w") as f:
f.write(json.dumps(self.videoPoints, indent=2))
def processing(self):
# length 구하기
length = 0
for videoPoint in self.videoPoints:
for _, points in videoPoint.items():
length += len(points)
with tqdm(total=length, desc="Processing frames", unit="frame") as pbar:
for videoPoint in self.videoPoints:
for videoPath, points in videoPoint.items():
extractor = FrameExtractor(
osp.join(path, *videoPath.split("/")[1:])
)
for point in points:
try:
id = str(uuid4())
frame = extractor.extractFrame(
math.trunc(point["duration"])
- 0.15 # 오류 방지
)
isFrame = frame is not None
# S3에 업로드
key = f"{id}.jpg"
self.s3.uploadFrame(frame, key)
# DB에 저장
self.db.insertData(
{
"id": id,
"lat": point["lat"],
"lng": point["lng"],
"ele": point["ele"],
"time": point["time"],
"duration": point["duration"],
"speed": point["speed"],
"video": videoPath,
"image": isFrame,
}
)
pbar.update(1)
except Exception as e:
print(e)
del extractor
if __name__ == "__main__":
path = osp.join("/", "Volumes", "T7", "road-data")
app = App(path)