The Beautiful Future

Flask 이미지 받고 처리해서 보여주기 본문

스킬

Flask 이미지 받고 처리해서 보여주기

Small Octopus 2019. 5. 8. 17:04

import os
from flask import Flask
from flask import render_template
from flask import Flask, request, redirect, url_for
from werkzeug import secure_filename
import uuid
import cv2
import numpy as np

UPLOAD_FOLDER = 'static/tmp/input'
RESULT_FOLDER = 'static/result'
ALLOWED_EXTENSIONS = set([ 'png', 'jpg', 'jpeg', 'bmp'])
app = Flask(__name__, static_folder="static")
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

@app.route("/model")
@app.route("/model/<name>")
def page_model(name=None):
return render_template('index.html', name=name)

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


@app.route('/uploader', methods=['GET', 'POST'])
def uploader_file():
if request.method == 'POST':
f = request.files['file']

if f.filename == '':
return 'No selected file'

if f and allowed_file(f.filename):
img_np = cv2.imdecode(np.fromstring(f.read(), np.uint8), cv2.IMREAD_COLOR)
if img_np is not None:
result_save_folder = '%s/%s/'%(RESULT_FOLDER, uuid.uuid4().hex)
os.mkdir(result_save_folder)
result1_save_path = '%s/result1.png' % result_save_folder
result2_save_path = '%s/result2.png' % result_save_folder

img_np_canny =cv2.Canny(img_np, 50, 200)
cv2.rectangle(img_np, (5,5), (100,100), (0,255,255), 3)

cv2.imwrite(result1_save_path, img_np_canny)
cv2.imwrite(result2_save_path, img_np)
return render_template('show_result.html', name1=result1_save_path, name2=result2_save_path)


#cv2.imwrite('./abcdefg.png', img_np)
#print (img_np.shape)
#cv2.imshow('d', img_np)
#cv2.waitKey()



return 'file uploaded successfully'

def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

if __name__ == '__main__':
#app.run(host='0.0.0.0')
app.run(host='10.78.80.50')
#print (uuid.uuid4().hex)
#img = cv2.imread('D:/Project_program/hello-web-master/static/tmp/input/9.jpg')
#print (img.shape)
#cv2.imshow('d', img)
#cv2.waitKey()

'스킬' 카테고리의 다른 글

how to put latex equations on blog  (0) 2019.11.15
git commands  (0) 2019.11.12
Docker  (0) 2019.05.07
LAPACK 설치 windows  (0) 2019.04.04
ubuntu remote connection  (0) 2018.01.23
Comments