GDDO commited on
Commit
c1d22cb
路
verified 路
1 Parent(s): 535ced9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -21
app.py CHANGED
@@ -9,32 +9,24 @@ app = FastAPI()
9
  # Carregar o modelo TFLite
10
  interpreter = tf.lite.Interpreter(model_path="model_unquant.tflite")
11
  interpreter.allocate_tensors()
12
-
13
  input_details = interpreter.get_input_details()
14
  output_details = interpreter.get_output_details()
15
-
16
  classes = ['Bastonete', 'Bas贸filo']
17
 
 
18
  @app.post("/predict")
19
  async def predict(file: UploadFile = File(...)):
20
- try:
21
- # Processar a imagem sem redimensionar
22
- contents = await file.read()
23
- image = Image.open(BytesIO(contents)).convert("RGB")
24
- image_array = np.array(image).astype(np.float32) / 255.0
25
- image_array = np.expand_dims(image_array, axis=0)
26
 
27
- # Realizar a predi莽茫o
28
- interpreter.set_tensor(input_details[0]['index'], image_array)
29
- interpreter.invoke()
30
- output_data = interpreter.get_tensor(output_details[0]['index'])
31
- predicted_class_index = np.argmax(output_data)
32
- predicted_class_name = classes[predicted_class_index]
33
- predicted_confidence = output_data[0][predicted_class_index] * 100
34
 
35
- return {
36
- "class": predicted_class_name,
37
- "confidence": f"{predicted_confidence:.2f}%"
38
- }
39
- except Exception as e:
40
- return {"error": str(e)}
 
9
  # Carregar o modelo TFLite
10
  interpreter = tf.lite.Interpreter(model_path="model_unquant.tflite")
11
  interpreter.allocate_tensors()
 
12
  input_details = interpreter.get_input_details()
13
  output_details = interpreter.get_output_details()
 
14
  classes = ['Bastonete', 'Bas贸filo']
15
 
16
+
17
  @app.post("/predict")
18
  async def predict(file: UploadFile = File(...)):
19
+ contents = await file.read()
20
+ image = Image.open(BytesIO(contents)).convert("RGB")
21
+ image = image.resize((224, 224))
22
+ image_array = np.array(image).astype(np.float32) / 255.0
23
+ image_array = np.expand_dims(image_array, axis=0)
 
24
 
25
+ interpreter.set_tensor(input_details[0]['index'], image_array)
26
+ interpreter.invoke()
27
+ output_data = interpreter.get_tensor(output_details[0]['index'])
28
+ predicted_class_index = np.argmax(output_data)
29
+ predicted_class_name = classes[predicted_class_index]
30
+ predicted_confidence = output_data[0][predicted_class_index] * 100
 
31
 
32
+ return {"class": predicted_class_name, "confidence": f"{predicted_confidence:.2f}%"}