Create optimizacion.py
Browse files- optimizacion.py +25 -0
optimizacion.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
|
| 3 |
+
# Reducir resoluci贸n de imagen antes de enviarla al modelo
|
| 4 |
+
def resize_imagen(img, size=(224,224)):
|
| 5 |
+
return img.resize(size)
|
| 6 |
+
|
| 7 |
+
# Funci贸n para preprocesar varias im谩genes en batch
|
| 8 |
+
def batch_preprocesar(imagenes, size=(224,224)):
|
| 9 |
+
return [img.resize(size) for img in imagenes]
|
| 10 |
+
|
| 11 |
+
# Limitar la carga del modelo solo una vez
|
| 12 |
+
def cargar_modelo_ligero():
|
| 13 |
+
from transformers import pipeline
|
| 14 |
+
# Modelo m谩s r谩pido y ligero que nateraw/food
|
| 15 |
+
modelo = pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 16 |
+
return modelo
|
| 17 |
+
|
| 18 |
+
# Funci贸n para cachear resultados y no recalcular lo mismo
|
| 19 |
+
_cache = {}
|
| 20 |
+
def cache_resultado(key, funcion, *args, **kwargs):
|
| 21 |
+
if key in _cache:
|
| 22 |
+
return _cache[key]
|
| 23 |
+
resultado = funcion(*args, **kwargs)
|
| 24 |
+
_cache[key] = resultado
|
| 25 |
+
return resultado
|