AOSSIE-Org / AOSSIE-Org/PictoPy

YOLO and ObjectClassifier fail silently on invalid or unreadable images

Open
#710 0 comments 0 reactions 1 assignee Claimed by @arpittkhandelwal View on GitHub
backend bug
Dominant language
Python
Stars
283
Forks
679
Avg merge
7d 2h
Merged PRs (30d)
3

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### What happened?

## Bug Description
The YOLO and ObjectClassifier modules fail silently when an invalid or unreadable image file is provided.
When `cv2.imread()` returns `None`, the models previously returned empty results instead of raising a clear error.
This leads to confusing API behavior and makes debugging difficult.

---

## Expected Behavior
When an invalid or unreadable image is provided:
- The API should return a structured **400 Bad Request** error.
- The frontend should receive a meaningful error message.
- Backend logs should clearly record the failure.
- Internal detection resources should be safely released.

---

## Current Behavior
- `cv2.imread()` returns `None`
- No proper exception was raised earlier
- API returned empty results instead of an error
- Frontend receives misleading "no detections"
- Debugging becomes difficult

---

## Proposed Fix
- Raise `HTTPException(status_code=400)` when the image is invalid/unreadable.
- Sanitize error messages to avoid exposing server file paths.
- Add consistent validation logic across YOLO and ObjectClassifier.
- Add unit tests to verify correct error handling.

---

## Related Pull Request
Fix implemented in PR:
Fix: Standardize invalid image handling in YOLO and ObjectClassifier #679

---

## Testing
- Valid image → YOLO and ObjectClassifier behave normally
- Corrupted image → API returns 400 Bad Request
- Missing file → Proper HTTPException raised

---

## Labels
bug, backend, validation, tests, good first issue

### Record

- [x] I agree to follow this project's Code of Conduct

### Checklist before Submitting

- [ ] Have you updated docs for it?
- [ ] Have you added unit tests?
- [ ] Have you made sure unit tests pass?
- [x] Have you made sure code formatting is correct?
- [ ] Do Your changes passes all tests?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.