Kimang18 commited on
Commit
7a43999
·
1 Parent(s): 25bcc0f

refactor code

Browse files
Files changed (1) hide show
  1. app.py +26 -23
app.py CHANGED
@@ -29,32 +29,39 @@ if not os.path.exists(OUT_FOLDER):
29
  os.makedirs(OUT_FOLDER)
30
 
31
 
 
 
 
 
 
 
 
 
32
  @app.route('/', methods=['GET', 'POST'])
33
  def index():
34
- # user_id = session.get('_id')
35
- user_id = request.cookies.get('session')
36
  if user_id is None:
37
  user_id = uuid.uuid4()
38
  session['_id'] = user_id
39
  logger.info(user_id)
40
- file_path = os.path.join(OUT_FOLDER, f'{user_id}.csv')
41
- with open(file_path, 'w') as f:
42
  f.write("image,id,name,xMin,xMax,yMin,yMax\n")
43
- print(user_id)
44
 
45
  if request.method == 'POST':
46
  if 'file' not in request.files:
47
  flash('No files selected')
48
  return redirect('/')
49
- img_folder = os.path.join(UPLOAD_FOLDER, f'{user_id}')
50
  try:
51
- os.makedirs(img_folder)
52
  except FileExistsError:
53
  logger.info('user already has an active session')
54
  files = request.files.getlist("file")
55
  filenames = []
56
  for f in files:
57
- f.save(os.path.join(img_folder, f.filename))
58
  filenames.append(f.filename)
59
  app.config["FILES"] = filenames
60
  # logger.info(app.config["FILES"], app.config["HEAD"])
@@ -70,12 +77,11 @@ def tagger():
70
  app.config["HEAD"] = 0
71
  return redirect(url_for('final'))
72
  user_id = session.get('_id')
73
- directory = app.config["IMAGES"] + f'/{user_id}'
74
  image = app.config["FILES"][app.config["HEAD"]]
75
  labels = app.config["LABELS"]
76
  not_end = not(app.config["HEAD"] == len(app.config["FILES"]) - 1)
77
- # print(not_end)
78
- return render_template('tagger.html', not_end=not_end, directory=directory, image=image, labels=labels, head=app.config["HEAD"] + 1, len=len(app.config["FILES"]))
79
 
80
 
81
  @app.route('/next')
@@ -83,7 +89,8 @@ def next():
83
  image = app.config["FILES"][app.config["HEAD"]]
84
  app.config["HEAD"] = app.config["HEAD"] + 1
85
  user_id = session.get("_id")
86
- with open(f"{app.config['OUT']}/{user_id}.csv", 'a+') as f:
 
87
  for label in app.config["LABELS"]:
88
  f.write(image + "," +
89
  label["id"] + "," +
@@ -124,22 +131,18 @@ def label(id):
124
 
125
  @app.route('/image/<f>')
126
  def images(f):
127
- #user_id = session.get('_id')
128
- user_id = request.cookies.get('session')
129
- # images = app.config["IMAGES"] + f'/{user_id}'
130
- img_path = os.path.join(UPLOAD_FOLDER, f'{user_id}/{f}')
131
- print(img_path)
132
- # print(images, '/', f)
133
- # return send_file(images +'/'+f)
134
  return send_file(img_path)
135
 
136
  @app.route('/download')
137
  def download():
138
  user_id = session.get('_id')
139
- anno_path = f"{app.config['OUT']}/{user_id}.csv"
140
- directory = app.config["IMAGES"] + f'/{user_id}'
141
- shutil.copyfile(anno_path, f'{directory}/annotations.csv')
142
- shutil.make_archive('final', 'zip', directory)
143
  return send_file('final.zip',
144
  mimetype='text/csv',
145
  download_name='final.zip',
 
29
  os.makedirs(OUT_FOLDER)
30
 
31
 
32
+ def get_anno_path(user_id):
33
+ return os.path.join(OUT_FOLDER, f'{user_id}.csv')
34
+
35
+
36
+ def get_images_directory(user_id):
37
+ return os.path.join(UPLOAD_FOLDER, f'{user_id}')
38
+
39
+
40
  @app.route('/', methods=['GET', 'POST'])
41
  def index():
42
+ user_id = session.get('_id')
43
+ # user_id = request.cookies.get('session')
44
  if user_id is None:
45
  user_id = uuid.uuid4()
46
  session['_id'] = user_id
47
  logger.info(user_id)
48
+ anno_path = get_anno_path(user_id)
49
+ with open(anno_path, 'w') as f:
50
  f.write("image,id,name,xMin,xMax,yMin,yMax\n")
 
51
 
52
  if request.method == 'POST':
53
  if 'file' not in request.files:
54
  flash('No files selected')
55
  return redirect('/')
56
+ img_dir = get_images_directory(user_id)
57
  try:
58
+ os.makedirs(img_dir)
59
  except FileExistsError:
60
  logger.info('user already has an active session')
61
  files = request.files.getlist("file")
62
  filenames = []
63
  for f in files:
64
+ f.save(os.path.join(img_dir, f.filename))
65
  filenames.append(f.filename)
66
  app.config["FILES"] = filenames
67
  # logger.info(app.config["FILES"], app.config["HEAD"])
 
77
  app.config["HEAD"] = 0
78
  return redirect(url_for('final'))
79
  user_id = session.get('_id')
80
+ img_dir = get_images_directory(user_id) # app.config["IMAGES"] + f'/{user_id}'
81
  image = app.config["FILES"][app.config["HEAD"]]
82
  labels = app.config["LABELS"]
83
  not_end = not(app.config["HEAD"] == len(app.config["FILES"]) - 1)
84
+ return render_template('tagger.html', not_end=not_end, directory=img_dir, image=image, labels=labels, head=app.config["HEAD"] + 1, len=len(app.config["FILES"]))
 
85
 
86
 
87
  @app.route('/next')
 
89
  image = app.config["FILES"][app.config["HEAD"]]
90
  app.config["HEAD"] = app.config["HEAD"] + 1
91
  user_id = session.get("_id")
92
+ anno_path = get_anno_path(user_id)
93
+ with open(anno_path, 'a+') as f:
94
  for label in app.config["LABELS"]:
95
  f.write(image + "," +
96
  label["id"] + "," +
 
131
 
132
  @app.route('/image/<f>')
133
  def images(f):
134
+ user_id = session.get('_id')
135
+ img_dir = get_images_directory(user_id)
136
+ img_path = os.path.join(img_dir, f'{f}')
 
 
 
 
137
  return send_file(img_path)
138
 
139
  @app.route('/download')
140
  def download():
141
  user_id = session.get('_id')
142
+ anno_path = get_anno_path(user_id)
143
+ img_dir = get_images_directory(user_id)
144
+ shutil.copyfile(anno_path, f'{img_dir}/annotations.csv')
145
+ shutil.make_archive('final', 'zip', img_dir)
146
  return send_file('final.zip',
147
  mimetype='text/csv',
148
  download_name='final.zip',