Skip to content
All case studies
15 Aug 2025

Reading a prescription with a camera and a regular expression

How Med Pocket turns a photographed prescription into structured medicine records on the device, and why nothing about that pipeline is sent to a server.

Role
Engineer, two person team
Timeline
2025
Stack
Flutter, Google ML Kit, Express.js, MongoDB, AWS S3

Context

Med Pocket is a personal health record application: vitals, medicines and test reports. It was my final year project at Northern University Bangladesh, built with one project partner and submitted in August 2025.

The problem it exists to solve is not storage. It is the form.

The real problem is data entry

A person leaves a doctor's office with a prescription listing six medicines, each with a type, a name and a dosage pattern. To use any medicine tracking app, they must now type all of that in.

They will not. That is the honest assessment, and every abandoned health app confirms it. The information already exists in structured form on the paper in their hand. The app's job is to read it, not to ask for it again.

Architecture

Three layers: a Flutter client that does all image work locally, an Express and Mongoose API that handles persistence and business rules, and MongoDB for storage, with report images in S3 and only their URLs in the database.

The important line in that diagram is the box boundary. Everything about the prescription happens inside the device.

Why the pipeline stays on the device

The straightforward implementation sends the photo to a server and runs OCR there. Better models are available, it is easier to update, and you can improve accuracy without shipping an app release.

I did not do that, for one reason that outweighs all three: a prescription is a medical record with a person's name on it. Sending it to a server means storing it, backing it up, securing it, and answering for it if it leaks. Not sending it means none of those problems exist.

The server receives [{ type: "TAB", name: "...", dosage: "1+1+0" }]. It never sees a prescription.

Trade off: on-device OCR is weaker than a cloud model, and improving the parser needs an app release rather than a server deploy. For this data, that is the right side of the trade.

Cropping before OCR, not after

The first working version pointed OCR at the raw camera image and the results were poor. The reason was obvious in hindsight: the photo contained a desk, a hand, the edge of a notebook and a prescription at an angle.

Adding automatic document detection and cropping ahead of OCR did more for accuracy than any change to the parsing that came after it. The cheapest accuracy win in an OCR pipeline is usually the input, not the model.

Parsing unstructured text into medicines

OCR returns a stream of lines with no structure. Sometimes the medicine name and its dosage are on the same line, sometimes they are separated, spacing varies, and other document text is mixed in: the clinic name, the patient's name, advice.

Bangladeshi prescriptions have two conventions that make this tractable.

Medicines are prefixed by type. TAB, CAP, INJ, DROP. That prefix is the anchor. Find it and the name follows.

Dosage is written as an additive pattern. 1+1+0 means one in the morning, one in the afternoon, none in the evening. It is unmistakable and almost never appears elsewhere in the document.

So the parser walks the lines and applies two patterns in sequence:

code
type prefix pattern   ->  TAB | CAP | INJ | DROP, then capture the name that follows
dosage pattern        ->  digit + digit + digit

then associates each dosage with the most recent unmatched medicine, which handles the common case where the two sit on different lines.

This is not machine learning and it does not need to be. The domain has a convention, the convention is stable, and a regular expression that encodes it is faster, entirely predictable and debuggable by reading it. When a rule based approach fits the domain, reaching for a model is a decision that costs accuracy and adds a dependency.

Where it breaks: a prescription that does not follow the convention. That is a real limit and the app is honest about it, showing the parsed result for review before saving rather than writing straight to the database.

What I would not claim

It does not read handwriting. The parser and the OCR are both tuned for printed and computer generated prescriptions. Handwritten ones are a genuinely different problem, needing a model trained for it, and no amount of regex closes that gap. Saying otherwise in an interview would take about one follow up question to unravel.

There are no reminders yet. The app shows today's medicines in order, morning through evening, and lets a user mark them taken. It does not notify anyone at the right time. That is the single most valuable next feature and it is future work, not shipped work.

Rest of the system

Vitals are time series per user, stored with timestamps so the dashboard can chart them rather than only show the latest reading. A single current value is a number. A line is information.

Reports are filed into user created folders such as Blood Test or ECG, with the image in S3 and metadata in MongoDB. Storing images as documents in the database would have worked at project scale and would have been the wrong habit.

Authentication is JWT, with the token issued on login and required on every route that touches user data.

What I took from it

Two things, and neither is about OCR.

The first is that the feature people remember is usually the one that removes work, not the one that adds capability. Vitals tracking and report storage are the substance of the app. The scanner is what makes someone show it to a friend.

The second is that privacy constraints simplify architecture more often than they complicate it. Deciding the image never leaves the device removed an upload path, a storage bucket, a retention policy and a category of risk, all at once.