-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
124 lines (95 loc) · 4.91 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
import cv2
import numpy as np
from flask import Flask, request, make_response, jsonify
import requests
from io import BytesIO
import os
from azure_blob_functions import upload_file_and_get_link, download_from_blob_storage
app = Flask(__name__)
@app.route('/')
def welcome():
return 'Welcome to the Depth Map API!'
@app.route('/depth-map', methods=['POST'])
def create_depth_map():
# Retrieve the two image files from the request
image1 = request.files['image1']
image2 = request.files['image2']
# Prompt the user to input values for numDisparities and blockSize
numDisparities = int(request.form.get('numDisparities', 16))
blockSize = int(request.form.get('blockSize', 15))
# Convert the images to grayscale
img1 = cv2.imdecode(np.fromstring(image1.read(), np.uint8), cv2.IMREAD_GRAYSCALE)
img2 = cv2.imdecode(np.fromstring(image2.read(), np.uint8), cv2.IMREAD_GRAYSCALE)
# Compute the depth map using OpenCV's StereoBM
stereo = cv2.StereoBM_create(numDisparities=numDisparities, blockSize=blockSize)
depth = stereo.compute(img1, img2)
# Convert the depth map to a PNG image
depth_png = cv2.imencode('.png', depth)[1]
# Return the depth map as a response
response = make_response(depth_png.tobytes())
response.headers.set('Content-Type', 'image/png')
response.headers.set('Content-Disposition', 'attachment', filename='depth_map.png')
return response
@app.route('/azure-blob-depth-map', methods=['POST'])
def create_azure_blob_depth_map():
# Retrieve the image URLs from the request
left_image_url = request.form.get('left_image_url')
right_image_url = request.form.get('right_image_url')
# Prompt the user to input values for numDisparities and blockSize
numDisparities = int(request.form.get('numDisparities', 16))
blockSize = int(request.form.get('blockSize', 15))
# Download the images from the URLs
left_image_response = requests.get(left_image_url)
right_image_response = requests.get(right_image_url)
if left_image_response.status_code != 200 or right_image_response.status_code != 200:
return "Failed to fetch images from the provided URLs", 400
# Read the images and convert them to grayscale
img1 = cv2.imdecode(np.frombuffer(left_image_response.content, np.uint8), cv2.IMREAD_GRAYSCALE)
img2 = cv2.imdecode(np.frombuffer(right_image_response.content, np.uint8), cv2.IMREAD_GRAYSCALE)
# Compute the depth map using OpenCV's StereoBM
stereo = cv2.StereoBM_create(numDisparities=numDisparities, blockSize=blockSize)
depth = stereo.compute(img1, img2)
# Convert the depth map to a PNG image
depth_png = cv2.imencode('.png', depth)[1]
# Return the depth map as a response
response = make_response(depth_png.tobytes())
response.headers.set('Content-Type', 'image/png')
response.headers.set('Content-Disposition', 'attachment', filename='depth_map.png')
return response
# Flask route to compute and upload a depth map
@app.route('/compute-and-upload-fetch-depth-map', methods=['POST'])
def compute_and_upload_depth_map_route():
# Retrieve the image URLs from the request
left_image_url = request.form.get('left_image_url')
right_image_url = request.form.get('right_image_url')
# Prompt the user to input values for numDisparities and blockSize
numDisparities = int(request.form.get('numDisparities', 16))
blockSize = int(request.form.get('blockSize', 15))
#get container name and the file name which be want to keep
container_name = request.form.get('container_name')
file_name = request.form.get('file_name')
firstname_name, extension = os.path.splitext(file_name)
if not extension:
file_name += ".png"
# Download the images from the URLs
left_image_response = requests.get(left_image_url)
right_image_response = requests.get(right_image_url)
if left_image_response.status_code != 200 or right_image_response.status_code != 200:
return "Failed to fetch images from the provided URLs", 400
# Read the images and convert them to grayscale
img1 = cv2.imdecode(np.frombuffer(left_image_response.content, np.uint8), cv2.IMREAD_GRAYSCALE)
img2 = cv2.imdecode(np.frombuffer(right_image_response.content, np.uint8), cv2.IMREAD_GRAYSCALE)
# Compute the depth map using OpenCV's StereoBM
stereo = cv2.StereoBM_create(numDisparities=numDisparities, blockSize=blockSize)
depth = stereo.compute(img1, img2)
# Convert the depth map to a PNG image
depth_png = cv2.imencode('.png', depth)[1]
#upload depth_map_and get link
try:
blob_url= upload_file_and_get_link(depth_png.tobytes(), file_name=file_name, container_name=container_name)
#response
return jsonify({"message": "Depth-Map Created Uploaded and Link Created Successfully", "blob_url": blob_url}), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(debug=True)