Skip to content

feat: add image quality validation before disease diagnosis#70

Open
stefrinpaul-curious wants to merge 3 commits into
jpdevhub:mainfrom
stefrinpaul-curious:feature/image-quality-validation
Open

feat: add image quality validation before disease diagnosis#70
stefrinpaul-curious wants to merge 3 commits into
jpdevhub:mainfrom
stefrinpaul-curious:feature/image-quality-validation

Conversation

@stefrinpaul-curious

@stefrinpaul-curious stefrinpaul-curious commented Jun 21, 2026

Copy link
Copy Markdown

Summary

Adds image quality validation before crop disease diagnosis to prevent low-quality images from being processed by the AI model. Images with insufficient resolution, excessive blur, or poor lighting are rejected with user-friendly feedback.

Related Issue

Closes #63

Changes

  • Added image_validation.py utility module
  • Implemented image resolution validation
  • Implemented blur detection using Laplacian variance
  • Implemented brightness validation
  • Added image quality scoring system
  • Integrated validation into the diagnosis pipeline before model inference
  • Added frontend handling for image quality errors
  • Hidden confidence score when image quality validation fails
  • Hidden WhatsApp sharing option for image quality failures
  • Improved UI messaging for image quality-related issues

Testing

  • Added/updated tests
  • Tested locally (describe steps)

Local Testing Steps

  1. Uploaded a clear crop image and verified normal disease diagnosis.
  2. Uploaded a blurry image and verified diagnosis was blocked.
  3. Uploaded a low-resolution image and verified validation error was displayed.
  4. Uploaded a poorly lit image and verified validation error was displayed.
  5. Verified confidence score is hidden for image quality failures.
  6. Verified WhatsApp sharing is hidden for image quality failures.
  7. Verified existing diagnosis functionality remains unchanged for valid images.

Checklist

  • Code follows the project's TypeScript / Python style conventions
  • No secrets or .env values are committed
  • CI passes

Summary by CodeRabbit

  • New Features
    • Added automated image quality validation (resolution, blur, brightness) to detect poor uploads before ML processing.
    • Updated the results UI with a dedicated “Image Quality Issue” state, including tailored messaging and symptom/action label changes.
  • Bug Fixes / Improvements
    • Improved handling for low-quality images with clearer “How to Fix” guidance and reduced result sections during quality errors.
    • Farm creation now rejects requests without an existing farmer profile (returns HTTP 400).
  • Style
    • Added new badge styling for quality-error state (and updated badge variants for consistent emphasis).

@vercel

vercel Bot commented Jun 21, 2026

Copy link
Copy Markdown

@stefrinpaul-curious is attempting to deploy a commit to the karan3431's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions

Copy link
Copy Markdown

🎉 Thanks for your contribution, @stefrinpaul-curious!

Please make sure CI passes and the checklist in the PR template is complete. A maintainer will review this soon.

— The AgroNavis team

@coderabbitai

coderabbitai Bot commented Jun 21, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: f1f4b9d2-67f6-46f9-a1c6-2a9f239bc294

📥 Commits

Reviewing files that changed from the base of the PR and between a5b715e and 5703dd8.

📒 Files selected for processing (3)
  • backend/requirements.txt
  • frontend/src/components/CropScanTab.tsx
  • frontend/src/styles/CropScan.module.css
🚧 Files skipped from review as they are similar to previous changes (1)
  • frontend/src/components/CropScanTab.tsx

📝 Walkthrough

Walkthrough

The PR adds backend image-quality validation before diagnosis, returns a quality-error response when validation fails, updates the frontend to render that state, and changes farm creation to require an existing farmer profile.

Changes

Image Quality Validation Pipeline

Layer / File(s) Summary
Validation module and dependency
backend/image_validation.py, backend/requirements.txt
Defines resolution, blur, and brightness thresholds; implements the validation checks, quality scoring, and top-level orchestrator; adds opencv-python-headless as a dependency.
Inference quality gate and response contract
backend/main.py
Imports the validator, extends PredictionResponse with is_quality_error, returns an early poor-quality prediction when validation fails, preserves the flag on the normal path, and adds GET /test123.
Frontend contract and quality-error UI
frontend/src/utils/cropScanApi.ts, frontend/src/styles/CropScan.module.css, frontend/src/components/CropScanTab.tsx
Adds the quality-error field to the API type, derives quality-error state in the scan tab, adds badge styles, and changes badge text, section titles, and conditional rendering for quality-error results.

Farm Creation Farmer Validation Fix

Layer / File(s) Summary
Farmer existence check and payload cleanup
backend/main.py
create_farm checks for a matching farmer record before insertion, filters out null payload values, and returns the inserted farm in the standard success wrapper.

Sequence Diagram(s)

