← Nitish Jha / project demos

PROJECT / DOCUMENT CONSISTENCY & FACE COMPARISON

From document images
to a guided comparison.

My KYC project combines optical character recognition, document-field checks and face comparison in one guided workflow. Explore how it works, then run the application directly in your browser with the provided fictional sample.

Real OCR on imagesPAN / Aadhaar / bothCamera or selfie uploadServer-side CPU inference

TRY IT IN FOUR STEPS

A complete sample run.

The sample is a clearly marked fictional document with a public test portrait. It passes through the same OCR, field checks and face models as an uploaded image. There is no simulated approval.

  1. Open the demo and load the sampleSelect “Load sample document”. The application fills the fictional name, date and PAN-format number.
  2. Check the documentConfirm server processing, then select “Check document”. OCR reads the image and the detector checks its portrait.
  3. Choose the sample portraitOn the next screen, select “Use sample portrait”, then “Compare face”. For your own permitted images, use the camera or upload option.
  4. Review and clearRead the text checks and cosine score. “Clear & start again” removes the current session.

Personal educational demonstration. A matching result means that visible fields and portraits agree under the implemented rules. It does not authenticate a government document, check official records or establish liveness.

THE COMPLETE WORKFLOW

What happens at every step.

Document + details→OCR + exact checks→Portrait embedding→Selfie comparison→Result + reset
01

Choose the document scope

The user selects PAN, Aadhaar or both. Each selected document needs a front image, its number, a full name and a full date of birth. Both-document mode checks the same entered name and date against each document, then compares both portraits.

The project explores consistency across supplied evidence. There is no government verification API or connection to a customer account, payment system or identity registry.

02

Validate the entered fields

Names must contain letters and fit the 2–120-character limit. Dates must be valid calendar dates between 1 January 1900 and today. An individual PAN must contain five letters, four digits and a final letter, with P in the fourth position.

Aadhaar requires 12 digits, a first digit from 2 to 9 and a valid Verhoeff checksum. Whitespace and hyphens are removed from entered identifiers. These checks catch malformed input; a valid format or checksum does not establish that a number was issued.

03

Decode and prepare the images

Only JPEG and PNG images are accepted. Each must be smaller than 8 MB, at least 80 pixels on each side and no larger than 20 megapixels. The application checks the decoded format, applies EXIF orientation, converts to RGB, reduces large images to fit within 2,400 × 2,400 pixels and converts to OpenCV’s BGR representation.

This bounds the input size and gives the models a consistent representation. PDFs and HEIC files need conversion first. Small text, rotation, glare, blur and secondary portraits can prevent a successful check.

04

Read the document with OCR

RapidOCR runs text detection and recognition on the server through ONNX Runtime on the CPU. OCR models ship with its installed package. Recognised lines are retained only when their OCR confidence is at least 0.65.

OCR confidence supports a text reading; it does not measure document authenticity. An omitted or incorrectly read line can produce a mismatch. The application does not silently change an uncertain identifier or use fuzzy spelling to force a positive result.

05

Compare the visible text with the entered record

Name matching applies Unicode normalisation, case folding and removal of non-letter characters, then requires an exact normalised match to a recognised line. It strips common name labels and excludes common English relative-name labels. This reduces accidental matches to a parent or spouse, but is not a complete document-layout parser.

Full dates are parsed in day/month/year or year/month/day order using slashes, hyphens or dots. Common issue/expiry labels are excluded. A year-only birth date cannot satisfy an exact full-date check. PAN and Aadhaar numbers must match complete recognised identifiers; partial or masked numbers are insufficient.

Name, date of birth and document number must all match. A failed field stops the flow with a specific message. The checks do not validate security printing, issuer signatures, address ownership or document alteration.

06

Detect and align the document portrait

YuNet detects faces and facial landmarks. The image used for face processing is resized to a maximum side of 1,200 pixels. The detector uses a score threshold of 0.8 and requires exactly one face. A face box smaller than 40 pixels on either side is rejected.

The landmarks guide SFace’s alignment and cropping, reducing differences in position and orientation. Cards with secondary or ghost portraits may need careful cropping that retains the required text and primary portrait. The app does not select arbitrarily between multiple faces.

07

Create a face representation

SFace converts the aligned portrait to a numerical feature vector. The application checks that the vector contains finite values and has a non-zero norm, then normalises it. This representation supports pairwise comparison; the app does not search a database to identify a person.

In both-document mode, the document portraits must meet the similarity threshold before the selfie step. A pending session temporarily stores their vectors and successful field checks on the server, linked to a random flow token.

08

Capture or choose a selfie

The camera starts after the user selects “Enable camera” and grants permission. “Take photo” captures one frame. A JPEG/PNG upload is available when camera access is unavailable. The camera stream stops on leaving the page or completing the comparison.

The selfie passes through the same decoding, detection, alignment and embedding stages. A still image, including the sample portrait, can pass. Camera capture alone does not provide anti-spoofing or liveness detection.

09

Calculate similarity and show the result

Cosine similarity compares vector directions: similarity = (A · B) / (‖A‖ × ‖B‖). Values are bounded from −1 to 1, with higher values indicating more similar representations. The application uses 0.363 as a baseline threshold, following OpenCV’s SFace example.

