-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
110 lines (82 loc) · 3.23 KB
/
app.py
File metadata and controls
110 lines (82 loc) · 3.23 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
import tensorflow as tf
tf.config.set_visible_devices([], 'GPU')
import io
import zipfile
from flask import Flask, request, jsonify, render_template, send_file
import os
import tensorflow as tf
from werkzeug.utils import secure_filename
from tensorflow.keras.preprocessing import image
import numpy as np
app = Flask(__name__)
creditos_iniciales = 200
# Configurar la carpeta de plantillas
app.template_folder = 'templates'
# Cargar el modelo
modelo = tf.keras.models.load_model(r"C:\Users\Gonzalo\Desktop\Bootcamp\proyecto-final-gcb-hs\Clasificador\fine.h5")
# Configuración para la carga de archivos
UPLOAD_FOLDER = 'uploads'
TEMP_FOLDER = 'temp_uploads'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
if not os.path.exists(TEMP_FOLDER):
os.makedirs(TEMP_FOLDER)
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def cargar_y_preparar_imagen(filename):
img = image.load_img(filename, target_size=(224, 224))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array /= 255.0
return img_array
@app.route('/')
def main():
return render_template('principal.html')
@app.route('/prueba')
def index():
return render_template('prueba.html')
@app.route('/precios')
def precios():
return render_template('precios.html')
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return jsonify({'error': 'No file part'})
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'})
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
temp_path = os.path.join(TEMP_FOLDER, filename)
file.save(temp_path)
img_array = cargar_y_preparar_imagen(temp_path)
prediction = modelo.predict(img_array)
categorias = ['animales', 'ciudades', 'personas', 'ninguna']
etiquetas_predichas = [categorias[i] for i, val in enumerate(prediction[0]) if val > 0.5]
return jsonify({'temp_path': temp_path, 'prediction': etiquetas_predichas})
return jsonify({'error': 'Invalid file type'})
from flask import send_file
from flask import Response
@app.route('/download', methods=['POST'])
def download_images():
data = request.get_json()
images = data['images']
# Crear un buffer en memoria para el archivo ZIP
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
for image_data in images:
temp_path = image_data['temp_path']
prediction = '_'.join(image_data['prediction'])
if os.path.exists(temp_path):
zip_file.write(temp_path, os.path.join(prediction, os.path.basename(temp_path)))
zip_buffer.seek(0)
# Configurar los encabezados para la descarga del archivo
headers = {
'Content-Disposition': 'attachment; filename=images.zip',
'Content-Type': 'application/zip'
}
return Response(zip_buffer.getvalue(), mimetype='application/zip', headers=headers)
if __name__ == '__main__':
app.run(debug=True)