File size: 2,387 Bytes
545f292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, render_template, request, session
import os
import subprocess
import glob
from PIL import Image
import shutil
import base64
import io


app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = '/Users/furkanuysal/Desktop/DataScienceRepository/satellite_imagery_ship_detection_cnn/static'

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

def run_detection(image_path, weights_path):
    shutil.rmtree('/Users/furkanuysal/Desktop/DataScienceRepository/satellite_imagery_ship_detection_cnn/predictions/')
    cmd = ['python', '/Users/furkanuysal/Desktop/DataScienceRepository/satellite_imagery_ship_detection_cnn/yolov5/detect.py',
	'--project', '/Users/furkanuysal/Desktop/DataScienceRepository/satellite_imagery_ship_detection_cnn/predictions' ,
	'--source', image_path, '--weights', weights_path, '--name', '1']
	
    return subprocess.run(cmd)

@app.route('/submit', methods=['POST'])
def upload_file():

    file = request.files['file']

    if file:
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
        image_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
        weights_path = '/Users/furkanuysal/Desktop/DataScienceRepository/satellite_imagery_ship_detection_cnn/tgrs_yolov5m_weights.pt'
        output = run_detection(image_path, weights_path)
        output_image_path = glob.glob('/Users/furkanuysal/Desktop/DataScienceRepository/satellite_imagery_ship_detection_cnn/predictions/1/*')[0]
        

        def pillow_image_to_base64_string(img):
            buffered = io.BytesIO()
            img.save(buffered, format="JPEG")
            return base64.b64encode(buffered.getvalue()).decode("utf-8")

        def base64_string_to_pillow_image(base64_str):
            return Image.open(io.BytesIO(base64.decodebytes(bytes(base64_str, "utf-8"))))

        my_img = Image.open(output_image_path)
        data_url = 'data:image/jpeg;base64,' + pillow_image_to_base64_string(my_img)
        size = Image.open(output_image_path)
        width = size.width
        height = size.height
        #return render_template('index.html', message='Upload success', output_image_path=output_image_path)
        
        return render_template('index.html', message = 'Detection Success',data_url=data_url,
        width=width/2, height=height/2)


if __name__ == '__main__':
    app.run(debug=True)