-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
145 lines (126 loc) · 5.73 KB
/
app.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import json
import os
import sys
from flask import Flask, render_template, request, jsonify
from redis import Redis
from werkzeug.utils import secure_filename
sys.path.insert(0,"/openpose-master/MultiPersonMatching")
from common import parse_JSON_multi_person, parse_JSON_single_person, parse_JSON_multi_person_jochen
import posematching.multi_person as multi_person
import glob
import base64
UPLOAD_FOLDER = '/home/jochen/fotos/processing'
DOWNLOAD_FOLDER = '/home/jochen/fotos/json/'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['DOWNLOAD_FOLDER'] = DOWNLOAD_FOLDER
redis = Redis(host='127.0.0.1 ', port=6379)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def makeImageInput():
return render_template('imagePost.html')
@app.route('/upload', methods=['POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
return "no file sended"
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
return "no filename"
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return processfile(filename)
return
@app.route('/findmatch', methods=['POST'])
def return_posematch():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
return "no file sended"
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
return "no filename"
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
id = request.form.get('id');
return findmatch(filename,id)
return
@app.route('/uploadPose', methods=['POST'])
def add_new_pose():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
return "no file sended"
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
return "no filename"
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
id =0
for json in glob.iglob("/home/jochen/dataset/poses/*"):
id = id+1
filepath ="/home/jochen/dataset/poses/pose"+str(id)
os.system("mkdir -p " +filepath)
os.system("mkdir -p " +filepath+"/fotos")
os.system("mkdir -p " +filepath+"/json")
os.system("mkdir -p " +filepath+"/Processedfotos")
filename = filename.rsplit('.')[1]
filename = "0."+filename
file.save(os.path.join(filepath+"/fotos", filename))
os.system("./build/examples/openpose/openpose.bin -write_keypoint_json "+filepath+"/json -image_dir "+filepath+"/fotos -write_images "+filepath+"/Processedfotos -no_display")
os.system("mv "+filepath+"/json/1_keypoints.json " +filepath+"/json/0.json ")
return "pose added at id : " +str(id)
return
@app.route('/getPoses', methods=['GET'])
def return_html_poses():
return render_template('AllPoses.html')
@app.route('/getAllPoses', methods=['GET'])
def get_all_poses():
data = []
count = 0
for picture in glob.iglob("/home/jochen/dataset/poses/pose*/fotos/0.*"):
with open(picture, "rb") as image_file:
dummy ={}
dummy['naam'] = picture.rsplit('/fotos')[0].rsplit('/poses/')[1]
encoded_string = base64.b64encode(image_file.read())
dummy['foto']=encoded_string
data.append(dummy)
return json.dumps(data)
def processfile (filename):
os.system("./build/examples/openpose/openpose.bin -write_keypoint_json /home/jochen/fotos/json -image_dir /home/jochen/fotos/processing -write_images /home/jochen/fotos/calculated_poses/ -no_display")
os.system("mv -v /home/jochen/fotos/processing/* /home/jochen/fotos/poses")
filename = filename.rsplit('.')[0]
filename = filename +'_keypoints.json'
json_file = open(os.path.join(app.config['DOWNLOAD_FOLDER'], filename), "r")
json_data = json_file.read()
return json_data #jsonify(json_data)
def findmatch(filename, id):
os.system("./build/examples/openpose/openpose.bin -write_keypoint_json /home/jochen/fotos/json -image_dir /home/jochen/fotos/processing -write_images /home/jochen/fotos/calculated_poses/ -no_display")
os.system("mv -v /home/jochen/fotos/processing/* /home/jochen/fotos/poses")
filename = filename.rsplit('.')[0]
inputadr = "/home/jochen/fotos/json/"+filename +'_keypoints.json'
modeladr = "/home/jochen/dataset/poses/pose"+id+"/json/0.json"
#find matched
model_features = parse_JSON_single_person(modeladr)
input_features = parse_JSON_single_person(inputadr)
(result, error_score, input_transform) = multi_person.match(model_features, input_features, True)
print("Match or not: ", result)
with open(inputadr) as json_file:
json_decoded = json.load(json_file)
json_decoded['match'] = bool(result)
return jsonify(json_decoded)
return json_data#jsonify(json_data)
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)