-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.py
51 lines (46 loc) · 1.5 KB
/
Server.py
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
from flask import Flask, request, Response, render_template, send_from_directory, session
import jsonpickle
import numpy as np
import cv2
import os
from werkzeug.utils import secure_filename
from config import *
# Initialize the Flask application
app = Flask(__name__)
app.config.from_object(__name__)
def get_last_pics():
""" Return a list of the last 25 uploaded images"""
names = os.listdir("img")
return names
# route http posts to this method
@app.route('/', methods=['GET', 'POST'])
def test():
if request.method=='POST':
r = request
# convert string of image data to uint8
nparr = np.fromstring(r.data, np.uint8)
# decode image
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
names =os.listdir("img")
if not names:
img_name = 'img/image_0.jpg'
else:
names = [filter(str.isdigit, x) for x in names]
names = [int(x) for x in names]
num = max(names)
if num == 0:
num=1
else:
num=num+1
img_name = 'img/image_'+str(num)+'.jpg'
cv2.imwrite(img_name, img)
return "Image Uploaded"
else:
return render_template('upload.html', pics=get_last_pics())
@app.route('/img/<filename>')
def return_pic(filename):
print filename
""" Show just the image specified"""
return send_from_directory(app.config['UPLOAD_DIR'], secure_filename(filename))
# start flask app
app.run(host="127.0.0.1", port=5000)