Skip to content

Commit ee9158f

Browse files
updated core logic
1 parent 7309c54 commit ee9158f

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ This repository extracts the Machine Readable Zone (MRZ) from document images. T
2828
![NumPy](https://img.shields.io/badge/numpy-316192?style=for-the-badge&logo=numpy&logoColor=white)
2929
![ONNX](https://img.shields.io/badge/ONNX-7B7B7B?style=for-the-badge&logo=onnx&logoColor=white)
3030

31-
## ⚙️Installation
32-
31+
## 🚨Prerequisites
32+
- Install [Tesseract OCR](https://tesseract-ocr.github.io/tessdoc/Installation.html) engine. And set `PATH` variable with the executable and ensure that tesseract can be reached from the command line.
3333

34-
1. Install [Tesseract OCR](https://tesseract-ocr.github.io/tessdoc/Installation.html) engine. And set `PATH` variable with the executable.
34+
## ⚙️Installation
3535

36-
2. Install `fastmrz`
36+
1. Install `fastmrz`
3737
```bash
3838
pip install fastmrz
3939
```
@@ -44,7 +44,7 @@ This repository extracts the Machine Readable Zone (MRZ) from document images. T
4444
conda activate fastmrz
4545
```
4646

47-
3. Copy the `mrz.traineddata` file from the `tessdata` folder of the [repository](https://github.com/sivakumar-mahalingam/fastmrz/raw/main/tessdata/mrz.traineddata) into the `tessdata` folder of the Tesseract installation on **YOUR MACHINE**
47+
2. Copy the `mrz.traineddata` file from the `tessdata` folder of the [repository](https://github.com/sivakumar-mahalingam/fastmrz/raw/main/tessdata/mrz.traineddata) into the `tessdata` folder of the Tesseract installation on **YOUR MACHINE**
4848

4949
## 💡Example
5050

@@ -114,7 +114,7 @@ Now, based on the example of a national passport, let us take a closer look at t
114114
115115
![MRZ GIF](https://raw.githubusercontent.com/sivakumar-mahalingam/fastmrz/main/docs/mrz.gif)
116116
117-
## 🗹ToDo
117+
## ToDo
118118
119119
- [x] Include mrva and mrvb documents
120120
- [x] Add wiki page
@@ -130,6 +130,7 @@ Now, based on the example of a national passport, let us take a closer look at t
130130
- [x] Add function - validate_mrz
131131
- [ ] Add function - generate_mrz
132132
- [ ] Extract face image
133+
- [ ] Add documentation page
133134
134135
## ⚖️License
135136

fastmrz/fastmrz.py

+7-15
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,6 @@ def _get_roi(self, output_data, image_path):
3131
pytesseract.pytesseract.tesseract_cmd = self.tesseract_path
3232
image = cv2.imread(image_path, cv2.IMREAD_COLOR) if isinstance(image_path, str) else image_path
3333

34-
# Add preprocessing steps
35-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
36-
# Increase contrast
37-
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
38-
gray = clahe.apply(gray)
39-
40-
# Denoise
41-
denoised = cv2.fastNlMeansDenoising(gray)
42-
4334
output_data = (output_data[0, :, :, 0] > 0.25) * 1
4435
output_data = np.uint8(output_data * 255)
4536
altered_image = cv2.resize(output_data, (image.shape[1], image.shape[0]))
@@ -58,18 +49,14 @@ def _get_roi(self, output_data, image_path):
5849
x, y, w, h = cv2.boundingRect(contours[np.argmax(c_area)])
5950

6051
# Add padding to the ROI
61-
padding = 10 # Adjust this value as needed
52+
padding = 10
6253
x_start = max(0, x - padding)
6354
y_start = max(0, y - padding)
6455
x_end = min(image.shape[1], x + w + padding)
6556
y_end = min(image.shape[0], y + h + padding)
6657

6758
roi_arr = image[y_start:y_end, x_start:x_end].copy()
6859

69-
# Optional: Show the ROI for debugging
70-
# cv2.imshow("ROI", roi_arr)
71-
# cv2.waitKey(0)
72-
7360
# Apply additional preprocessing to ROI before OCR
7461
roi_gray = cv2.cvtColor(roi_arr, cv2.COLOR_BGR2GRAY)
7562
roi_thresh = cv2.threshold(roi_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
@@ -283,7 +270,12 @@ def get_details(self, input_data, input_type="imagepath", ignore_parse=False, in
283270
if input_type == "imagepath":
284271
if not self._is_valid(input_data):
285272
return {"status": "FAILURE", "message": "Invalid input image"}
286-
mrz_text = self._get_mrz(input_data)
273+
image_file = open(input_data, "rb")
274+
image_data = image_file.read()
275+
image_file.close()
276+
base64_string = base64.b64encode(image_data).decode("utf-8")
277+
image_array = self._base64_to_image_array(base64_string)
278+
mrz_text = self._get_mrz(image_array)
287279

288280
return mrz_text if ignore_parse else self._parse_mrz(mrz_text)
289281
elif input_type == "numpy":

0 commit comments

Comments
 (0)