File size: 795 Bytes
5e6da3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, render_template
from ultralytics import YOLO
import os

app = Flask(__name__)

# Load YOLOv8 model
model = YOLO("model/best.pt")

@app.route("/", methods=["GET", "POST"])
def index():
    result_img = None

    if request.method == "POST":
        file = request.files["image"]

        if file:
            input_path = os.path.join("static", "input.jpg")
            file.save(input_path)

            results = model(input_path)

            output_path = os.path.join("static", "result.jpg")
            results[0].save(filename=output_path)

            result_img = output_path

    return render_template("index.html", result_img=result_img)

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 7860)) 
    app.run(host="0.0.0.0", port=port)