-
Notifications
You must be signed in to change notification settings - Fork 0
/
call_ODM_API.py
130 lines (107 loc) · 3.85 KB
/
call_ODM_API.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
126
127
128
129
130
from flask import Flask, request, jsonify
import requests
import glob
import json
SUCCESS = 0
NO_IMAGES = -1
NO_PROJECT = 1
NO_TASK = 2
NO_FILE = 3
NO_JSON = 4
app = Flask(__name__)
def create_task(input_data):
token = authenticate()
headers = {'Authorization': 'JWT {}'.format(token)}
images = glob.glob(input_data)
files = []
for image_path in images:
files.append(('images', (image_path, open(image_path, 'rb'), 'image/png')))
if len(files) < 2:
json_data = {
"task_id": "",
"project_id": NO_IMAGES,
"authentication": token
}
with open("data.json", "w") as json_file:
json.dump(json_data, json_file)
return json_data
projecturl = "http://localhost:8000/api/projects/"
data = {
"name": "API_Call"
}
project_id = requests.post(projecturl, headers=headers, data=data).json()['id']
taskurl = "http://localhost:8000/api/projects/" + str(project_id) + "/tasks/"
task_id = requests.post(taskurl, headers = headers, files=files).json()['id']
json_data = {
"task_id": task_id,
"project_id": project_id,
"authentication": token
}
with open("data.json", "w") as json_file:
json.dump(json_data, json_file)
return json_data
def authenticate():
url = "http://localhost:8000/api/token-auth/"
data = {
"username": "authentication",
"password": "authkeyword"
}
response = requests.post(url, data=data)
return response.json()['token']
def filePathJSON(status, task, path):
data = {
"error_code": status,
"webodm_status": task,
"path": path
}
with open("filepath.json", "w") as json_file:
json.dump(data, json_file)
return data
def getFilePath(json_file, request_type):
try:
with open(json_file, 'r') as file:
json_data = json.load(file)
except:
return filePathJSON(NO_JSON, "", "")
if json_data['project_id'] is NO_IMAGES:
return filePathJSON(NO_PROJECT, "", "")
headers = {'Authorization': 'JWT {}'.format(json_data['authentication'])}
project_id = json_data['project_id']
task_id = json_data['task_id']
task = requests.get('http://localhost:8000/api/projects/{}/tasks/{}'.format(project_id, task_id),
headers=headers).json()
print(task)
try:
available_assets = task['available_assets']
except:
return filePathJSON(NO_TASK, "", "")
available_assets = task['available_assets']
if request_type == "texturemap":
if "textured_model.zip" in available_assets:
path = '/var/lib/docker/volumes/webodm_appmedia/_data/project/{}/task/{}/assets/odm_texturing'.format(project_id, task_id)
return filePathJSON(SUCCESS, task['status'], path)
else:
return filePathJSON(NO_FILE, task['status'], "")
else:
if "georeferenced_model.laz" in available_assets:
path = '/var/lib/docker/volumes/webodm_appmedia/_data/project/{}/task/{}/assets/odm_georeferencing/odm_georeferenced_model.laz'.format(project_id, task_id)
return filePathJSON(SUCCESS, task['status'], path)
else:
return filePathJSON(NO_FILE, task['status'], "")
# API endpoint
@app.route('/task', methods=['POST'])
def task_api():
data = request.get_json()
input_data = data['input']
processed_data = create_task(input_data)
return processed_data
@app.route('/getfile', methods=['GET'])
def getFile_api():
data = request.get_json()
input_data = data['input']
request_type = data['request']
print(request_type)
output = getFilePath(input_data, request_type)
return output if output else "Not available"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=9000, debug=True)