Skip to content
System design
Production experienceIntermediate20 Aug 2025

An OCR pipeline that never uploads the document

Med Pocket's architecture: document detection, on-device text recognition and rule based parsing, with only structured data crossing the network.

OCRPrivacyFlutterMongoDB

Med Pocket reads a printed prescription with the camera and creates medicine records from it. This is how the system is arranged and why the interesting decisions went the way they did.

Components

Three tiers, and one rule that shapes all of them: image work happens on the client, persistence happens on the server, and only structured data crosses between them.

Why the boundary sits there

The conventional split would put OCR on the server. Stronger models, no app release needed to improve accuracy, less work on the device.

The counterargument is the data. A prescription carries a person's name, their doctor and their conditions. Uploading it means storing it, securing it, retaining it under some policy and being accountable for it. Keeping it on the device means none of that exists as a problem.

The server receives a list of objects. It has no prescription to lose.

This is worth generalising. Privacy constraints usually simplify an architecture rather than complicating it. Deciding the image stays local removed an upload endpoint, a storage bucket, an encryption question and a retention policy in one decision.

Client pipeline

Detect and crop first. Running OCR on a raw camera frame gives poor results because the frame contains a desk, a hand and a document at an angle. Automatic document detection and cropping ahead of recognition improved output more than any later parser change. In an OCR pipeline the cheapest accuracy comes from the input.

Recognise on device. Google ML Kit text recognition runs locally and returns lines of raw text.

Parse with rules, not a model. Prescriptions in this market follow conventions: a type prefix such as TAB, CAP, INJ or DROP before the medicine name, and dosage written as an additive pattern like 1+1+0. Those two anchors are enough to structure the text.

A rule based parser is the right tool when the domain has a stable convention. It is faster, fully predictable, and debuggable by reading it. Reaching for a model here would add a dependency and lose determinism to solve a problem that was already solved.

Review before writing. The parsed list is shown for confirmation rather than saved directly. Any extraction pipeline has a failure mode, and in a health application the correct response to that is a review step, not confidence.

Server side

Express.js with Mongoose over MongoDB. Document storage suits the data: vitals, medicines and reports vary in shape between users, and health records are naturally document shaped.

Collections:

  • Users, with profile and authentication.
  • HealthRecords, one document per reading with a timestamp, so vitals are a time series and can be charted rather than only showing the latest value.
  • Medications, the structured output of the scan or manual entry, with type, name, dosage pattern and schedule.
  • Reports, metadata plus the S3 URL of the image.
  • Folders, user created categories such as Blood Test or ECG.

Images go to S3, URLs go to MongoDB. Report images are the one thing that does leave the device, because a user explicitly wants them stored and retrievable. Putting binary data in the database would work at this size and would be a bad habit to build.

JWT on every protected route. Issued at login, required on anything touching user data.

Trade-offs

On-device OCR is weaker than a cloud model. Accepted deliberately for this data class.

Improving the parser needs an app release. A server side pipeline could be fixed in a deploy. The privacy gain is worth the slower iteration.

Rules do not generalise. The parser encodes local prescription convention. A market that writes prescriptions differently needs different rules, and handwritten prescriptions need a different approach entirely. Both are honest limits rather than bugs.

What I would add next

Scheduled local notifications for medicine times. The schedule is already structured and sitting on the device, which is exactly where a reminder should be generated from, with no server and no push infrastructure involved.