Skip to content

Commit

Permalink
Added verbose error message for prediction failure & added exception …
Browse files Browse the repository at this point in the history
…handling for FileNotFound exception in predict method.
  • Loading branch information
sachin-acharya-projects committed Jul 18, 2024
1 parent 4116d69 commit ec64aca
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
21 changes: 16 additions & 5 deletions Backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down Expand Up @@ -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)
Expand All @@ -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 (
Expand Down
45 changes: 41 additions & 4 deletions Frontend/src/Components/UploadForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<div>
<p className="font-semibold text-[16px] mb-2">
Possible Reasons for Detection
Failure
</p>
<ol className="list-decimal list-inside pl-5 pr-5 flex flex-col gap-2">
<li>
The image is <b>unclear</b>,
making it difficult to
accurately identify the
disease.
</li>
<li>
The image <b>size</b> is too{" "}
<b>broad</b>. Please take a{" "}
<b>close-up</b> picture of
the <b>plant leaf</b> for
better results.
</li>
<li>
The disease is{" "}
<b>not registered</b> in our
database.
</li>
</ol>
</div>
)
},
},
}
)
} else if (error instanceof Error) console.error(error.message)
else console.error(error)
},
Expand All @@ -62,10 +99,10 @@ export default function UploadForm({ image, onPress }: Props) {
uploadImage = useCallback(
(data: typeof image) => {
if (!data || !data.data) return

setDiseaseState(null)
toast.info("Your request is being processed. Please wait a moment.")

const formData = new FormData()
const file = prepareFileForUpload(data.data, data.name)
formData.append("image", file)
Expand All @@ -74,7 +111,7 @@ export default function UploadForm({ image, onPress }: Props) {
.then((data) => {
const result = data.data
if (!result.status) throw new Error(result.message)
toast.success("You request has been processed.")
toast.success("You request has been processed.")

const descriptions: {
header: string
Expand Down

0 comments on commit ec64aca

Please sign in to comment.