-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdarkflow-flask.py
More file actions
70 lines (55 loc) · 2.17 KB
/
darkflow-flask.py
File metadata and controls
70 lines (55 loc) · 2.17 KB
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
62
63
64
65
66
67
68
69
70
from darkflow.net.build import TFNet
import cv2
import tensorflow as tf
import threading
import numpy as np
import sys
import os
from PIL import Image
import imutils
from flask import Flask, request, redirect, render_template, url_for, flash
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = './static'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
options = {"model": "cfg/yolo.cfg", "load": "bin/yolo.weights", "threshold": 0.1, "gpu": 0.1}
tfnet = TFNet(options)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET','POST'])
def home():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return render_template('darkflow.html', result=predict(filename))
return render_template('home.html')
@app.route('/<key>')
def sample(key):
if key in ['computer','dog','eagle','giraffe','horses','office','person','scream']:
return render_template('darkflow.html', result=predict('sample_' + key + '.jpg'))
else:
return render_template('darkflow.html', result=predict('sample_dog.jpg'))
def predict(key):
imgcv = cv2.imread("./static/{}".format(key))
pred = tfnet.return_predict(imgcv)
for p in pred:
pt1 = (p["bottomright"]["x"], p["bottomright"]["y"])
pt2 = (p["topleft"]["x"],p["topleft"]["y"])
cv2.rectangle(imgcv, pt1, pt2, (255,0,0), 3)
cv2.imwrite('./static/predicted_{}'.format(key), imgcv)
return key
if __name__ == '__main__':
app.run(host='0.0.0.0')