File size: 2,015 Bytes
53c6c4a
 
353d4c7
 
 
 
94327e5
fa898f6
53c6c4a
fa898f6
94327e5
 
 
53c6c4a
94327e5
 
fa898f6
53c6c4a
 
0478b85
353d4c7
53c6c4a
 
0478b85
 
 
 
 
 
94327e5
0478b85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa898f6
53c6c4a
0478b85
 
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
from flask import Flask, render_template, request, jsonify
from ultralytics import YOLO
import os
import tempfile
import base64
import json
from werkzeug.utils import secure_filename

app = Flask(__name__)

# Configure maximum content length for file uploads (10MB)
app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024

# Load OSMF model
model_path = os.getenv('MODEL_PATH', 'best.pt')
osmf = YOLO(model_path, task="classify")

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/predict', methods=['POST'])
def predict():
    if 'image' not in request.files:
        return jsonify({'error': 'No image provided', 'status': 'error'}), 400
    
    file = request.files['image']
    if not file.filename:
        return jsonify({'error': 'Empty file provided', 'status': 'error'}), 400

    try:
        # Secure the filename and create temporary file
        filename = secure_filename(file.filename)
        with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file:
            file.save(temp_file.name)
            temp_path = temp_file.name
            
        # Process image
        predict = osmf(temp_path)
        results = predict[0].to_json()
        predict_dict = json.loads(results)
        
        # Extract prediction results
        name = predict_dict[0]["name"]
        confidence = float(predict_dict[0]["confidence"]) * 100
            
    except Exception as e:
        return jsonify({
            'error': str(e),
            'status': 'error'
        }), 500
        
    finally:
        # Cleanup temporary files
        try:
            if 'temp_file' in locals():
                os.close(temp_file.fileno())
                os.unlink(temp_path)
        except Exception:
            pass
            
    return jsonify({
        'class': name,
        'confidence': f"{confidence:.2f}%",
        'status': 'success'
    })

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