From ec64aca382c766f54e99b7486a8e90685ab5a2b6 Mon Sep 17 00:00:00 2001 From: Sachin Acharya <51048248+sachin-acharya-projects@users.noreply.github.com> Date: Thu, 18 Jul 2024 18:36:14 +0545 Subject: [PATCH] Added verbose error message for prediction failure & added exception handling for FileNotFound exception in predict method. --- Backend/main.py | 21 +++++++++--- Frontend/src/Components/UploadForm.tsx | 45 +++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/Backend/main.py b/Backend/main.py index 4acec20b..f4cfe298 100644 --- a/Backend/main.py +++ b/Backend/main.py @@ -22,13 +22,16 @@ def predict_image(image_src: str, confidence: int = 0.5) -> str | None: Returns: str | None: Classname of the predicted image. """ - results: Results = model(image_src) - + try: + results: Results = model(image_src) + except FileNotFoundError: + return + for result in results: names = result.names probs = result.probs.data.tolist() max_prob_index = np.argmax(probs) - + if probs[max_prob_index] < confidence: return None print(names[max_prob_index], type(names[max_prob_index])) @@ -67,7 +70,7 @@ def predict(): 400, ) - # print(file.mimetype) + print(file.mimetype) if file and "image" in file.mimetype.lower(): filename = secure_filename(file.filename) filepath = os.path.join(app.config["UPLOAD_FOLDER"], filename) @@ -92,7 +95,15 @@ def predict(): try: disease_data: Dict = json_data[disease_id] except KeyError: - return jsonify({"status": False, "message": "Disease Not Found in our database"}), 404 + return ( + jsonify( + { + "status": False, + "message": "Disease Not Found in our database", + } + ), + 404, + ) return jsonify({"status": True, "data": disease_data}), 200 return ( diff --git a/Frontend/src/Components/UploadForm.tsx b/Frontend/src/Components/UploadForm.tsx index 33e84db6..223e28ce 100644 --- a/Frontend/src/Components/UploadForm.tsx +++ b/Frontend/src/Components/UploadForm.tsx @@ -51,7 +51,44 @@ export default function UploadForm({ image, onPress }: Props) { "Cannot process your data due to some user-side errors." ) else if (statusCode === 422) - toast.error("Cannot predict the disease in the image.") + toast.error( + "Cannot predict the disease in the image. Please select another image.", + { + action: { + label: "Why?", + onClick: () => { + toast.error( +
+ Possible Reasons for Detection + Failure +
+