Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ•Œ Scan4Halal β€” AI-Powered Halal Ingredient Verification System

An intelligent full-stack web application that analyzes food product ingredients and classifies them as Halal, Haram, or Mushbooh (Doubtful) using a hybrid pipeline of OCR, semantic similarity, and Retrieval-Augmented Generation (RAG).


πŸš€ Overview

Scan4Halal bridges the gap between raw ingredient labels and consumer understanding by combining:

  • πŸ“Έ OCR-based text extraction
  • 🧠 Semantic embedding + similarity matching
  • πŸ“š Curated halal knowledge base
  • πŸ€– LLM-powered contextual explanations

The system enables users to scan, analyze, understand, and store ingredient insights in a seamless workflow.


πŸ—οΈ System Architecture

πŸ”Ή High-Level Workflow

Image Upload
   ↓
OCR (PyTesseract)
   ↓
Text Preprocessing
   ↓
Ingredient Parsing
   ↓
Multi-Stage Matching Pipeline
   ↓
Semantic Similarity (Embeddings)
   ↓
Halal Classification
   ↓
RAG Explanation (LLM)
   ↓
Frontend Display + User Actions

πŸŽ₯ Demo App Demo


πŸ”Ή Layered Architecture

1. Frontend Layer

  • React + DaisyUI SPA

  • Handles:

    • Image upload
    • Results visualization
    • Chat interface
    • Bookmarking & history

2. API Gateway Layer (Flask)

  • Centralized request handling

  • JWT-based authentication

  • Routes:

    • OCR processing
    • Ingredient matching
    • RAG explanation
    • User operations

3. Core Processing Services

πŸ” OCR Service
  • Extracts raw text using PyTesseract
🧹 Text Preprocessing
  • Regex + string normalization

  • Removes noise like:

    • "Ingredients:"
    • Special characters
    • Formatting inconsistencies
βš™οΈ Ingredient Matching Pipeline (Core Innovation)

A 3-stage hybrid matching system:

  1. Exact Matching

    • Direct string comparison
  2. Fuzzy Matching

    • Handles typos using Levenshtein distance
  3. Semantic Similarity Engine

    • Uses embeddings for contextual understanding

    • Matches:

      • Synonyms
      • E-numbers
      • Misspellings

4. AI Layer

🧠 Embedding Model
  • SentenceTransformers (all-MiniLM-L6-v2)
  • Fine-tuned on ingredient variants
  • Generates 384-dimensional vectors
πŸ“Š Similarity Search
  • Cosine similarity using NumPy
  • Finds closest ingredient match in database
πŸ€– RAG Explanation Engine
  • Powered by Mistral 7B (via Hugging Face)

  • Combines:

    • Retrieved ingredient context
    • LLM generation
  • Produces human-readable explanations


5. Database Layer (MongoDB)

Collections:

  • ingredients β†’ canonical data + embeddings
  • users β†’ authentication + profiles
  • scans β†’ history
  • bookmarks β†’ saved ingredients
  • submissions β†’ unknown ingredient requests

🧠 Core Innovation

πŸ”₯ Semantic Similarity Engine

Traditional systems rely on exact matching. Scan4Halal introduces embedding-based semantic matching:

  • Handles:

    • β€œE471” ↔ β€œMono- and Diglycerides”
    • Misspellings
    • Variants across regions
  • Enables:

    • High accuracy with small datasets (~100+ ingredients)
    • Reduced dependency on strict naming conventions

πŸ§ͺ Model Training & Fine-Tuning

  • Base Model: all-MiniLM-L6-v2
  • Training Platform: Google Colab + Hugging Face

Dataset Preparation

  • Generated 700,000+ pairs
  • Balanced to 8,000+ high-quality pairs

Training Strategy

  • Positive pairs β†’ synonym matches
  • Negative pairs β†’ unrelated ingredients

Outcome

  • Improved recognition of:

    • Variants
    • Abbreviations
    • E-number mappings

πŸ“Š Ingredient Classification Logic

Each ingredient is classified into:

Status Meaning
βœ… Halal Permissible
❌ Haram Forbidden
⚠️ Mushbooh Doubtful / unclear

πŸ“¦ Ingredient Categorization System