sequenceDiagram
  participant User
  participant CropScanTab
  participant run_inference
  participant validate_image_quality
  participant ResNet

  User->>CropScanTab: Upload crop image
  CropScanTab->>run_inference: POST image
  run_inference->>validate_image_quality: validate(image)
  validate_image_quality-->>run_inference: {valid, quality_score, blur_score, brightness}
  alt image invalid
    run_inference-->>CropScanTab: PredictionResponse(is_quality_error=true)
    CropScanTab-->>User: Show quality badge and fix guidance
  else image valid
    run_inference->>ResNet: classify(image)
    ResNet-->>run_inference: prediction
    run_inference-->>CropScanTab: PredictionResponse(is_quality_error=false)
    CropScanTab-->>User: Show diagnosis result
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐇 I sniffed the leaf, then checked the light,
No blur too soft, no dark too bright.
If pixels wobble, I hop away,
“Try once more!” I softly say.
With badges orange, green, and red,
The garden sings above my head 🌿

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The farm creation logic and /test123 endpoint are unrelated to image quality validation. Remove or split the farm-flow and debug-endpoint changes into a separate PR focused on image quality validation.
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding image quality validation before diagnosis.
Linked Issues check ✅ Passed The PR adds resolution, blur, and brightness checks and integrates them before inference with user-facing quality-error handling.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@backend/image_validation.py`:
- Around line 1-2: The cv2 module imported in image_validation.py is not
declared as a runtime dependency in the requirements file. Add `opencv-python`
to the backend requirements file to ensure the dependency is installed when the
API starts. For headless deployments without GUI capabilities, use
`opencv-python-headless` instead, as it is lighter weight and sufficient for
image processing operations without display functionality.

In `@frontend/src/styles/CropScan.module.css`:
- Around line 232-254: Rename the CSS class selectors from camelCase to
kebab-case format: change .healthyBadge to .healthy-badge, .diseaseBadge to
.disease-badge, and .qualityBadge to .quality-badge. Add empty lines before each
rule definition to satisfy the rule-empty-line-before lint check. Update the
corresponding TypeScript module lookups in CropScanTab.tsx to reference the new
kebab-case class names instead of the camelCase versions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 48d3a352-4e7c-4324-b407-4c0b580ec3bc

📥 Commits

Reviewing files that changed from the base of the PR and between e220859 and a5b715e.

📒 Files selected for processing (5)
  • backend/image_validation.py
  • backend/main.py
  • frontend/src/components/CropScanTab.tsx
  • frontend/src/styles/CropScan.module.css
  • frontend/src/utils/cropScanApi.ts

Comment thread backend/image_validation.py
Comment thread frontend/src/styles/CropScan.module.css Outdated
Comment on lines +232 to +254
.healthyBadge {
background: #16a34a;
color: white;
padding: 8px 12px;
border-radius: 8px;
font-weight: 600;
}

.diseaseBadge {
background: #dc2626;
color: white;
padding: 8px 12px;
border-radius: 8px;
font-weight: 600;
}

.qualityBadge {
background: #ea580c;
color: white;
padding: 8px 12px;
border-radius: 8px;
font-weight: 600;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Fix stylelint violations for new badge selectors.

The new rules currently violate configured lint checks (rule-empty-line-before and selector-class-pattern expecting kebab-case). Please rename selectors to kebab-case and add the required spacing (and update TS module lookups accordingly).

💡 Example fix pattern
-.healthyBadge {
+.healthy-badge {
   ...
 }

-.diseaseBadge {
+.disease-badge {
   ...
 }

-.qualityBadge {
+.quality-badge {
   ...
 }

And in frontend/src/components/CropScanTab.tsx:

-? styles.qualityBadge
+? styles['quality-badge']
🧰 Tools
🪛 Stylelint (17.13.0)

[error] 232-238: Expected empty line before rule (rule-empty-line-before)

(rule-empty-line-before)


[error] 232-232: Expected class selector ".healthyBadge" to be kebab-case (selector-class-pattern)

(selector-class-pattern)


[error] 240-240: Expected class selector ".diseaseBadge" to be kebab-case (selector-class-pattern)

(selector-class-pattern)


[error] 248-248: Expected class selector ".qualityBadge" to be kebab-case (selector-class-pattern)

(selector-class-pattern)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@frontend/src/styles/CropScan.module.css` around lines 232 - 254, Rename the
CSS class selectors from camelCase to kebab-case format: change .healthyBadge to
.healthy-badge, .diseaseBadge to .disease-badge, and .qualityBadge to
.quality-badge. Add empty lines before each rule definition to satisfy the
rule-empty-line-before lint check. Update the corresponding TypeScript module
lookups in CropScanTab.tsx to reference the new kebab-case class names instead
of the camelCase versions.

Source: Linters/SAST tools

@stefrinpaul-curious stefrinpaul-curious left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have reviewed everything and it is completely fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Image Quality Validation Before Disease Diagnosis

1 participant