Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: subprocess processing temporary file in Windows #510

Merged
merged 7 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions mapillary_tools/geotag/geotag_from_gopro.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import datetime
import logging
import os
Expand Down Expand Up @@ -109,11 +110,24 @@ def extract_and_parse_bin(path: str) -> T.List:
if stream_id is None:
raise IOError("No GoPro metadata track found - was GPS turned on?")

with tempfile.NamedTemporaryFile() as tmp:
LOG.debug("Extracting GoPro stream %s to %s", stream_id, tmp.name)
ffmpeg.extract_stream(path, tmp.name, stream_id)
LOG.debug("Parsing GoPro GPMF %s", tmp.name)
return parse_bin(tmp.name)
# https://github.com/mapillary/mapillary_tools/issues/503
if sys.platform == "win32":
delete = False
else:
delete = True

with tempfile.NamedTemporaryFile(delete=delete) as tmp:
try:
LOG.debug("Extracting GoPro stream %s to %s", stream_id, tmp.name)
ffmpeg.extract_stream(path, tmp.name, stream_id)
LOG.debug("Parsing GoPro GPMF %s", tmp.name)
return parse_bin(tmp.name)
finally:
if not delete:
try:
os.remove(tmp.name)
except FileNotFoundError:
pass


def get_points_from_gpmf(path: str) -> T.List[types.GPXPoint]:
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
sample-5s.mp4 is downloaded from https://samplelib.com/sample-mp4.html with no license restrictions

hero8.mp4 is downloaded from https://github.com/gopro/gpmf-parser with MIT license
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sample-5s.mp4 is downloaded from https://samplelib.com/sample-mp4.html with no license restrictions

hero8.mp4 is downloaded from https://github.com/gopro/gpmf-parser with MIT license
Binary file not shown.
137 changes: 137 additions & 0 deletions tests/integration/test_gopro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import os
import json
import subprocess

import pytest
import py.path

from .test_process import setup_config, EXECUTABLE, is_ffmpeg_installed


IMPORT_PATH = "tests/integration/mapillary_tools_process_images_provider/gopro_data"

expected_descs = [
{
"MAPLatitude": 42.0266244,
"MAPLongitude": -129.2943386,
"MAPCaptureTime": "2019_11_18_23_42_08_645",
"MAPAltitude": 9540.24,
"MAPCompassHeading": {
"TrueHeading": 123.93587938690177,
"MagneticHeading": 123.93587938690177,
},
"filename": "hero8.mp4/hero8_000001.jpg",
},
{
"MAPLatitude": 38.40647030477047,
"MAPLongitude": -127.91879935228401,
"MAPCaptureTime": "2019_11_18_23_42_10_645",
"MAPAltitude": 8210.638894516123,
"MAPCompassHeading": {
"TrueHeading": 164.2783143490891,
"MagneticHeading": 164.2783143490891,
},
"filename": "hero8.mp4/hero8_000002.jpg",
},
{
"MAPLatitude": 35.33045525508525,
"MAPLongitude": -126.85656645076101,
"MAPCaptureTime": "2019_11_18_23_42_12_645",
"MAPAltitude": 7111.61486484729,
"MAPCompassHeading": {
"TrueHeading": 140.84083032165609,
"MagneticHeading": 140.84083032165609,
},
"filename": "hero8.mp4/hero8_000003.jpg",
},
{
"MAPLatitude": 35.67994968324376,
"MAPLongitude": -126.96633932025631,
"MAPCaptureTime": "2019_11_18_23_42_14_645",
"MAPAltitude": 7234.620097836571,
"MAPCompassHeading": {
"TrueHeading": 139.9975566712228,
"MagneticHeading": 139.9975566712228,
},
"filename": "hero8.mp4/hero8_000004.jpg",
},
{
"MAPLatitude": 36.62692461557483,
"MAPLongitude": -127.27854373218855,
"MAPCaptureTime": "2019_11_18_23_42_16_645",
"MAPAltitude": 7570.467814029364,
"MAPCompassHeading": {
"TrueHeading": 137.69409398454923,
"MagneticHeading": 137.69409398454923,
},
"filename": "hero8.mp4/hero8_000005.jpg",
},
{
"MAPLatitude": 36.9141600088776,
"MAPLongitude": -127.37008598994919,
"MAPCaptureTime": "2019_11_18_23_42_18_645",
"MAPAltitude": 7673.03659529988,
"MAPCompassHeading": {
"TrueHeading": 344.835906081436,
"MagneticHeading": 344.835906081436,
},
"filename": "hero8.mp4/hero8_000006.jpg",
},
]


@pytest.fixture
def setup_data(tmpdir: py.path.local):
data_path = tmpdir.mkdir("data")
source = py.path.local(IMPORT_PATH)
source.copy(data_path)
yield data_path
if tmpdir.check():
tmpdir.remove(ignore_errors=True)


def test_process_gopro_hero8(
tmpdir: py.path.local, setup_data: py.path.local, setup_config: py.path.local
):
if not is_ffmpeg_installed:
pytest.skip("skip because ffmpeg not installed")
os.environ["MAPILLARY_CONFIG_PATH"] = str(setup_config)
upload_dir = tmpdir.mkdir("mapillary_public_uploads")
os.environ["MAPILLARY_UPLOAD_PATH"] = str(upload_dir)
video_path = setup_data.join("hero8.mp4")
x = subprocess.run(
f"{EXECUTABLE} video_process --geotag_source=gopro_videos {str(video_path)} --interpolation_use_gpx_start_time",
shell=True,
)
assert x.returncode == 0, x.stderr
desc_path = setup_data.join("mapillary_sampled_video_frames").join(
"mapillary_image_description.json"
)
assert desc_path.exists()
with open(desc_path) as fp:
# print(fp.read())
descs = json.load(fp)
for expected, actual in zip(expected_descs, descs):
assert abs(expected["MAPLatitude"] - actual["MAPLatitude"]) < 0.0001
assert abs(expected["MAPLongitude"] - actual["MAPLongitude"]) < 0.0001
assert expected["MAPCaptureTime"] == actual["MAPCaptureTime"]
assert abs(expected["MAPAltitude"] - actual["MAPAltitude"]) < 0.0001
assert (
abs(
expected["MAPCompassHeading"]["TrueHeading"]
- actual["MAPCompassHeading"]["TrueHeading"]
)
< 0.0001
)
assert (
abs(
expected["MAPCompassHeading"]["MagneticHeading"]
- actual["MAPCompassHeading"]["MagneticHeading"]
)
< 0.0001
)
assert expected["filename"] == actual["filename"]
assert {
"strings": [{"key": "mapillary_tools_version", "value": "0.8.2"}]
} == actual["MAPMetaTags"]
assert "MAPSequenceUUID" in actual