-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_service.py
69 lines (52 loc) · 2.33 KB
/
api_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
"""
API Service Module
------------------
This module sets up and defines the Flask API for the image processing application. It provides
endpoints for clients, such as retrieving processed image data. It also saves the retrieving processed image into
the directory.
Functions:
- get_frames(depth_min, depth_max): Retrieves and returns color-mapped frames within a specified depth range.
"""
import sqlite3
from flask import Flask, request, jsonify
from color_mapping import apply_color_map
import matplotlib.pyplot as plt
app = Flask(__name__)
def get_frames(depth_min, depth_max):
"""
Retrieves and returns color-mapped frames within a specified depth range from the database.
This endpoint processes GET requests and expects 'depth_min' and 'depth_max' parameters.
It fetches frames from the database within the specified depth range, applies color map to each frame,
and returns the color-mapped frames in a JSON format.
Parameters:
depth_min (float): Minimum depth value for the frames to retrieve.
depth_max (float): Maximum depth value for the frames to retrieve.
Returns:
JSON: A list of objects with 'depth' and 'frame' (base64 encoded image data).
"""
conn = sqlite3.connect('images.db')
cursor = conn.cursor()
cursor.execute("SELECT * FROM image_data WHERE depth BETWEEN ? AND ?", (depth_min, depth_max))
rows = cursor.fetchall()
conn.close()
frames = []
for row in rows:
frame_data = row[1:] # excluding depth value
colorized_frame = apply_color_map(frame_data)
frames.append({'depth': row[0], 'frame': colorized_frame})
return frames
@app.route('/get_frames', methods=['GET'])
def api_get_frames():
depth_min = request.args.get('depth_min', type=float)
depth_max = request.args.get('depth_max', type=float)
if depth_min is None or depth_max is None:
return "Invalid request. Please specify depth_min and depth_max.", 400
frames = get_frames(depth_min, depth_max)
for image in frames:
depth = image['depth']
plt.savefig(f"/Users/fatihbicer/PycharmProjects/pythonProject2/image_depth_{depth}.png", bbox_inches='tight')
plt.show()
plt.close()
return jsonify(frames)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000, debug=True)