Refactored GCP resize code to python

pull/838/head
Piero Toffanin 2020-03-23 11:45:46 -04:00
rodzic 9b83da5ca4
commit d896f8954d
3 zmienionych plików z 98 dodań i 35 usunięć

91
app/classes/gcp.py 100644
Wyświetl plik

@ -0,0 +1,91 @@
import glob
import os
import logging
logger = logging.getLogger('app.logger')
class GCPFile:
def __init__(self, gcp_path):
self.gcp_path = gcp_path
self.entries = []
self.raw_srs = ""
self.read()
def read(self):
if self.exists():
with open(self.gcp_path, 'r') as f:
contents = f.read().strip()
lines = list(map(str.strip, contents.split('\n')))
if lines:
self.raw_srs = lines[0] # SRS
for line in lines[1:]:
if line != "" and line[0] != "#":
parts = line.split()
if len(parts) >= 6:
self.entries.append(line)
else:
logger.warning("Malformed GCP line: %s" % line)
else:
logger.warning("GCP file %s does not exist" % self.gcp_path)
def iter_entries(self):
for entry in self.entries:
yield self.parse_entry(entry)
def parse_entry(self, entry):
if entry:
parts = entry.split()
x, y, z, px, py, filename = parts[:6]
extras = " ".join(parts[6:])
return GCPEntry(float(x), float(y), float(z), float(px), float(py), filename, extras)
def get_entry(self, n):
if n < self.entries_count():
return self.parse_entry(self.entries[n])
def entries_count(self):
return len(self.entries)
def exists(self):
return bool(self.gcp_path and os.path.exists(self.gcp_path))
def create_resized_copy(self, gcp_file_output, image_ratios):
"""
Creates a new resized GCP file from an existing GCP file. If one already exists, it will be removed.
:param gcp_file_output output path of new GCP file
:param image_ratios dictionary with "imagename" --> "resize_ratio" values
:return path to new GCP file
"""
if os.path.exists(gcp_file_output):
os.remove(gcp_file_output)
output = [self.raw_srs]
for entry in self.iter_entries():
entry.px *= image_ratios.get(entry.filename, 1.0)
entry.py *= image_ratios.get(entry.filename, 1.0)
output.append(str(entry))
with open(gcp_file_output, 'w') as f:
f.write('\n'.join(output) + '\n')
return gcp_file_output
class GCPEntry:
def __init__(self, x, y, z, px, py, filename, extras=""):
self.x = x
self.y = y
self.z = z
self.px = px
self.py = py
self.filename = filename
self.extras = extras
def __str__(self):
return "{} {} {} {} {} {} {}".format(self.x, self.y, self.z,
self.px, self.py,
self.filename,
self.extras).rstrip()

Wyświetl plik

@ -35,6 +35,7 @@ from nodeodm import status_codes
from nodeodm.models import ProcessingNode
from pyodm.exceptions import NodeResponseError, NodeConnectionError, NodeServerError, OdmError
from webodm import settings
from app.classes.gcp import GCPFile
from .project import Project
from functools import partial
@ -889,21 +890,17 @@ class Task(models.Model):
# Assume we only have a single GCP file per task
gcp_path = gcp_path[0]
resize_script_path = os.path.join(settings.BASE_DIR, 'app', 'scripts', 'resize_gcp.js')
dict = {}
image_ratios = {}
for ri in resized_images:
dict[os.path.basename(ri['path'])] = ri['resize_ratio']
image_ratios[os.path.basename(ri['path'])] = ri['resize_ratio']
try:
new_gcp_content = subprocess.check_output("node {} {} '{}'".format(quote(resize_script_path), quote(gcp_path), json.dumps(dict)), shell=True)
with open(gcp_path, 'w') as f:
f.write(new_gcp_content.decode('utf-8'))
logger.info("Resized GCP file {}".format(gcp_path))
return gcp_path
except subprocess.CalledProcessError as e:
gcpFile = GCPFile(gcp_path)
return gcpFile.create_resized_copy(gcp_path, image_ratios)
except Exception as e:
logger.warning("Could not resize GCP file {}: {}".format(gcp_path, str(e)))
return None
def create_task_directories(self):
"""

Wyświetl plik

@ -1,25 +0,0 @@
#!/usr/bin/env node
const fs = require('fs');
const Gcp = require('../static/app/js/classes/Gcp');
const argv = process.argv.slice(2);
function die(s){
console.log(s);
process.exit(1);
}
if (argv.length != 2){
die(`Usage: ./resize_gcp.js <path/to/gcp_file.txt> <JSON encoded image-->ratio map>`);
}
const [inputFile, jsonMap] = argv;
if (!fs.existsSync(inputFile)){
die('File does not exist: ' + inputFile);
}
const originalGcp = new Gcp(fs.readFileSync(inputFile, 'utf8'));
try{
const map = JSON.parse(jsonMap);
const newGcp = originalGcp.resize(map, true);
console.log(newGcp.toString());
}catch(e){
die("Not a valid JSON string: " + jsonMap);
}