--- 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.