mateoluksenberg commited on
Commit
eea0f24
verified
1 Parent(s): 4b780d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -11
app.py CHANGED
@@ -3,37 +3,43 @@ from roboflow import Roboflow
3
  import shutil
4
  from pathlib import Path
5
 
6
- # Inicializar la aplicaci贸n FastAPI
7
  app = FastAPI()
8
 
9
- # Inicializar Roboflow
10
  rf = Roboflow(api_key="z15djNx8oHjsud3dWL4A")
11
  project = rf.workspace().project("stine")
12
  model = project.version(3).model
13
 
14
- # Carpeta temporal para guardar im谩genes cargadas
15
  UPLOAD_DIR = Path("uploads")
16
- UPLOAD_DIR.mkdir(exist_ok=True)
17
 
18
  @app.post("/predict/")
19
  async def predict_image(file: UploadFile = File(...)):
20
- """
21
- Endpoint para predecir una imagen usando el modelo de Roboflow.
22
- """
23
  # Verificar el tipo de archivo
24
  if file.content_type not in ["image/jpeg", "image/png"]:
25
  raise HTTPException(status_code=400, detail="El archivo debe ser una imagen (JPEG o PNG)")
26
 
27
  # Guardar el archivo temporalmente
28
  temp_file = UPLOAD_DIR / file.filename
29
- with temp_file.open("wb") as buffer:
30
- shutil.copyfileobj(file.file, buffer)
31
-
32
- # Realizar la predicci贸n
33
  try:
 
 
 
 
34
  prediction = model.predict(str(temp_file), confidence=40, overlap=30).json()
 
35
  except Exception as e:
 
 
 
36
  raise HTTPException(status_code=500, detail=f"Error al realizar la predicci贸n: {e}")
37
 
 
 
 
 
38
  # Devolver la predicci贸n como respuesta
39
  return prediction
 
3
  import shutil
4
  from pathlib import Path
5
 
6
+
7
  app = FastAPI()
8
 
9
+ # Inicializaci贸n de Roboflow
10
  rf = Roboflow(api_key="z15djNx8oHjsud3dWL4A")
11
  project = rf.workspace().project("stine")
12
  model = project.version(3).model
13
 
14
+ # Definir la carpeta temporal para guardar im谩genes
15
  UPLOAD_DIR = Path("uploads")
16
+ UPLOAD_DIR.mkdir(exist_ok=True) # Crear la carpeta si no existe
17
 
18
  @app.post("/predict/")
19
  async def predict_image(file: UploadFile = File(...)):
20
+
 
 
21
  # Verificar el tipo de archivo
22
  if file.content_type not in ["image/jpeg", "image/png"]:
23
  raise HTTPException(status_code=400, detail="El archivo debe ser una imagen (JPEG o PNG)")
24
 
25
  # Guardar el archivo temporalmente
26
  temp_file = UPLOAD_DIR / file.filename
 
 
 
 
27
  try:
28
+ with temp_file.open("wb") as buffer:
29
+ shutil.copyfileobj(file.file, buffer)
30
+
31
+ # Realizar la predicci贸n
32
  prediction = model.predict(str(temp_file), confidence=40, overlap=30).json()
33
+
34
  except Exception as e:
35
+ # Eliminar archivo temporal si algo sale mal
36
+ if temp_file.exists():
37
+ temp_file.unlink()
38
  raise HTTPException(status_code=500, detail=f"Error al realizar la predicci贸n: {e}")
39
 
40
+ # Limpiar archivo temporal despu茅s de procesar
41
+ if temp_file.exists():
42
+ temp_file.unlink()
43
+
44
  # Devolver la predicci贸n como respuesta
45
  return prediction