-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
377 lines (294 loc) · 11 KB
/
main.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import json
from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse
from typing import Dict, List, Annotated
from fastapi import FastAPI, Request, HTTPException, Form, UploadFile
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from app.config import Configuration
from app.forms.classification_form import ClassificationForm
from app.forms.enhancement_form import EnhancementForm
from app.ml.classification_utils import classify_image
from PIL import ImageEnhance, Image
import base64
from io import BytesIO
from app.forms.histogram_form import HistogramForm
from app.utils import list_images
import matplotlib.pyplot as plt
import cv2
import numpy as np
import magic
app = FastAPI()
config = Configuration()
app.mount("/static", StaticFiles(directory="app/static"), name="static")
templates = Jinja2Templates(directory="app/templates")
@app.get("/info")
def info() -> Dict[str, List[str]]:
"""Returns a dictionary with the list of models and
the list of available image files."""
list_of_images = list_images()
list_of_models = Configuration.models
data = {"models": list_of_models, "images": list_of_images}
return data
@app.get("/", response_class=HTMLResponse)
def home(request: Request):
"""The home page of the service."""
return templates.TemplateResponse("home.html", {"request": request})
@app.get("/classifications")
def create_classify(request: Request):
return templates.TemplateResponse(
"classification_select.html",
{"request": request, "images": list_images(), "models": Configuration.models},
)
@app.post("/classifications")
async def request_classification(request: Request):
form = ClassificationForm(request)
await form.load_data()
image_id = form.image_id
model_id = form.model_id
classification_scores = classify_image(model_id=model_id, img_id=image_id)
return templates.TemplateResponse(
"classification_output.html",
{
"request": request,
"image_id": image_id,
"classification_scores": json.dumps(classification_scores),
},
)
# Issue No.4 Upload Image Button -------------------------------------------------------------------
@app.get("/custom_classifications")
def create_classify(request: Request):
"""
Create the page for custom image classification.
Args:
request: Request: The request object.
Returns:
TemplateResponse: The page for custom image classification.
"""
return templates.TemplateResponse(
"custom_classification_select.html",
{"request": request, "models": Configuration.models},
)
@app.post("/custom_classifications")
async def upload_file(file: UploadFile, request: Request):
"""
Uploads a file (an image) and classifies it.
Args:
file: UploadFile: The uploaded file.
request: Request: The request object.
Returns:
TemplateResponse: The page with the classification results.
"""
try:
# Legge il contenuto del file
file_content = await file.read()
# Ottiene il tipo MIME del file
mime = magic.Magic(mime=True)
file_type = mime.from_buffer(file_content)
# Verifica se il file è un'immagine
if not file_type.startswith("image"):
raise ValueError("Uploaded file is not an image")
# Salva il file temporaneamente in un buffer
buffer = BytesIO(file_content)
img = Image.open(buffer)
# Converte l'immagine in PNG e la memorizza in un buffer
png_buffer = BytesIO()
img.save(png_buffer, format="PNG")
image_64 = base64.b64encode(png_buffer.getvalue()).decode("utf-8")
data_url = f"data:image/png;base64,{image_64}"
# Classifica l'immagine
form = ClassificationForm(request)
await form.load_data()
model_id = form.model_id
classification_scores = classify_image(model_id=model_id, img_id=None, custom_img_id=img)
# Invia il file dal buffer
return templates.TemplateResponse(
"custom_classification_output.html",
{
"request": request,
"image_id": data_url,
"classification_scores": json.dumps(classification_scores),
},
)
except Exception as e:
return {"error": f"An error occurred during the file upload: {str(e)}"}
# Issue No.3 Download Results Button -------------------------------------------------------------------
@app.get("/download_results", response_class=JSONResponse)
def download_results(classification_scores):
"""
Download the classification scores as a JSON file.
Args:
classification_scores: str: The classification scores as a JSON string.
Returns:
JSONResponse: The classification scores as a JSON file.
"""
# Extract the labels and scores from the JSON string
results = json.loads(classification_scores)
labels = [result[0] for result in results][::-1]
scores = [result[1] for result in results][::-1]
# Create a dictionary with the classification scores
results_dict = {labels[0]: scores[0],
labels[1]: scores[1],
labels[2]: scores[2],
labels[3]: scores[3],
labels[4]: scores[4]}
return JSONResponse(
content=results_dict,
media_type="application/json",
headers={"Content-Disposition": "attachment; filename=results.json"}
)
@app.get("/download_plot", response_class=StreamingResponse)
async def download_plot(classification_scores: str):
"""
Download the classification scores as a graph image.
Args:
classification_scores: str: The classification scores as a JSON string.
Returns:
StreamingResponse: The classification scores as a bar plot.
"""
# Extract the labels and scores from the JSON string
results = json.loads(classification_scores)
labels = [result[0] for result in results][::-1]
scores = [result[1] for result in results][::-1]
# Create the bar plot
fig, ax = plt.subplots(figsize=(10, 6))
colors = [
(63 / 255, 3 / 255, 85 / 255, 0.8),
(6 / 255, 33 / 255, 108 / 255, 0.8),
(121 / 255, 87 / 255, 3 / 255, 0.8),
(117 / 255, 0 / 255, 20 / 255, 0.8),
(26 / 255, 74 / 255, 4 / 255, 0.8)
]
ax.barh(labels, scores, color=colors)
ax.set_xlabel('Scores')
ax.set_title('Top 5 Classification Scores')
ax.grid()
# Save the plot in a buffer
img_buffer = BytesIO()
plt.savefig(img_buffer, format='png')
plt.close(fig)
# Take the plot from the buffer
img = Image.open(img_buffer)
img_byte = BytesIO()
img.save(img_byte, format='PNG')
img.close()
img_byte.seek(0)
return StreamingResponse(
content=img_byte,
media_type="image/png",
headers={"Content-Disposition": "attachment; filename=plot.png"}
)
# Issue No.2 Image Transformation -------------------------------------------------------------------
@app.get("/enhancement")
def create_transformed_image(request: Request):
"""
Create the page for image enhancement.
Args:
request: Request: The request object.
Returns:
TemplateResponse: The page for image enhancement.
"""
return templates.TemplateResponse(
"enhancement_select.html",
{"request": request, "images": list_images(), "models": Configuration.models},
)
@app.post("/enhancement")
async def apply_transformation(request: Request):
"""
Create the page with the transformed image.
Args:
request: Request: The request object.
Returns:
TemplateResponse: The page with the transformed image.
"""
# Load the form data
form = EnhancementForm(request)
await form.load_data()
# Apply the image transformation by calling the function apply_image_transformation
image_id = form.image_id
transformation_params = {
"color": form.color,
"brightness": form.brightness,
"contrast": form.contrast,
"sharpness": form.sharpness,
}
transformed_image_path = apply_image_transformation(image_id, transformation_params)
return templates.TemplateResponse(
"enhancement_output.html",
{"request": request,
"color": form.color,
"brightness": form.brightness,
"contrast": form.contrast,
"sharpness": form.sharpness,
"image_id": image_id,
"transformed_image_path": transformed_image_path
}
)
def apply_image_transformation(image_id, params):
"""
The function that applies the image transformation.
Args:
image_id: str: The image ID.
params: dict: The transformation parameters.
Returns:
str: The path of the transformed image.
"""
try:
image_path = f"app/static/imagenet_subset/{image_id}"
img = Image.open(image_path)
enhancer = ImageEnhance.Color(img)
img = enhancer.enhance(params["color"])
enhancer = ImageEnhance.Brightness(img)
img = enhancer.enhance(params["brightness"])
enhancer = ImageEnhance.Contrast(img)
img = enhancer.enhance(params["contrast"])
enhancer = ImageEnhance.Sharpness(img)
img = enhancer.enhance(params["sharpness"])
buffer = BytesIO()
img.save(buffer, format="PNG")
image_64= base64.b64encode(buffer.getvalue()).decode("utf-8")
data_url = f"data:image/png;base64,{image_64}"
return data_url
except Exception as e:
error_message = f"Error during image transformation: {str(e)}"
print(error_message)
raise HTTPException(status_code=500, detail=error_message)
# Issue No.1 Image Histogram -------------------------------------------------------------------
@app.get("/image_histogram")
def create_histogram(request: Request):
"""
Create the page for image histogram.
Args:
request: Request: The request object.
Returns:
TemplateResponse: The page for image histogram.
"""
return templates.TemplateResponse(
"histogram_select.html",
{"request": request, "images": list_images()},
)
@app.post("/image_histogram")
async def request_classification(request: Request):
"""
Calculate the histogram of the selected image.
Args:
request: Request: The request object.
Returns:
TemplateResponse: The page with the histogram of the selected image.
"""
form = HistogramForm(request)
await form.load_data()
image_id = form.image_id
# Read image
im = cv2.imread('app/static/imagenet_subset/'+image_id)
# Calculate mean value from RGB channels and flatten to 1D array
vals = im.mean(axis=2).flatten()
# Calculate histogram
histogram, bins = np.histogram(vals, range(257))
return templates.TemplateResponse(
"histogram_output.html",
{
"request": request,
"image_id": image_id,
"histogram": json.dumps(histogram.tolist()),
},
)