-
Notifications
You must be signed in to change notification settings - Fork 2
/
__init__.py
66 lines (56 loc) · 2.16 KB
/
__init__.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
import os
import json
from flask import Flask, request, render_template, g, redirect, Response, url_for, session, send_from_directory
from flask import jsonify
import json
from grabcut import *
abs_path ='./' # '/home/ecenaz/research/demo_grabcut/'
tmpl_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')
app = Flask(__name__, template_folder=tmpl_dir)
@app.route('/')
def home():
return render_template("grabcut_demo.html")
@app.route('/do_grabcut', methods=['POST'])
def do_grabcut():
data = request.get_json()
img_file = data['img_file']
project_name = data['project_name']
row_id = data['row_id']
rect_coords = data['rect_coords']
rect_coords_scaled = cal_rect_coords(rect_coords)
new_file = grabcut_rect(abs_path + 'static/images/'+img_file, rect_coords_scaled, project_name, row_id)
return jsonify(saved_file = new_file)
@app.route('/refine_grabcut', methods=['POST'])
def refine_grabcut():
data = request.get_json()
bg_drawing = data['bg_drawing']
fg_drawing = data['fg_drawing']
img_file = data['img_file']
rect_coords = data['rect_coords']
project_name = data['project_name']
row_id = data['row_id']
rect_coords_scaled = cal_rect_coords(rect_coords)
scale_drawing(bg_drawing)
scale_drawing(fg_drawing)
new_file = grabcut_drawing(abs_path + 'static/images/'+img_file, rect_coords_scaled, fg_drawing,bg_drawing,project_name,row_id)
print(new_file)
return jsonify(saved_file = new_file)
def scale_drawing(drawing):
scale_factor = drawing['scale_factor']
drawing['thickness'] = drawing['thickness'] / scale_factor
for line in drawing['lines']:
path = line['path']
for path_seg in path:
path_seg_type = path_seg[0]
if (path_seg_type == 'Q'):
path_seg[2] = path_seg[2] / scale_factor
path_seg[1] = path_seg[1] / scale_factor
def cal_rect_coords(rect_coords):
factor = rect_coords['scale_factor']
rect_coords['left'] = rect_coords['left'] / factor
rect_coords['height'] = rect_coords['height'] / factor
rect_coords['width'] = rect_coords['width'] / factor
rect_coords['top'] = rect_coords['top'] / factor
return rect_coords
if __name__ == "__main__":
app.run(debug=True)