Spaces:
Sleeping
Sleeping
| title: Pill Identification System | |
| emoji: π | |
| colorFrom: green | |
| colorTo: blue | |
| sdk: docker | |
| app_port: 7860 | |
| pinned: false | |
| # Pill Identification System (OCR + HSV Color Recognition) | |
| A computer-vision pipeline that identifies medical pills from a single photo by | |
| combining **YOLO pill detection**, **multi-angle OCR imprint reading**, an | |
| **HSV-based color-recognition system**, and **shape analysis**, then matches the | |
| result against a drug database and returns the most likely medications with their | |
| indications, warnings, and side effects. | |
| Built as a Flask web service with a browser front-end for live capture and lookup. | |
| --- | |
| ## Highlights | |
| - **YOLOv8 pill detection** β locates and crops the pill, with a low-confidence | |
| retry pass so faint or low-contrast pills are still found. | |
| - **Multi-angle OCR** β the imprint is read at 8 rotations (0β315Β°) through | |
| OpenOCR, and the best result is selected by a combined length Γ confidence score. | |
| - **HSV-based color recognition** β a robust, contour-aware color extractor that | |
| is resilient to lighting, shadows, specular highlights, and imprint text. | |
| See [below](#hsv-based-color-recognition). | |
| - **Shape classification** β circle / ellipse / other via ellipse-fit ratios with | |
| shadow-corrected segmentation. | |
| - **Top-N database matching** β OCR text is matched front/back against the drug | |
| database with an LCS-based scorer, returning ranked candidates (with a | |
| low-confidence fallback and a "retake the photo" path for unusable images). | |
| --- | |
| ## System Architecture | |
| ``` | |
| photo (base64 / upload) | |
| β | |
| βΌ | |
| βββββββββββββββββββββββββ | |
| β YOLOv8 pill detection β conf 0.25 β retry 0.10 | |
| βββββββββββββ¬ββββββββββββ | |
| β cropped pill | |
| βββββββββββ΄ββββββββββββββββββββββββββββ | |
| βΌ βΌ | |
| βββββββββββββββββββββββ βββββββββββββββββββββββββββ | |
| β Multi-angle OCR β β HSV color recognition β | |
| β (8 rotations, β β + shape classification β | |
| β OpenOCR, best pick)β β (contour-aware, robust) β | |
| ββββββββββββ¬βββββββββββ ββββββββββββββ¬βββββββββββββ | |
| β imprint text β color, shape | |
| βββββββββββββββββ¬βββββββββββββββββββββββ | |
| βΌ | |
| βββββββββββββββββββββββββββ | |
| β Top-N front/back match β LCS scorer + color/shape filter | |
| β against drug database β | |
| ββββββββββββββ¬βββββββββββββ | |
| βΌ | |
| ranked medications + drug info | |
| ``` | |
| The pipeline lives in [`app/utils/pill_detection.py`](app/utils/pill_detection.py) | |
| (`process_image`), with HTTP routing in [`app/route.py`](app/route.py). | |
| --- | |
| ## HSV-based Color Recognition | |
| The color module ([`app/utils/shape_color_utils.py`](app/utils/shape_color_utils.py)) | |
| uses a robust HSV pipeline rather than a naΓ―ve KMeans/RGB average: | |
| 1. **Contour-aware masking** β analyze only pill pixels, not the background. | |
| 2. **Noise rejection** β drop the darkest 15% (shadow), specular highlights, | |
| a 5% border band, and dilated imprint pixels. | |
| 3. **Robust statistics** β adaptive hue histogram + IQR-trimmed medians for | |
| S and V, so a few stray pixels can't skew the result. | |
| 4. **Semantic classification** β `classify_hsv_to_semantic_color()` maps HSV to | |
| human color labels with lighting-tuned thresholds, plus a color-tolerance | |
| table for fuzzy matching (`is_color_match_multi`, `get_color_tolerance`). | |
| Key functions: `extract_pill_colors_hsv`, `classify_hsv_to_semantic_color`, | |
| `detect_shape_and_extract_colors`, `score_candidate`. | |
| --- | |
| ## Tech Stack | |
| Python Β· Flask Β· OpenCV Β· NumPy Β· Ultralytics YOLOv8 Β· OpenOCR (ONNX Runtime) Β· | |
| PyTorch Β· scikit-learn Β· Pandas | |
| --- | |
| ## Project Structure | |
| ``` | |
| . | |
| βββ app/ | |
| β βββ route.py # Flask routes (/upload, /match, /api/*) | |
| β βββ utils/ | |
| β β βββ pill_detection.py # YOLO detect β OCR β color/shape pipeline | |
| β β βββ shape_color_utils.py # HSV color recognition + shape detection | |
| β β βββ matcher.py # LCS-based top-N OCR matching | |
| β β βββ ocr_utils.py # OpenOCR wrapper | |
| β β βββ data_loader.py # drug database loading | |
| β β βββ image_io.py # safe image reading (HEIC, etc.) | |
| β βββ templates/ static/ # web front-end | |
| β βββ __init__.py # create_app() | |
| βββ data/ # drug database (TESTData.xlsx) + pictures/ | |
| βββ models/ # best.pt (YOLO) + OCR onnx (auto-downloaded) | |
| βββ main.py # app entry point | |
| βββ setup_models.py # downloads OCR models | |
| βββ requirements.txt | |
| ``` | |
| --- | |
| ## Getting Started | |
| ```bash | |
| # 1. Install dependencies | |
| pip install -r requirements.txt | |
| # 2. Download the OCR models (best.pt YOLO weights are already included) | |
| python setup_models.py | |
| # 3. Run | |
| python main.py | |
| # server starts on http://localhost:10000 | |
| ``` | |
| ### API | |
| | Endpoint | Method | Purpose | | |
| | --- | --- | --- | | |
| | `/` | GET | Web UI | | |
| | `/upload` | POST | Detect + OCR + color/shape from a base64 image | | |
| | `/match` | POST | Match `{texts, colors, shape}` against the drug DB | | |
| | `/api/status` | GET | Health / data-loaded status | | |
| --- | |
| ## Notes on Data & Models | |
| - **`models/best.pt`** (YOLO detector) ships with the repo. | |
| - **OCR models** are excluded from git and fetched by `setup_models.py`. | |
| - **`data/pictures/`** (the ~534 MB drug image database) is git-ignored to keep the | |
| repo lean β remove that line from `.gitignore` if you want to publish it, or drop | |
| your own images there. | |