Spaces:
Runtime error
Runtime error
| 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' | |
| 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) | |
| 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) | |