forked from LuisSoe/plastictrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
47 lines (35 loc) · 1.25 KB
/
Copy pathapi.py
File metadata and controls
47 lines (35 loc) · 1.25 KB
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
import os
from flask import Flask, request, jsonify
from flask_cors import CORS
import cv2
import numpy as np
from ml.classifier import PlastiTraceClassifier
app = Flask(__name__)
allowed_origins = os.environ.get("ALLOWED_ORIGINS", "*")
if allowed_origins == "*":
CORS(app)
else:
CORS(app, origins=[origin.strip() for origin in allowed_origins.split(",") if origin.strip()])
# Load model
classifier = PlastiTraceClassifier("models/plastitrace.pth")
@app.route('/api/classify', methods=['POST'])
def classify():
try:
# Get image from request
if 'image' not in request.files:
return jsonify({'error': 'No image provided'}), 400
file = request.files['image']
# Read image
img_bytes = file.read()
nparr = np.frombuffer(img_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
# Classify
result = classifier.predict_from_bgr(img)
return jsonify(result)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/health', methods=['GET'])
def health():
return jsonify({'status': 'ok'})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001, debug=True) # Ganti dari 5000 ke 5001