-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfullcall_ODM_API.py
219 lines (177 loc) · 7.37 KB
/
fullcall_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from flask import Flask, request, send_file, jsonify
import mysql.connector
import requests
import glob
import json
import time
import os
import zipfile
import tempfile
import shutil
SUCCESS = 0
NO_IMAGES = -1
NO_PROJECT = 1
NO_TASK = 2
NO_FILE = 3
NO_JSON = 4
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 4 * 1024 * 1024 * 1024 # 4 gb limit
def create_task(file):
mydb = mysql.connector.connect(
host="localhost",
user="root", # Your MySQL username
password="", # Your MySQL password (if any)
port=3308, # Your MySQL port
unix_socket="/opt/lampp/var/mysql/mysql.sock"
)
cursor = mydb.cursor()
cursor.execute("USE sample")
token = authenticate()
headers = {'Authorization': 'JWT {}'.format(token)}
temp_dir = tempfile.mkdtemp()
zip_filepath = os.path.join(temp_dir, file.filename)
file.save(zip_filepath)
# Extract the contents of the zip file
extract_dir = os.path.join(temp_dir, 'extracted_folder')
with zipfile.ZipFile(zip_filepath, 'r') as zip_ref:
zip_ref.extractall(extract_dir)
# Now you can process the contents of the extracted folder
# For example, print the list of files extracted
extracted_files = os.listdir(extract_dir)
print(extract_dir)
print(f"Extracted files: {extracted_files}")
images = glob.glob(extract_dir + "/*.JPG") + glob.glob(extract_dir + "/*.jpg") + glob.glob(extract_dir + "/*.png") + glob.glob(extract_dir + "/*.PNG")
SQLid = -1
files = []
for image_path in images:
files.append(('images', (image_path, open(image_path, 'rb'), 'image/png')))
print("images: " + str(files))
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)
print("INSERT INTO whole_data (status) VALUES (1)")
cursor.execute("INSERT INTO whole_data (status) VALUES (1)")
mydb.commit()
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']
print("INSERT INTO whole_data (status) VALUES (3)")
cursor.execute("INSERT INTO whole_data (status) VALUES (3)")
mydb.commit()
SQLid = cursor.lastrowid
while True:
res = requests.get('http://localhost:8000/api/projects/{}/tasks/{}/'.format(project_id, task_id),
headers={'Authorization': 'JWT {}'.format(token)}).json()
try:
if res['status'] == 40:
print("Task has completed!")
break
elif res['status'] == 30:
print("Task failed: {}".format(res))
cursor.execute("UPDATE whole_data SET status = 4 WHERE uid = " + SQLid)
mydb.commit()
cursor.close()
mydb.close()
shutil.rmtree(temp_dir)
return "UPDATE whole_data SET status = 2 WHERE uid = " + str(SQLid)
else:
print("Processing, hold on...")
time.sleep(30)
except:
print("Task failed: {}".format(res))
cursor.execute("UPDATE whole_data SET status = 4 WHERE uid = " + SQLid)
mydb.commit()
cursor.close()
mydb.close()
shutil.rmtree(temp_dir)
return "UPDATE whole_data SET status = 2 WHERE uid = " + str(SQLid)
update_query = "UPDATE whole_data SET status = 4, project_id = %s, task_id = %s, whole_las_path = %s, all_files = %s, whole_glb_path = %s WHERE uid = %s"
tm_str = getFilePath(token, project_id, task_id, "texturemap")
all_str = getFilePath(token, project_id, task_id, "all")
pc_str = getFilePath(token, project_id, task_id, "pointcloud")
update_data = (project_id, task_id, pc_str, all_str, tm_str, SQLid)
cursor.execute(update_query, update_data)
mydb.commit()
cursor.close()
mydb.close()
shutil.rmtree(temp_dir)
return "update done"
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 getFilePath(headers, project_id, task_id, request_type):
headers = {'Authorization': 'JWT {}'.format(headers)}
task = requests.get('http://localhost:8000/api/projects/{}/tasks/{}'.format(project_id, task_id),
headers=headers).json()
available_assets = task['available_assets']
if request_type == "texturemap":
if "textured_model.zip" in available_assets:
return 'webodm.boshang.online/api/projects/{}/tasks/{}/download/textured_model.glb'.format(project_id, task_id)
else:
return "textured zip file not found"
elif request_type == "pointcloud":
if "georeferenced_model.laz" in available_assets:
return 'webodm.boshang.online/api/projects/{}/tasks/{}/download/georeferenced_model.laz'.format(project_id, task_id)
else:
return "laz file not found"
else:
if "all.zip" in available_assets:
return 'webodm.boshang.online/api/projects/{}/tasks/{}/download/all.zip'.format(project_id, task_id)
else:
return "all.zip file not found"
def test_db():
token = authenticate()
project_id = 123
task_id = "1423cc6e-0218-495a-9818-e10114997b9e"
tm_str = getFilePath(token, project_id, task_id, "texturemap")
pc_str = getFilePath(token, project_id, task_id, "pointcloud")
result = tm_str + " " + pc_str
print(tm_str + " " + pc_str)
return result
def unzip_folder(file):
with tempfile.TemporaryDirectory() as temp_dir:
zip_filepath = os.path.join(temp_dir, file.filename)
file.save(zip_filepath)
# Extract the contents of the zip file
extract_dir = os.path.join(temp_dir, 'extracted_folder')
with zipfile.ZipFile(zip_filepath, 'r') as zip_ref:
zip_ref.extractall(extract_dir)
# Now you can process the contents of the extracted folder
# For example, print the list of files extracted
extracted_files = os.listdir(extract_dir)
print(extract_dir)
print(f"Extracted files: {extracted_files}")
return extract_dir
# API endpoint
@app.route('/task', methods=['POST'])
def task_api():
if 'file' not in request.files:
return jsonify({'error': 'No file part in the request'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No file selected for uploading'}), 400
# folder = unzip_folder(file)
# print(jsonify({'message': 'Folder uploaded and extracted successfully'}), 200)
processed_data = create_task(file)
return processed_data
# API endpoint
@app.route('/test', methods=['POST'])
def test_api():
return test_db()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=2000, debug=True)