Spaces:
Build error
Build error
add files
Browse files- app.py +72 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from akaocr import TextEngine, BoxEngine
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
# Initialize the OCR engines
|
| 9 |
+
box_engine = BoxEngine()
|
| 10 |
+
text_engine = TextEngine()
|
| 11 |
+
|
| 12 |
+
def transform_image(image, box):
|
| 13 |
+
# Get perspective transform image
|
| 14 |
+
assert len(box) == 4, "Shape of points must be 4x2"
|
| 15 |
+
img_crop_width = int(
|
| 16 |
+
max(
|
| 17 |
+
np.linalg.norm(box[0] - box[1]),
|
| 18 |
+
np.linalg.norm(box[2] - box[3])))
|
| 19 |
+
img_crop_height = int(
|
| 20 |
+
max(
|
| 21 |
+
np.linalg.norm(box[0] - box[3]),
|
| 22 |
+
np.linalg.norm(box[1] - box[2])))
|
| 23 |
+
pts_std = np.float32([[0, 0],
|
| 24 |
+
[img_crop_width, 0],
|
| 25 |
+
[img_crop_width, img_crop_height],
|
| 26 |
+
[0, img_crop_height]])
|
| 27 |
+
box = np.array(box, dtype="float32")
|
| 28 |
+
M = cv2.getPerspectiveTransform(box, pts_std)
|
| 29 |
+
dst_img = cv2.warpPerspective(
|
| 30 |
+
image,
|
| 31 |
+
M, (img_crop_width, img_crop_height),
|
| 32 |
+
borderMode=cv2.BORDER_REPLICATE,
|
| 33 |
+
flags=cv2.INTER_CUBIC)
|
| 34 |
+
|
| 35 |
+
img_height, img_width = dst_img.shape[0:2]
|
| 36 |
+
if img_height/img_width >= 1.25:
|
| 37 |
+
dst_img = np.rot90(dst_img, k=3)
|
| 38 |
+
|
| 39 |
+
return dst_img
|
| 40 |
+
|
| 41 |
+
def main():
|
| 42 |
+
st.title("OCR Application with akaOCR")
|
| 43 |
+
|
| 44 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 45 |
+
|
| 46 |
+
if uploaded_file is not None:
|
| 47 |
+
# Convert the uploaded file to an OpenCV image
|
| 48 |
+
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 49 |
+
org_image = cv2.imdecode(file_bytes, 1)
|
| 50 |
+
st.image(org_image, channels="BGR", caption='Uploaded Image')
|
| 51 |
+
|
| 52 |
+
images = []
|
| 53 |
+
start = time.perf_counter()
|
| 54 |
+
boxes = box_engine(org_image)
|
| 55 |
+
processing_time = time.perf_counter() - start
|
| 56 |
+
st.write(f"Box detection took {processing_time:.2f} seconds.")
|
| 57 |
+
|
| 58 |
+
for box in boxes:
|
| 59 |
+
org_image = cv2.polylines(org_image, [box.astype(np.int32)], isClosed=True, color=(0, 255, 0), thickness=2)
|
| 60 |
+
image = transform_image(org_image, box)
|
| 61 |
+
images.append(image)
|
| 62 |
+
|
| 63 |
+
texts = text_engine(images)
|
| 64 |
+
|
| 65 |
+
# Convert back to PIL Image for displaying
|
| 66 |
+
output_image = Image.fromarray(cv2.cvtColor(org_image, cv2.COLOR_BGR2RGB))
|
| 67 |
+
st.image(output_image, caption='Detected Text Boxes', use_column_width=True)
|
| 68 |
+
st.write("Extracted Texts:")
|
| 69 |
+
st.write(texts)
|
| 70 |
+
|
| 71 |
+
if __name__ == '__main__':
|
| 72 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
akaocr
|
| 3 |
+
opencv-python-headless
|
| 4 |
+
numpy
|