This repository presents a comprehensive Python script designed to automate the processes of expiry date verification and fresh product identification using image analysis. It leverages the capabilities of OpenCV for image processing, Tesseract OCR for text extraction, and advanced techniques for date and color analysis to determine if a product is expired or fresh.
This code is only valid for VS Code and PyCharm.
The code has two parts: one for expiry date verification and the other for fresh food detection.
-
If you use the expiry date verification, click on the 'Run' button, and it will create an external window where you can upload an image of an expiry date. The code will detect the expiry date from the text, print the value, and indicate whether the item has expired or not.
-
If you use the fresh food detection, running the code will create an external window where you can upload an image of either fresh or rotten food. The code will detect if the food is fresh or rotten and show the percentage of how much it is rotten.
- Image Processing: Employs OpenCV to preprocess images, enhance contrast, and extract relevant features for analysis.
- Text Extraction: Utilizes Tesseract OCR to accurately extract text from images, including expiry dates and product information.
- Date Verification: Implements robust algorithms to parse and validate extracted expiry dates, ensuring accuracy and reliability.
- Freshness Assessment: Analyzes image features, such as color and texture, to assess the freshness of products.
- User-Friendly Interface: Provides a simple and intuitive interface for users to select images and view the results.
OPTION 1 : For Expiry Date Verification
OPTION 2 : For Fresh Produce Inspection
Here is the link to the explanation ppt. Click here
Here is the link to the video solution. Click here
To download and set up Tesseract OCR, follow these steps:
Step 1: Download Tesseract OCR
-
Go to the official Tesseract GitHub page: Tesseract at GitHub.
-
For Windows users, you can directly download the installer from UB Mannheim Tesseract Repository:
- Choose the latest version of the Windows installer (usually it will be an
.exefile). - Download the file and run the installer.
- Choose the latest version of the Windows installer (usually it will be an
Step 2: Install Python Libraries
To use Tesseract with Python, you will need to install the pytesseract library:
pip install pytesseract
Step 3: Configure Tesseract in Python (Windows)
Once Tesseract is installed, you will need to point Python to the tesseract.exe file. Here’s how to do that:
-
Find the Tesseract Executable:
- By default, Tesseract is installed in:
C:\Program Files\Tesseract-OCR\tesseract.exe.
- By default, Tesseract is installed in:
-
Configure the Path in Python: Add this line to your Python code to configure the path to the Tesseract executable:
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
Step 4 : Test the Setup
You can use the following code to test if Tesseract is working:
from PIL import Image
import pytesseract
# Set the path to the Tesseract executable (for Windows)
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
# Open an image
image = Image.open('sample_image.png')
# Perform OCR on the image
text = pytesseract.image_to_string(image)
print("Detected Text:")
print(text)
Step 5 : Add Tesseract to PATH (Optional)
You can add the Tesseract folder to your system's PATH environment variable so that you don't need to specify the path to tesseract.exe in your Python script every time.
-
Open System Properties:
- Go to Control Panel > System and Security > System > Advanced System Settings.
-
Environment Variables:
- Click on Environment Variables.
-
Edit PATH:
- Under System variables, find the Path variable, select it, and click Edit.
-
Add New Path:
- Click New and add the path to the Tesseract executable (e.g.,
C:\Program Files\Tesseract-OCR\).
- Click New and add the path to the Tesseract executable (e.g.,
After this step, you will no longer need to explicitly define the path to the Tesseract executable in your Python code.
- Run the following commands for the installation of the requireds libraries:
pip install pillow pytesseract opencv-python numpy
[Note: If error is met while installing, then run the install command separately.
For example, pip install opencv-python, pip install numpy, pip install pillow, pip install pytesseract]
- Run the Python file using the following command:
python expiration_date_verification.py
For fresh_produce_inspection.py ( to inspect the quality of fresh produce )
1. Run the following commands for the installation of the requireds libraries:
pip install opencv-python numpy
[Note: If error is met while installing, then run the install command separately.
For example, pip install opencv-python, pip install numpy]
- Run the Python file using the following command:
python fresh_produce_inspection.py
This project is released under the MIT License. See the LICENSE file for more information.



