-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.py
More file actions
172 lines (137 loc) · 4.9 KB
/
app.py
File metadata and controls
172 lines (137 loc) · 4.9 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import base64
import colorsys
import io
import os
import click
import sqlalchemy
import werkzeug
from flask import Flask, jsonify
from flask_cors import CORS
from google.cloud import storage
def create_app(environment=os.environ.get('ENVIRONMENT', 'deployment')):
app = Flask(__name__)
CORS(app)
app.config.from_object('configs.' + environment)
from models import db
db.init_app(app)
from views.files import api
app.register_blueprint(api)
from views.file_annotations import api
app.register_blueprint(api)
from views.jobs import api
app.register_blueprint(api)
from views.categories import api
app.register_blueprint(api)
from views.paint_fill import api
app.register_blueprint(api)
@app.errorhandler(werkzeug.exceptions.HTTPException)
def handle_bad_request(e):
if e.name == 'Not Found':
return jsonify({
'error': 'Not found'
}), 404
elif e.name == 'Bad Request':
return jsonify({
'error': 'Error in arguments ({})'.format(', '.join(e.args))
}), 400
raise
@app.errorhandler(sqlalchemy.orm.exc.NoResultFound)
def handle_no_result_found(e):
return jsonify({
'error': 'Not found'
}), 404
@app.cli.command()
def create_db():
db.drop_all()
db.create_all()
@app.cli.command()
@click.argument('job_id')
@click.argument('export_path')
def export_annotations(job_id, export_path):
from models import File, Category, Job
from views.files import get_bucket
bucket = get_bucket()
job = Job.query.filter_by(id=job_id).one()
export_path = 'exports/' + export_path + '/'
index = 1
for file in job.files:
annotations = file.annotations
if annotations:
newest_annotation = sorted(
annotations, key=lambda a: a.created_at
)[-1]
categories = newest_annotation.categories
binary_annotation_data = base64.b64decode(
newest_annotation.data[len('data:image/png;base64,'):]
)
# Original
blob = list(bucket.list_blobs(prefix=file.bucket_path, max_results=1))[0]
file_object = io.BytesIO()
blob.download_to_file(file_object)
file_object.seek(0)
new_blob = bucket.blob(export_path + f'{index:04d}_original.png')
new_blob.upload_from_file(file_object)
# Annotation
new_blob = bucket.blob(export_path + f'{index:04d}_annotation.png')
new_blob.upload_from_string(binary_annotation_data)
# Labels
new_blob = bucket.blob(export_path + f'{index:04d}_categories.json')
new_blob.upload_from_string(categories)
index += 1
@app.cli.command()
@click.argument('prefix')
@click.argument('job_name')
@click.argument('categories')
def insert_file_info(prefix, job_name, categories):
# prefix = '70de_big/'
# job_name = 'Test images 70 degree angle'
category_names = [c.capitalize() for c in categories.split(',')]
from models import File, Category, Job
job = Job(
name=job_name,
prefix=prefix
)
db.session.add(job)
for category_index, category_name in enumerate(category_names):
h_value = category_index * 1.0 / len(category_names)
# Remove green
green_width_in_h_space = 0.33
green_start_in_h_space = 0.116
h_value = h_value * (1 - green_width_in_h_space)
if h_value > green_start_in_h_space:
h_value += green_width_in_h_space
HSV_tuple = (
h_value,
[0.3, 0.6, 0.9][category_index % 3],
[1.0, 0.9, 0.8][category_index % 3]
)
RGB_tuple = colorsys.hsv_to_rgb(*HSV_tuple)
db.session.add(Category(
job=job,
name=category_name,
color='#' + ''.join([
'{0:02x}'.format(int(255 * part))
for part in RGB_tuple
])
))
storage_client = storage.Client()
bucket = storage_client.get_bucket(
os.environ['BUCKET_NAME']
)
for blob in bucket.list_blobs(prefix=prefix):
if any(
blob.path.lower().endswith('.' + extension)
for extension in [
'jpg', 'jpeg', 'png'
]
):
db.session.add(File(
bucket_path=blob.name,
job=job,
))
db.session.commit()
return app
if __name__ == '__main__':
app = create_app()
else:
gunicorn_app = create_app()