The selfie must meet the threshold for every document. The result shows “Checks matched” or “Manual review needed”, plus each document’s text-check status and face similarity. A score of 0.8 is not “80% identity confidence”, and a low score is not evidence of fraud.

The server claims a pending session for one comparison at a time. A completed flow cannot be reused; invalid face-input retries are limited to five attempts. The result is read from server state rather than editable URL parameters.

10

Clear the temporary data

Uploaded images are processed in memory rather than saved to an upload directory. Temporary vectors and checks live in a bounded in-memory store: 15-minute expiry, up to 100 sessions. Expired entries are purged on the next request, and the oldest can be evicted at capacity.

After comparison, vectors are replaced by results and scores. “Clear & start again” removes the current flow immediately. Restarting clears the entire store. Closing a tab alone does not immediately remove server state; operating-system swap and crash dumps are outside the application’s memory-only behaviour.

THE MODEL STACK

Components with distinct jobs.

ComponentPurposeSetting in this demo
RapidOCR + ONNX RuntimeRead document textOCR confidence ≥ 0.65
Python field checksCompare name, date and numberExact normalised matching
YuNetDetect a single face and landmarksScore ≥ 0.8; face size ≥ 40 pixels
SFaceProduce aligned face embeddingsNormalised feature vectors
Cosine comparisonCompare portraits and selfieSimilarity ≥ 0.363

These are configuration values, not a measured accuracy claim for your users or documents. Face models are downloaded with SHA-256 verification. No external inference API, API key or GPU is required.

CODE STRUCTURE

How the application fits together.

FileResponsibility
kyc/documents.pyForm validation, normalisation, identifier checks and OCR matching.
kyc/vision.pyImage decoding, OCR, detection, alignment and vector comparison.
kyc/models.pyFace-model downloads and hash checks.
kyc/store.pyBounded, expiring, thread-protected session state.
kyc/web.pyDocument, selfie, result and reset routes, including CSRF protection.
templates/ and static/Forms, camera/upload controls, results and responsive styling.
manage.py and setup_local.pyInstall packages, check models and start the local server.

The portfolio mounts this app at /projects/kyc/demo/ with its own session cookie. Repository document/model logic runs directly. Integration adds navigation, sample controls and a comparison URL compatible with the portfolio path. Flask is aligned with the portfolio version.

The current repository replaces the older EasyOCR/TensorFlow/dlib prototype and its missing face helper. The earlier text-only adapter is no longer the main KYC demo. This explanation follows revision b0c2ccb.

Inspect the source revision ↗

SETUP & RUNNING

Ready in your browser.

Select Open runnable demo, load the fictional sample document, and complete the portrait comparison. No Python installation is needed to use this hosted demo. Images are uploaded over HTTPS and processed temporarily on the server; use the provided samples instead of real identity documents.

The instructions below are optional for running a separate copy on your own computer.

Run the repository by itself

From the kyc-project folder:

.\.venv\Scripts\python.exe manage.py run --port 5055

Open http://127.0.0.1:5055. Keep the terminal open; Ctrl+C stops the standalone server. This starts a separate local copy of the model workflow.

Set up a fresh copy

Use 64-bit Python 3.12; Python 3.10–3.12 are supported. From the downloaded portfolio folder:

py -3.12 setup_kyc.py
Start-Portfolio.cmd

Setup needs internet for packages and about 39 MB of face models. Afterwards, inference can run offline.

Original repository setup and diagnostics
git clone https://github.com/Nitish1206/KYC-System-for-Fraud-Prevention-and-Identity-Verification.git
cd KYC-System-for-Fraud-Prevention-and-Identity-Verification
py -3.12 setup_local.py
.\.venv\Scripts\python.exe manage.py run

The default standalone address is http://localhost:5000. Use manage.py check, manage.py self-test or manage.py download-models with the environment’s Python for diagnostics. Conda and Docker instructions are also in the repository.

TESTING & PRACTICAL LIMITS

What to check when trying the project.

A successful sample run exercises real OCR, matching, portrait detection, embeddings and final comparison. Change the name or date to explore mismatches. Try a face-free image to see the single-face requirement. Both-document mode requires matching text on each document and consistent portraits.

The automated tests cover valid/invalid fields, OCR matching, corrupt images, face-count handling, session expiry, CSRF, reset and result access. Real-model tests include synthetic text recognition and public-portrait comparisons. These verify functionality, not population-level accuracy, spoof resistance or ID authenticity.

What you seeWhat to do
Field could not be matchedCheck the entered value and image clarity. Full English names/dates and unmasked numbers are needed.
Exactly one clear face requiredRemove unrelated faces or secondary portraits while retaining required text.
Manual review neededThe face score did not meet the rule. Check image quality; it is not a fraud finding.
Camera unavailableUse selfie upload, or a camera-capable browser over HTTPS.
Session expiredStart a new check. Sessions last 15 minutes and are lost on restart.

Future work would require labelled evaluations across image conditions and user groups, calibrated thresholds, document-authenticity checks and a dedicated liveness design. Those capabilities are outside this educational demo.

The sample uses a public NASA portrait distributed with scikit-image as a test fixture. Its name, date and PAN-format identifier are fictional test values. Sample source ↗