-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
75 lines (53 loc) · 1.86 KB
/
app.py
File metadata and controls
75 lines (53 loc) · 1.86 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
from flask import Flask,render_template,request,send_file
import matplotlib.pyplot as plt
import numpy as np
from main import *
from io import BytesIO
app=Flask(__name__)
@app.route('/home')
def home():
return render_template(
'index.html',
title="Home",
)
from PIL import Image
@app.route('/home1',methods=['POST'])
def home1():
file=request.files['image']
original_img = plt.imread(file) / 255.0 # Normalize to [0, 1]
X_img = np.reshape(original_img, (original_img.shape[0] * original_img.shape[1], 3))
K = 16
max_iters = 10
initial_centroids = kMeans_init_centroids(X_img, K)
print("Initial centroids:", initial_centroids)
centroids, idx = run_kMeans(X_img, initial_centroids, max_iters)
print("Centroids after K-means:", centroids)
idx = find_closest_centroids(X_img, centroids)
X_recovered = centroids[idx, :]
X_recovered = np.reshape(X_recovered, original_img.shape)
print("X_recovered shape:", X_recovered.shape)
#X_recovered = np.clip(X_recovered, 0, 1)
print("X_recovered min:", X_recovered.min(), "max:", X_recovered.max())
compressed_image = Image.fromarray((X_recovered * 255).astype(np.uint8),'RGB')
# Save the figure to a BytesIO object
output = BytesIO()
compressed_image.save(output, format='JPEG')
#fig.savefig(output, format='JPEG')
#plt.close(fig)
output.seek(0)
return send_file(output, mimetype='image/jpeg',as_attachment=True,
download_name="compressed_image.jpg")
@app.route('/')
def start():
return render_template(
'start.html',
title="Start",
)
@app.route('/about')
def aboutus():
return render_template(
'about.html',
title="What we do"
)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000, debug=True)