Mavic Mini SRT support, cleanup, fixes

pull/1567/head
Piero Toffanin 2023-01-18 15:48:29 -05:00
rodzic 1106635d09
commit 0da6067796
2 zmienionych plików z 60 dodań i 94 usunięć

Wyświetl plik

@ -1,5 +1,26 @@
from datetime import datetime
import re
def match_single(regexes, line, dtype=int):
if isinstance(regexes, str):
regexes = [(regexes, dtype)]
for i in range(len(regexes)):
if isinstance(regexes[i], str):
regexes[i] = (regexes[i], dtype)
try:
for r, transform in regexes:
match = re.search(r, line)
if match:
res = match.group(1)
return transform(res)
except Exception as e:
log.ODM_WARNING("Cannot parse SRT line \"%s\": %s", (line, str(e)))
return None
class SrtFileParser:
def __init__(self, filename):
self.filename = filename
@ -37,15 +58,9 @@ class SrtFileParser:
with open(self.filename, 'r') as f:
srtcnt = None
difftime = None
timestamp = None
iso = None
shutter = None
fnum = None
ev = None
ct = None
color_md = None
focal_len = None
latitude = None
longitude = None
@ -57,34 +72,23 @@ class SrtFileParser:
# Check if line is empty
if not line.strip():
if srtcnt is not None:
if start is not None:
self.data.append({
"start": start,
"end": end,
"srtcnt": srtcnt,
"difftime": difftime,
"timestamp": timestamp,
"iso": iso,
"shutter": shutter,
"fnum": fnum,
"ev": ev,
"ct": ct,
"color_md": color_md,
"focal_len": focal_len,
"latitude": latitude,
"longitude": longitude,
"altitude": altitude
})
srtcnt = None
difftime = None
timestamp = None
iso = None
shutter = None
fnum = None
ev = None
ct = None
color_md = None
focal_len = None
latitude = None
longitude = None
@ -94,7 +98,7 @@ class SrtFileParser:
continue
# Remove the html font tag
# Remove html tags
line = re.sub('<[^<]+?>', '', line)
# Search this "00:00:00,000 --> 00:00:00,016"
@ -103,59 +107,34 @@ class SrtFileParser:
start = datetime.strptime(match.group(1), "%H:%M:%S,%f")
end = datetime.strptime(match.group(2), "%H:%M:%S,%f")
iso = match_single([
"iso : (\d+)",
"ISO (\d+)"
], line)
match = re.search("SrtCnt : (\d+)", line)
if match:
srtcnt = int(match.group(1))
shutter = match_single([
"shutter : \d+/(\d+\.?\d*)"
"SS (\d+\.?\d*)"
], line)
match = re.search("DiffTime : (\d+)ms", line)
if match:
difftime = int(match.group(1))
fnum = match_single([
("fnum : (\d+)", lambda v: float(v)/100.0),
("F/([\d\.]+)", float),
], line)
match = re.search("(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3})", line)
if match:
timestamp = match.group(1)
timestamp = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S,%f")
focal_len = match_single("focal_len : (\d+)", line)
match = re.search("iso : (\d+)", line)
if match:
iso = int(match.group(1))
latitude = match_single([
("latitude: ([\d\.\-]+)", lambda v: float(v) if v != 0 else None),
("GPS \(([\d\.\-]+),? [\d\.\-]+,? [\d\.\-]+\)", lambda v: float(v) if v != 0 else None),
], line)
match = re.search("shutter : (\d+/\d+.\d+)", line)
if match:
shutter = match.group(1)
longitude = match_single([
("longitude: ([\d\.\-]+)", lambda v: float(v) if v != 0 else None),
("GPS \(([\d\.\-]+),? [\d\.\-]+,? [\d\.\-]+\)", lambda v: float(v) if v != 0 else None),
], line)
match = re.search("fnum : (\d+)", line)
if match:
fnum = int(match.group(1))
match = re.search("ev : (\d+)", line)
if match:
ev = int(match.group(1))
match = re.search("ct : (\d+)", line)
if match:
ct = int(match.group(1))
match = re.search("color_md : (\w+)", line)
if match:
color_md = match.group(1)
match = re.search("focal_len : (\d+)", line)
if match:
focal_len = int(match.group(1))
match = re.search("latitude: (\d+.\d+)", line)
if match:
latitude = float(match.group(1))
latitude = latitude if latitude != 0 else None
match = re.search("longitude: (\d+.\d+)", line)
if match:
longitude = float(match.group(1))
longitude = longitude if longitude != 0 else None
match = re.search("altitude: (\d+.\d+)", line)
if match:
altitude = float(match.group(1))
altitude = altitude if altitude != 0 else None
altitude = match_single([
("altitude: ([\d\.\-]+)", lambda v: float(v) if v != 0 else None),
("GPS \([\d\.\-]+,? [\d\.\-]+,? ([\d\.\-]+)\)", lambda v: float(v) if v != 0 else None),
], line)

