Daniel00611 commited on
Commit
82c435d
verified
1 Parent(s): 43162db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -16
app.py CHANGED
@@ -26,28 +26,34 @@ model_path = hf_hub_download(repo_id="Daniel00611/InceptionV3_72", filename="Inc
26
  model = load_model(model_path)
27
 
28
  def preprocess_image(image_file, target_size=(299, 299)):
29
- img = image.load_img(image_file, target_size=target_size)
30
- img_array = image.img_to_array(img)
31
- img_array = np.expand_dims(img_array, axis=0)
32
- img_array = preprocess_input(img_array)
 
 
33
  return img_array
34
-
35
  @app.post("/predict/")
36
  async def predict(file: UploadFile = File(...)):
 
37
  # Procesar la imagen
38
- img_array = preprocess_image(file.file)
 
 
 
 
 
 
 
 
39
 
40
- # Realizar la predicci贸n
41
- predictions = model.predict(img_array)[0]
 
42
 
43
- # Obtener el top 10 de predicciones
44
- top_10_indices = predictions.argsort()[-10:][::-1]
45
- top_10_classes = [class_names[i] for i in top_10_indices]
46
- top_10_probabilities = predictions[top_10_indices]
47
-
48
- # Formar la respuesta en formato JSON
49
- result = [{"class": top_10_classes[i], "probability": float(top_10_probabilities[i])} for i in range(10)]
50
- return {"predictions": result}
51
 
52
  @app.get("/")
53
  def greet_json():
 
26
  model = load_model(model_path)
27
 
28
  def preprocess_image(image_file, target_size=(299, 299)):
29
+ # Convertir el archivo a BytesIO
30
+ img_bytes = image_file.read() # Leer el archivo como bytes
31
+ img = Image.open(io.BytesIO(img_bytes)) # Abrir la imagen con PIL
32
+ img = img.resize(target_size) # Redimensionar la imagen al tama帽o objetivo
33
+ img_array = np.array(img) # Convertir la imagen a un array de NumPy
34
+ img_array = np.expand_dims(img_array, axis=0) # A帽adir dimensi贸n extra para lote
35
  return img_array
36
+
37
  @app.post("/predict/")
38
  async def predict(file: UploadFile = File(...)):
39
+ try:
40
  # Procesar la imagen
41
+ img_array = preprocess_image(file.file)
42
+
43
+ # Realizar la predicci贸n
44
+ predictions = model.predict(img_array)[0]
45
+
46
+ # Obtener el top 10 de predicciones
47
+ top_10_indices = predictions.argsort()[-10:][::-1]
48
+ top_10_classes = [class_names[i] for i in top_10_indices]
49
+ top_10_probabilities = predictions[top_10_indices]
50
 
51
+ # Formar la respuesta en formato JSON
52
+ result = [{"class": top_10_classes[i], "probability": float(top_10_probabilities[i])} for i in range(10)]
53
+ return {"predictions": result}
54
 
55
+ except Exception as e:
56
+ return {"error": str(e)}
 
 
 
 
 
 
57
 
58
  @app.get("/")
59
  def greet_json():