PatipanM/CloudcomputingTest
0
1from flask import Flask, request, jsonify2from PIL import Image3import time4import io5from ultralytics import YOLO6 7app = Flask(__name__)8model = YOLO('mix320x160.pt') # โมเดลที่เทรนไว้9 10@app.route('/', methods=['POST'])11def detect_flame():12 start = time.time()13 14 img_file = request.files.get("image")15 if img_file is None:16 return jsonify({"error": "No image provided"}), 40017 18 image = Image.open(io.BytesIO(img_file.read())).convert("RGB")19 results = model(image)20 rtt = time.time() - start21 22 # แปลงผลลัพธ์: [label, confidence]23 detections = []24 names = model.names # เช่น {0: 'flame', ...}25 26 for box in results[0].boxes:27 cls_id = int(box.cls.item())28 conf = round(box.conf.item(), 2)29 label = names.get(cls_id, f"class_{cls_id}")30 detections.append({"label": label, "confidence": conf})31 32 return jsonify({33 "RTT_ms": round(rtt * 1000, 2),34 "detections": detections35 })36 37if __name__ == '__main__':38 app.run(host="0.0.0.0", port=7860)