Wyświetl plik

@ -68,16 +68,9 @@ class Video2Dataset:
else:
srt_parser = None
if (self.blur_checker is not None and self.blur_checker.NeedPreProcess()):
log.ODM_INFO("Preprocessing for blur checker...")
self.blur_checker.PreProcess(input_file, self.parameters.start, self.parameters.end, self.parameters.internal_width, self.parameters.internal_height)
end = time.time()
log.ODM_INFO("Preprocessing time: {:.2f}s".format(end - start))
log.ODM_INFO("Calculated threshold is {}".format(self.blur_checker.threshold))
if (self.black_checker is not None and self.black_checker.NeedPreProcess()):
start2 = time.time()
log.ODM_INFO("Preprocessing for black checker...")
log.ODM_INFO("Preprocessing for black frame checker... this might take a bit")
self.black_checker.PreProcess(input_file, self.parameters.start, self.parameters.end, self.parameters.internal_width, self.parameters.internal_height)
end = time.time()
log.ODM_INFO("Preprocessing time: {:.2f}s".format(end - start2))
@ -199,15 +192,7 @@ class Video2Dataset:
_, buf = cv2.imencode('.' + self.parameters.frame_format, frame)
#start_time_utc = video_info.start_time_utc if video_info.start_time_utc is not None \
# else srt_parser.data[0].timestamp if srt_parser is not None \
# else datetime.datetime.now()
#elapsed_time_utc = start_time_utc + datetime.timedelta(seconds=(self.frame_index / video_info.frame_rate))
#elapsed_time = elapsed_time_utc + srt_parser.utc_offset if srt_parser is not None else elapsed_time_utc
delta = datetime.timedelta(seconds=(self.frame_index / video_info.frame_rate))
# convert to datetime
elapsed_time = datetime.datetime(1900, 1, 1) + delta
img = Image.open(io.BytesIO(buf))
@ -236,14 +221,16 @@ class Video2Dataset:
}}
if entry is not None:
segs = entry["shutter"].split("/")
exif_dict["Exif"][piexif.ExifIFD.ExposureTime] = (int(float(segs[0])), int(float(segs[1])))
exif_dict["Exif"][piexif.ExifIFD.FocalLength] = (entry["focal_len"], 1)
exif_dict["Exif"][piexif.ExifIFD.FNumber] = (entry["fnum"], 1)
exif_dict["Exif"][piexif.ExifIFD.ISOSpeedRatings] = (entry["iso"], 1)
exif_dict["GPS"] = get_gps_location(elapsed_time, entry["latitude"], entry["longitude"], entry["altitude"])
if entry["shutter"] is not None:
exif_dict["Exif"][piexif.ExifIFD.ExposureTime] = (1, int(entry["shutter"]))
if entry["focal_len"] is not None:
exif_dict["Exif"][piexif.ExifIFD.FocalLength] = (entry["focal_len"], 1)
if entry["fnum"] is not None:
exif_dict["Exif"][piexif.ExifIFD.FNumber] = float_to_rational(entry["fnum"])
if entry["iso"] is not None:
exif_dict["Exif"][piexif.ExifIFD.ISOSpeedRatings] = (entry["iso"], 1)
if entry["latitude"] is not None and entry["longitude"] is not None:
exif_dict["GPS"] = get_gps_location(elapsed_time, entry["latitude"], entry["longitude"], entry.get("altitude"))
exif_bytes = piexif.dump(exif_dict)
img.save(path, exif=exif_bytes)
@ -328,7 +315,7 @@ def get_gps_location(elapsed_time, lat, lng, altitude):
gps_ifd = {
piexif.GPSIFD.GPSVersionID: (2, 0, 0, 0),
piexif.GPSIFD.GPSDateStamp: elapsed_time.strftime('%Y:%m:%d %H:%M:%S')
piexif.GPSIFD.GPSDateStamp: elapsed_time.strftime('%Y:%m:%d')
}
if lat is not None and lng is not None: