Spaces:
Sleeping
Sleeping
File size: 2,587 Bytes
0f4a8fd 31c96da |
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 |
from quart import Quart, request, jsonify, send_from_directory, Blueprint
from quart_cors import cors
import json
import base64
# import os
import numpy as np
import cv2
import asyncio
import aiofiles
from controllerKlasifikasi import klasifikasiMangrove
app = Quart(__name__)
cors(app, allow_origin="*") # Mengaktifkan CORS
# Buat blueprint untuk file statis
static_bp = Blueprint('static', __name__, static_folder='gambar_data_tanaman')
# Tambahkan route untuk menyajikan gambar
@static_bp.route('/gambar_data_tanaman/<path:filename>')
async def serve_image(filename):
return await send_from_directory(static_bp.static_folder, filename)
# Daftarkan blueprint ke aplikasi Quart
app.register_blueprint(static_bp)
@app.route('/', methods=['GET'])
async def index():
return jsonify("hello world")
@app.route('/mangrove/get-data', methods=['GET'])
async def get_data():
async with aiofiles.open('dataTanaman.json', 'r') as file:
data = await file.read()
return jsonify(json.loads(data))
@app.route('/mangrove/upload-image', methods=['POST'])
async def upload_image():
request_json = await request.json
if 'image' not in request_json:
return jsonify({"error": "No image part in the request"}), 400
image_b64 = request_json['image']
# Decode base64 to image data
image_data = base64.b64decode(image_b64)
# Convert image data to OpenCV format
nparr = np.frombuffer(image_data, np.uint8)
img_cv = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
# Perform classification (synchronously)
klasifikasi_mangrove, id_mangrove = await asyncio.to_thread(klasifikasiMangrove, img_cv)
# Load dataTanaman.json
async with aiofiles.open('dataTanaman.json', 'r') as file:
data_tanaman_json = await file.read()
data_tanaman = json.loads(data_tanaman_json)
# Find data by ID
result_data = None
for data in data_tanaman:
if data['id'] == id_mangrove + 1:
result_data = data
break
if result_data:
return jsonify({
"message": "Sukses Klasifikasi Mangrove!",
"result": str(klasifikasi_mangrove),
"id": str(id_mangrove + 1),
"data_tanaman": result_data
})
else:
return jsonify({
"message": "Sukses Klasifikasi Mangrove!",
"result": str(klasifikasi_mangrove),
"id": str(id_mangrove),
"data_tanaman": "Data not found for ID " + str(id_mangrove + 1)
})
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, port=7860)
|