Custom taxonomy:

  • ENA-1000+ β†’ E-Numbers
  • NOI-2000+ β†’ Natural Ingredients
  • NUS-3000+ β†’ Nutrients
  • PAM-4000+ β†’ Additives

Each entry includes:

  • Synonyms
  • Halal status
  • Source
  • Processing info
  • Usage context

πŸ› οΈ Tech Stack

Frontend

  • React
  • Tailwind CSS + DaisyUI
  • Context API

Backend

  • Flask
  • Flask-JWT-Extended
  • PyMongo

AI / ML

  • PyTesseract (OCR)
  • Sentence Transformers
  • NumPy (cosine similarity)
  • Mistral 7B (RAG)

Database

  • MongoDB Atlas

Dev Tools

  • VS Code
  • Postman
  • Git & GitHub
  • Google Colab

βš™οΈ Setup Instructions

πŸ”Ή Backend

cd scan4halal-backend
python -m venv venv
venv\Scripts\activate
pip install -r requirements.txt

Create .env:

MONGO_URI=your_mongodb_uri
JWT_SECRET_KEY=your_secret

Run:

python app.py

πŸ”Ή Frontend

cd scan4halal-frontend
npm install
npm start

🌐 Key API Endpoints

Method Endpoint Description
POST /ocr Extract text
POST /match Ingredient classification
POST /rag/explain AI explanation
POST /rag/chat Chat interaction
POST /bookmark Save ingredient
GET /bookmarks Retrieve bookmarks

🎯 Use Cases

  • Muslim consumers verifying food products
  • Educational tool for ingredient awareness
  • Smart retail or scanning applications

⚠️ Limitations

  • Limited Training Data The semantic embedding model was trained on a relatively small and curated dataset of ingredients and their variants. As a result, the system may struggle when processing real-world product labels that contain unfamiliar, rare, or untrained ingredients. This can lead to a higher number of ingredients being classified as unknown or requiring further verification.

  • Surface-Level Halal Verification The system performs analysis primarily at the ingredient level based on available label information. However, true halal verification often depends on deeper factors such as:

    • Source of ingredients (e.g., animal-derived vs plant-based)
    • Processing methods
    • Cross-contamination during manufacturing
    • Certification standards

Since such information is typically not disclosed on product labels, the system cannot provide definitive halal certification, but rather serves as an informational and decision-support tool.


πŸ” Enhancements to Address Current Limitations

πŸ“š Expand and Continuously Update Ingredient Dataset Increase the size and diversity of the training dataset to better handle real-world ingredient variations and reduce the number of unknown classifications.

🧠 Adaptive Learning / Feedback Loop Incorporate user-submitted ingredients and feedback into the system to continuously improve model accuracy and database coverage. 🏭 Deeper Ingredient Context Awareness Enhance the system to consider additional factors such as ingredient sources, processing methods, and certification data where available, moving beyond surface-level label analysis.

🏷️ Positioning as a General Ingredient Awareness Tool Extend the application beyond halal verification to serve as an ingredient awareness platform for all consumers, providing insights into:

  • Additives and preservatives
  • Nutritional and health implications
  • Potential allergens or dietary concerns

This broadens the usability of the system and makes it valuable for a wider audience focused on informed food choices and transparency.

πŸ“ˆ Future Improvements

  • πŸ“± Mobile app version
  • πŸ“· Barcode scanning
  • 🌍 Multi-language support
  • ⚑ Vector database integration (Pinecone / Weaviate)
  • ☁️ External model hosting

πŸ“… Project Details

  • Type: Final Year Dissertation Project
  • Duration: June 2025 – Sep 2025
  • Affiliation: Cardiff Metropolitan University

πŸ‘©β€πŸ’» Author

Mazeena Cader

Software Engineering Graduate | MERN Stack Developer | ML Enthusiast

πŸ”— Portfolio: https://mazeenacader.netlify.app


πŸ“„ License

This project is developed for academic and educational purposes.


⭐ If you found this useful

Give this project a ⭐ on GitHub!


About

A smart AI-powered web application that allows users to scan product labels and verify the Halal status of ingredients using OCR, semantic search, and explanation generation

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages