Skip to content

Instantly share code, notes, and snippets.

@clpud
Created January 31, 2020 01:06
Show Gist options
  • Save clpud/60dae92cf586ea760ad84468846a3d82 to your computer and use it in GitHub Desktop.
Save clpud/60dae92cf586ea760ad84468846a3d82 to your computer and use it in GitHub Desktop.
Upload image to the server, then return to the client.
from flask import Flask, request, send_from_directory
from werkzeug.utils import secure_filename
import os
import cv2
UPLOAD_FOLDER = './teeth_img'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
return 'No file detected.'
file = request.files['file']
if file.filename == '':
return 'No file selected'
if not allowed_file(file.filename):
return 'File extension not support'
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
height, weight, channel = cv2.imread(os.path.join(
app.config['UPLOAD_FOLDER'], filename)).shape
# return "height: {}, weight: {}".format(height, weight)
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
return '''
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Upload</title>
</head>
<h1>Upload new File</h1>
<p>Allowed Extensions: .png, .jpg. .jpeg</p>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
</html>
'''
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment