chore: Add OCR functionality using OpenCV and PyTesseract
Browse files- app.py +24 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
import pytesseract
|
5 |
+
|
6 |
+
def ocr_image(img):
|
7 |
+
"""Performs OCR on an image using OpenCV and PyTesseract."""
|
8 |
+
# Preprocess image (optional)
|
9 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
10 |
+
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
|
11 |
+
|
12 |
+
# OCR using PyTesseract
|
13 |
+
text = pytesseract.image_to_string(thresh)
|
14 |
+
return text
|
15 |
+
|
16 |
+
# Create the Gradio interface
|
17 |
+
iface = gr.Interface(
|
18 |
+
fn=ocr_image,
|
19 |
+
inputs="image",
|
20 |
+
outputs="text",
|
21 |
+
title="OCR with OpenCV and PyTesseract"
|
22 |
+
)
|
23 |
+
|
24 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
opencv-python
|
2 |
+
numpy
|
3 |
+
gradio
|
4 |
+
pytesseract
|