-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
91 lines (76 loc) · 2.67 KB
/
app.py
File metadata and controls
91 lines (76 loc) · 2.67 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import logging
from flask import Flask, render_template, request
import os
from werkzeug.utils import secure_filename
from flask import jsonify
import base64
import cv2
import numpy as np
from crowd_count import Counting
from matplotlib import cm as c
import matplotlib.pyplot as plt
from flask_ngrok import run_with_ngrok
from flask_cors import CORS
config = {}
config["IMAGE_UPLOADS"] = "data"
config["ALLOWED_IMAGE_EXTENSIONS"] = ["JPEG", "JPG", "PNG", "GIF"]
app = Flask(__name__)
run_with_ngrok(app)
CORS(app)
@app.route('/')
def hello_world():
return render_template("hello.html")
@app.route('/input')
def input():
return render_template("input.html")
@app.route('/submit', methods=['GET', 'POST'])
def submit():
if request.method == 'POST':
try:
data = request.files["file"]
filename = secure_filename(data.filename)
data.save(os.path.join(config["IMAGE_UPLOADS"], filename))
path = os.path.join(config["IMAGE_UPLOADS"], filename)
result = detect_persons(path)
people_count = np.sum(result, dtype=np.float32)
# people_count = 200
except Exception as e:
print(e)
# json_data = request.get_json()
# recipe_title = json_data['file']
people_count = 100
print("People count is ", people_count)
if people_count == 0:
faceDetected = False
num_faces = 0
to_send = ''
else:
faceDetected = True
num_faces = int(people_count)
# In memory
image = plt.imshow(result, cmap=c.jet)
plt.savefig('data/output_image.jpg')
image = cv2.imread('data/output_image.jpg')
image_content = cv2.imencode('.jpg', image)[1].tostring()
encoded_image = base64.encodestring(image_content)
to_send = 'data:image/jpg;base64, ' + str(encoded_image, 'utf-8')
print("Final count is ", num_faces)
# ret = {
# 'faces': faceDetected,
# 'count': num_faces
# }
# return jsonify(ret)
return render_template('index.html', faceDetected=faceDetected, num_faces=num_faces, image_to_show=to_send,
init=True)
def detect_persons(img_path):
model_file = "files/model_reduce_filter.json"
weight_file = "files/model_weights_1_rmsprop.h5"
obj = Counting(model_file, weight_file)
result = obj.predict_img(img_path)
return result
def draw_rectangle(img, rect):
'''Draw a rectangle on the image'''
(x, y, w, h) = rect
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), 2)
if __name__ == '__main__':
app.run()