-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathapp.py
133 lines (107 loc) · 3.93 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
import codecs
import hashlib
import json
import threading
import time
import os
import argparse
import traceback
import requests
from flask import Flask, render_template, jsonify, send_file
from flask import request
import config as sys_config
import utils.tool as tool
app = Flask(__name__)
app.config.from_object('config')
mu = threading.Lock() # 创建一个锁
# Route to any template
@app.route('/')
def index():
names = [name for name in os.listdir(sys_config.SAMPLE_FILE_PATH) \
if name.split('.')[-1].lower() in sys_config.SAMPLE_TYPE_SET]
return render_template('index.html', \
sample_count=len(names), \
sample_type=sys_config.SAMPLE_FILE_TYPE)
@app.route('/<template>')
def route_template(template):
return render_template(template)
# 读取类别标签
@app.route('/api/annotation/labels', methods=['GET'])
def get_sample():
label_json = tool.get_labels()
result = dict()
result['message'] = 'success'
result['data'] = label_json
return jsonify(result)
# 读取标注样本
@app.route('/api/annotation/sample', methods=['GET'])
def get_labels():
if 'index' in request.args:
img_name = request.args['index'] + '.' + sys_config.SAMPLE_FILE_TYPE
img_path = os.path.join(sys_config.SAMPLE_FILE_PATH, img_name)
return send_file(img_path, mimetype='application/octet-stream',
as_attachment=True, attachment_filename=img_name)
else:
result = dict()
result['message'] = 'failure'
return jsonify(result)
# 标注接口
@app.route('/api/annotation/save', methods=['POST'])
def save_annotation():
tags = request.form['tags']
tags_new = ''
for tag in tags.split('\n'):
if tag == '':
continue
values = tag.split(',', maxsplit=1)
tags_new += values[0] + '.' + sys_config.SAMPLE_FILE_TYPE + ',' + values[1]+'\n'
path_annotation = 'annotation/annotation.txt'
try:
if mu.acquire(True):
if not os.path.exists(path_annotation):
file = codecs.open(path_annotation, mode='a+', encoding='utf-8')
file.close()
file = codecs.open(path_annotation, mode='a+', encoding='utf-8')
file.write(tags_new)
file.close()
mu.release()
except Exception as e:
print(e)
result = dict()
result['message'] = 'success'
return jsonify(result)
# Errors
@app.errorhandler(403)
def not_found_error(error):
return render_template('page_403.html'), 403
@app.errorhandler(404)
def not_found_error(error):
return render_template('page_404.html'), 404
@app.errorhandler(500)
def internal_error(error):
return render_template('page_500.html'), 500
def run():
app.run(debug=sys_config.DEBUG, host='0.0.0.0', port=sys_config.SERVER_PORT, threaded=True)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Object detection annotation service.')
parser.add_argument('--start', action='store_true', help='running background')
parser.add_argument('--stop', action='store_true', help='shutdown process')
parser.add_argument('--restart', action='store_true', help='restart process')
parser.add_argument('--daemon', action='store_true', help='restart process')
parser.add_argument('--convert2voc', action='store_true', help='restart process')
FLAGS = parser.parse_args()
if FLAGS.start:
if FLAGS.daemon:
tool.start_daemon_service(run, sys_config.PID_FILE)
else:
tool.start_service(run, sys_config.PID_FILE)
elif FLAGS.stop:
tool.shutdown_service(sys_config.PID_FILE)
elif FLAGS.restart:
tool.shutdown_service(sys_config.PID_FILE)
if FLAGS.daemon:
tool.start_daemon_service(run, sys_config.PID_FILE)
else:
tool.start_service(run, sys_config.PID_FILE)
elif FLAGS.convert2voc:
tool.convert_to_voc2007()