Kimang18 commited on
Commit
43bd8fd
·
1 Parent(s): d806786

adapt download folder to image dataset style

Browse files
Files changed (1) hide show
  1. app.py +40 -16
app.py CHANGED
@@ -11,8 +11,8 @@ from flask import send_file
11
  logger = logging.getLogger(__name__)
12
  logging.basicConfig(level=logging.INFO)
13
 
14
- UPLOAD_FOLDER = './images'
15
- OUT_FOLDER = './anno'
16
 
17
  app = Flask(__name__)
18
  app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
@@ -20,14 +20,14 @@ app.config["SECRET_KEY"] = '#thaistartsfirst!'
20
  app.config["IMAGES"] = UPLOAD_FOLDER
21
  app.config["OUT"] = OUT_FOLDER
22
  app.config["LABELS"] = []
23
- app.config["CATEGORIES"] = {} # []
24
  app.config["HEAD"] = 0
25
  app.config["SESSION_PERMANENT"] = False
26
 
27
  # Ensure the upload directory exists
28
- if not os.path.exists(UPLOAD_FOLDER):
29
  os.makedirs(UPLOAD_FOLDER)
30
- if not os.path.exists(OUT_FOLDER):
31
  os.makedirs(OUT_FOLDER)
32
 
33
 
@@ -49,7 +49,7 @@ def index():
49
  logger.info(user_id)
50
  anno_path = get_anno_path(user_id)
51
  with open(anno_path, 'w') as f:
52
- f.write("image,category,name,xMin,xMax,yMin,yMax\n")
53
 
54
  if request.method == 'POST':
55
  if 'file' not in request.files:
@@ -79,10 +79,11 @@ def tagger():
79
  app.config["HEAD"] = 0
80
  return redirect(url_for('final'))
81
  user_id = session.get('_id')
82
- img_dir = get_images_directory(user_id) # app.config["IMAGES"] + f'/{user_id}'
 
83
  image = app.config["FILES"][app.config["HEAD"]]
84
  labels = app.config["LABELS"]
85
- not_end = not(app.config["HEAD"] == len(app.config["FILES"]) - 1)
86
  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"]))
87
 
88
 
@@ -95,12 +96,12 @@ def next():
95
  with open(anno_path, 'a+') as f:
96
  for label in app.config["LABELS"]:
97
  f.write(image + "," +
98
- str(label["category"]) + "," +
99
- label["name"] + "," +
100
- str(round(float(label["xMin"]))) + "," +
101
- str(round(float(label["xMax"]))) + "," +
102
- str(round(float(label["yMin"]))) + "," +
103
- str(round(float(label["yMax"]))) + "\n")
104
  app.config["LABELS"] = []
105
  return redirect(url_for('tagger'))
106
 
@@ -116,7 +117,8 @@ def add(id):
116
  xMax = request.args.get("xMax")
117
  yMin = request.args.get("yMin")
118
  yMax = request.args.get("yMax")
119
- app.config["LABELS"].append({"id":id, "name":"", "category": "", "xMin":xMin, "xMax":xMax, "yMin":yMin, "yMax":yMax})
 
120
  return redirect(url_for('tagger'))
121
 
122
 
@@ -155,7 +157,29 @@ def download():
155
  user_id = session.get('_id')
156
  anno_path = get_anno_path(user_id)
157
  img_dir = get_images_directory(user_id)
158
- shutil.copyfile(anno_path, f'{img_dir}/annotations_pascal_voc.csv')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  download_zip = os.path.join(img_dir, 'final')
160
  print(download_zip)
161
  shutil.make_archive('final', 'zip', img_dir)
 
11
  logger = logging.getLogger(__name__)
12
  logging.basicConfig(level=logging.INFO)
13
 
14
+ UPLOAD_FOLDER = 'images'
15
+ OUT_FOLDER = 'anno'
16
 
17
  app = Flask(__name__)
18
  app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
 
20
  app.config["IMAGES"] = UPLOAD_FOLDER
21
  app.config["OUT"] = OUT_FOLDER
22
  app.config["LABELS"] = []
23
+ app.config["CATEGORIES"] = {} # []
24
  app.config["HEAD"] = 0
25
  app.config["SESSION_PERMANENT"] = False
26
 
27
  # Ensure the upload directory exists
28
+ if not os.path.exists(os.path.join(os.getcwd(), UPLOAD_FOLDER)):
29
  os.makedirs(UPLOAD_FOLDER)
30
+ if not os.path.exists(os.path.join(os.getcwd(), OUT_FOLDER)):
31
  os.makedirs(OUT_FOLDER)
32
 
33
 
 
49
  logger.info(user_id)
50
  anno_path = get_anno_path(user_id)
51
  with open(anno_path, 'w') as f:
52
+ f.write("image,category,name,xMin,yMin,xMax,yMax\n")
53
 
54
  if request.method == 'POST':
55
  if 'file' not in request.files:
 
79
  app.config["HEAD"] = 0
80
  return redirect(url_for('final'))
81
  user_id = session.get('_id')
82
+ # app.config["IMAGES"] + f'/{user_id}'
83
+ img_dir = get_images_directory(user_id)
84
  image = app.config["FILES"][app.config["HEAD"]]
85
  labels = app.config["LABELS"]
86
+ not_end = not (app.config["HEAD"] == len(app.config["FILES"]) - 1)
87
  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"]))
88
 
89
 
 
96
  with open(anno_path, 'a+') as f:
97
  for label in app.config["LABELS"]:
98
  f.write(image + "," +
99
+ str(label["category"]) + "," +
100
+ label["name"] + "," +
101
+ str(round(float(label["xMin"]))) + "," +
102
+ str(round(float(label["yMin"]))) + "," +
103
+ str(round(float(label["xMax"]))) + "," +
104
+ str(round(float(label["yMax"]))) + "\n")
105
  app.config["LABELS"] = []
106
  return redirect(url_for('tagger'))
107
 
 
117
  xMax = request.args.get("xMax")
118
  yMin = request.args.get("yMin")
119
  yMax = request.args.get("yMax")
120
+ app.config["LABELS"].append(
121
+ {"id": id, "name": "", "category": "", "xMin": xMin, "xMax": xMax, "yMin": yMin, "yMax": yMax})
122
  return redirect(url_for('tagger'))
123
 
124
 
 
157
  user_id = session.get('_id')
158
  anno_path = get_anno_path(user_id)
159
  img_dir = get_images_directory(user_id)
160
+ data = {}
161
+ with open(anno_path, 'r') as f:
162
+ for line in f.readlines():
163
+ elems = line.strip().split(',')
164
+ file_name = data.get(elems[0], None)
165
+ if file_name is None:
166
+ data[elems[0]] = {'bbox': [], 'categories': [], 'names': []}
167
+ data[elems[0]]['categories'].append(int(elems[1]))
168
+ data[elems[0]]['names'].append(elems[2])
169
+ data[elems[0]]['bbox'].append([int(elems[i]) for i in range(3, len(elems))])
170
+ img_dataset = []
171
+ for file_name, obj in data.items():
172
+ img_dataset.append(
173
+ {
174
+ 'file_name': file_name,
175
+ 'objects': obj
176
+ }
177
+ )
178
+ with open(f'{img_dir}/metadata.jsonl', 'w') as f:
179
+ for item in img_dataset:
180
+ f.write(json.dumps(item) + "\n")
181
+
182
+ #shutil.copyfile(anno_path, f'{img_dir}/annotations_pascal_voc.csv')
183
  download_zip = os.path.join(img_dir, 'final')
184
  print(download_zip)
185
  shutil.make_archive('final', 'zip', img_dir)