-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.py
105 lines (83 loc) · 2.78 KB
/
service.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
import os
import shutil
import subprocess
import warnings
from pathlib import Path
import cv2
from flask import Flask, jsonify, make_response, request
app = Flask(__name__)
TEMP_IMAGE = "/tmp/temp.exr"
MAPPED_TEMP_IMAGE = "/tmp/temp.png"
TEMP_SCENE = "/tmp/scene.xml"
def encode_image(path_to_xml: str) -> bytes:
print("Rendering ...")
return_code = subprocess.call(["mitsuba", "-o", TEMP_IMAGE, path_to_xml])
print("Return code: {}".format(return_code))
# some flags of rendering causes that png is produced directly
if os.path.exists(TEMP_IMAGE):
print("Tone mapping ...")
return_code = subprocess.call(
[
"mtsutil",
"tonemap",
"-a",
"-f",
"png",
"-o",
MAPPED_TEMP_IMAGE,
TEMP_IMAGE,
]
)
print("Return code: {}".format(return_code))
os.remove(TEMP_IMAGE)
an_img = cv2.imread(MAPPED_TEMP_IMAGE, cv2.IMREAD_UNCHANGED)[..., ::-1]
os.remove(MAPPED_TEMP_IMAGE)
encoded = cv2.imencode(".png", an_img)[1].squeeze().tolist()
return encoded
@app.route("/render", methods=["GET", "POST"])
def render():
xml_data = request.data
xml_data = xml_data.decode()
try:
with open(TEMP_SCENE, "w") as f:
f.write(xml_data)
encoded = encode_image(TEMP_SCENE)
return jsonify(encoded)
except Exception as e:
print(e)
return make_response(jsonify(error=str(e)))
finally:
if os.path.exists(TEMP_SCENE):
os.remove(TEMP_SCENE)
return make_response()
@app.route("/render_zip", methods=["GET", "POST"])
def render_zip():
zip_file = request.files["zip"]
temporary_file_name = "/tmp/render_data.zip"
unpack_directory = "/tmp/render_data/"
zip_file.save(temporary_file_name)
try:
print("Unpacking ...")
os.mkdir(unpack_directory)
subprocess.call(["unzip", temporary_file_name, "-d", unpack_directory])
xml_files = list(Path(unpack_directory).rglob("*.xml"))
if len(xml_files) > 1:
warnings.warn(
f"Found XML {len(xml_files)} files in total, taking the first found"
)
xml_file = xml_files[0]
encoded = encode_image(xml_file)
shutil.rmtree(unpack_directory)
os.remove(temporary_file_name)
return jsonify(encoded)
except Exception as e:
print(e)
return make_response(jsonify(error=str(e)))
finally:
if os.path.exists(unpack_directory):
shutil.rmtree(unpack_directory)
if os.path.exists(temporary_file_name):
os.remove(temporary_file_name)
return make_response()
if __name__ == "__main__":
app.run(host="0.0.0.0", port="8000")