forked from shkh/TrainingAssistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
93 lines (70 loc) · 2.3 KB
/
views.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
# -*- coding: utf-8 -*-
from flask import *
import os, sys
import json
import re
import sys
app = Flask(__name__)
app.secret_key = '佐久間さん可愛い'
#画像の準備
image_ptrn = re.compile('.*[.](jpg|jpeg|png|bmp|gif)$')
image_dir = os.path.join( 'static', 'img' )
images = []
images = [ image for image in os.listdir( image_dir ) if re.match( image_ptrn, image ) ]
if not len( images ):
sys.exit( 'Error: Could not find images')
logf = open('log.dat', 'w')
pos = 0
@app.route('/')
def index():
global pos
#正例と負例用のファイル
global positive
global negative
positive = open('info.dat', 'a')
negative = open('bg.txt', 'a')
#最初の画像
imgsrc = os.path.join( image_dir, images[pos] )
imgnum = len(images)
count = pos
counter = ''.join( [ str(pos+1).zfill( len(str(imgnum)) ), ' of ', str(imgnum) ] )
return render_template( 'index.html', imgsrc=imgsrc, imgnum=imgnum, count=count, counter=counter )
@app.route('/_next')
def _next():
global pos
#その画像をスキップするか
skip = request.args.get('skip')
if skip == u'0':
#囲まれた範囲の座標
coords = request.args.get('coords')
coords = json.loads(coords)
#処理中の画像のパス
image_path = os.path.join( image_dir, images[pos] )
#正例か負例か
if len(coords) == 0:
negative.write( ''.join( [ image_path, '\n' ] ) )
logf.write( ''.join( [ image_path, '\n' ] ) )
logf.flush()
else:
s = ''
for coord in coords:
s = ' '.join( [ s, ' '.join( [ str(int(e)) for e in coord ] ) ] )
positive.write('%s %d%s\n' % (image_path, len(coords), s))
logf.write( "%s %d%s\n" % (image_path, len(coords), s) )
logf.flush()
#まだ画像があるか
if pos+1 >= len(images):
imgsrc = ""
finished = True
pos = pos + 1
logf.close()
negative.close()
positive.close()
else:
finished = False
imgsrc = os.path.join( image_dir, images[pos+1] )
pos = pos + 1
return jsonify( imgsrc=imgsrc, finished=finished, count=pos )
if __name__ == '__main__':
app.debug = True
app.run()