-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
160 lines (118 loc) · 4.67 KB
/
app.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
import os
import cv2
import time
import shutil
import imutils
from pathlib import Path
from flask import Flask, request, jsonify, send_from_directory
from werkzeug.utils import secure_filename
from stitcher import Stitcher
from flask_cors import CORS
from panoramer import *
app = Flask(__name__)
CORS(app)
UPLOAD_FOLDER = "uploads"
RESULTS_FOLDER = "results"
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
app.config["MAX_CONTENT_LENGTH"] = 16 * 1024 * 1024
ALLOWED_EXTENSIONS = set(["png", "jpg", "jpeg"])
def allowed_file(filename):
return "." in filename and filename.split(".", 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/heartbeat", methods=["GET"])
def heartbeat():
heartbeat = time.monotonic_ns()
return {"heartbeat": heartbeat}, 200
@app.route("/upload", methods=["POST"])
def upload_image():
try:
# Clear the contents of the 'uploads' folder before saving new files
shutil.rmtree(UPLOAD_FOLDER)
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
# check if the post request contains files
if "files[]" not in request.files:
return jsonify({"message": "Images are required!"}), 400
files = request.files.getlist("files[]")
print("+++ uploaded files +++")
print(len(files))
for file in files:
print(f"FILE: {file.filename}")
print("+++ uploaded files +++")
is_uploaded = False
for file in files:
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
is_uploaded = True
else:
return jsonify({"message": "Invalid file type!"}), 400
if len(files) < 2:
return jsonify({"message": "At least two images are required!"}), 400
if is_uploaded:
return jsonify({"message": "Images uploaded successfully!"}), 201
except Exception as e:
return jsonify({"message": e}), 500
@app.route("/generate-panorama", methods=["GET"])
def generate_panorama():
try:
uploaded_files = os.listdir(UPLOAD_FOLDER)
# sorting files
uploaded_files = sorted(uploaded_files, key=lambda x: int(Path(x).stem))
if len(uploaded_files) < 2:
return (
jsonify({"message": "At least two images are required for stitching!"}),
400,
)
print(uploaded_files)
panorama = Panoramer(parent_folder=UPLOAD_FOLDER, img_name_list=uploaded_files)
panorama.generate()
# get list of generated files
results_folder = os.path.join(UPLOAD_FOLDER, "results")
generated_files = os.listdir(results_folder)
# group the files based on their names
grouped_results = {
"sift_correspondences": [],
"inliers_outliers": [],
"panoramas": [],
}
for file_name in generated_files:
if "sift_correspondence" in file_name:
grouped_results["sift_correspondences"].append( file_name)
elif "inliers" in file_name or "outliers" in file_name:
grouped_results["inliers_outliers"].append(file_name)
elif "panorama" in file_name:
grouped_results["panoramas"].append(file_name)
# sort files withing each group
for category, files in grouped_results.items():
grouped_results[category] = sorted(files)
return jsonify(
{
"message": "Panorama generated successfully!",
"results": grouped_results,
},
200,
)
except Exception as e:
return jsonify({"message": str(e)}), 500
@app.route("/clear-uploads", methods=["DELETE"])
def delete_uploads():
try:
# Clear the contents of the 'uploads' folder
shutil.rmtree(UPLOAD_FOLDER)
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
return jsonify({"message": "Uploads folder cleared successfully!"}), 200
except Exception as e:
return jsonify({"message": str(e)}), 500
@app.route("/serve-files/<path:filename>")
def serve_files(filename):
return send_from_directory(os.path.join(UPLOAD_FOLDER, RESULTS_FOLDER), filename)
@app.route("/serve-all-files")
def serve_all_files():
try:
results_folder = os.path.join(UPLOAD_FOLDER, RESULTS_FOLDER)
files = os.listdir(results_folder)
file_paths = [os.path.join(results_folder, file) for file in files]
return jsonify({"files": file_paths}), 200
except Exception as e:
return jsonify({"message": str(e)}), 500
if __name__ == "__main__":
app.run(debug=True)