Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
image_array = np.expand_dims(image_array, axis=0)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
predicted_confidence = output_data[0][predicted_class_index] * 100
|
| 34 |
|
| 35 |
-
|
| 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}%"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|