Mahiruoshi commited on
Commit
15bbc31
·
verified ·
1 Parent(s): ea770b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -70
app.py CHANGED
@@ -1,70 +1,70 @@
1
- import time
2
- import os
3
- from flask import Flask, request, jsonify
4
- from flask_cors import CORS
5
-
6
- from model import *
7
-
8
- app = Flask(__name__)
9
- CORS(app)
10
- model = load_model()
11
-
12
- os.makedirs('uploads', exist_ok=True)
13
-
14
- @app.route('/', methods=['GET', 'POST'])
15
- def main_endpoint():
16
- if request.method == 'GET':
17
- return jsonify({
18
- "result": "ok",
19
- "service": "RPI Image Recognition API",
20
- "endpoints": {
21
- "GET /": "API status and documentation",
22
- "POST /": "Image prediction (upload 'file')",
23
- "GET /stitch": "Image stitching"
24
- },
25
- "model_loaded": model is not None
26
- })
27
-
28
- elif request.method == 'POST':
29
- if 'file' not in request.files:
30
- return jsonify({"error": "No file uploaded"}), 400
31
-
32
- file = request.files['file']
33
- if file.filename == '':
34
- return jsonify({"error": "No file selected"}), 400
35
-
36
- filename = file.filename
37
- file.save(os.path.join('uploads', filename))
38
-
39
- # filename format: "<timestamp>_<obstacle_id>_<signal>.jpeg"
40
- constituents = file.filename.split("_")
41
- obstacle_id = constituents[1] if len(constituents) > 1 else "unknown"
42
-
43
- ## Week 8 ##
44
- signal = constituents[2].strip(".jpg") if len(constituents) > 2 else "default"
45
- image_id = predict_image(filename, model, signal)
46
-
47
- ## Week 9 ##
48
- # We don't need to pass in the signal anymore
49
- #image_id = predict_image_week_9(filename,model)
50
-
51
- # Return the obstacle_id and image_id
52
- result = {
53
- "obstacle_id": obstacle_id,
54
- "image_id": image_id
55
- }
56
- return jsonify(result)
57
-
58
- @app.route('/stitch', methods=['GET'])
59
- def stitch():
60
- """
61
- This is the main endpoint for the stitching command. Stitches the images using two different functions, in effect creating two stitches, just for redundancy purposes
62
- """
63
- img = stitch_image()
64
- img.show()
65
- img2 = stitch_image_own()
66
- img2.show()
67
- return jsonify({"result": "ok"})
68
-
69
- if __name__ == '__main__':
70
- app.run(host='0.0.0.0', port=5000, debug=True)
 
1
+ import time
2
+ import os
3
+ from flask import Flask, request, jsonify
4
+ from flask_cors import CORS
5
+
6
+ from model import *
7
+
8
+ app = Flask(__name__)
9
+ CORS(app)
10
+ model = load_model()
11
+
12
+ os.makedirs('uploads', exist_ok=True)
13
+
14
+ @app.route('/', methods=['GET', 'POST'])
15
+ def main_endpoint():
16
+ if request.method == 'GET':
17
+ return jsonify({
18
+ "result": "ok",
19
+ "service": "RPI Image Recognition API",
20
+ "endpoints": {
21
+ "GET /": "API status and documentation",
22
+ "POST /": "Image prediction (upload 'file')",
23
+ "GET /stitch": "Image stitching"
24
+ },
25
+ "model_loaded": model is not None
26
+ })
27
+
28
+ elif request.method == 'POST':
29
+ if 'file' not in request.files:
30
+ return jsonify({"error": "No file uploaded"}), 400
31
+
32
+ file = request.files['file']
33
+ if file.filename == '':
34
+ return jsonify({"error": "No file selected"}), 400
35
+
36
+ filename = file.filename
37
+ file.save(os.path.join('uploads', filename))
38
+
39
+ # filename format: "<timestamp>_<obstacle_id>_<signal>.jpeg"
40
+ constituents = file.filename.split("_")
41
+ obstacle_id = constituents[1] if len(constituents) > 1 else "unknown"
42
+
43
+ ## Week 8 ##
44
+ signal = constituents[2].strip(".jpg") if len(constituents) > 2 else "default"
45
+ image_id = predict_image(filename, model, signal)
46
+
47
+ ## Week 9 ##
48
+ # We don't need to pass in the signal anymore
49
+ #image_id = predict_image_week_9(filename,model)
50
+
51
+ # Return the obstacle_id and image_id
52
+ result = {
53
+ "obstacle_id": obstacle_id,
54
+ "image_id": image_id
55
+ }
56
+ return jsonify(result)
57
+
58
+ @app.route('/stitch', methods=['GET'])
59
+ def stitch():
60
+ """
61
+ This is the main endpoint for the stitching command. Stitches the images using two different functions, in effect creating two stitches, just for redundancy purposes
62
+ """
63
+ img = stitch_image()
64
+ img.show()
65
+ img2 = stitch_image_own()
66
+ img2.show()
67
+ return jsonify({"result": "ok"})
68
+
69
+ if __name__ == '__main__':
70
+ app.run(host='0.0.0.0', port=7860, debug=True)