Spaces:
Sleeping
Sleeping
upload files
Browse files- BIB_Extraction.py +80 -0
- README.md +5 -4
- __pycache__/BIB_Extraction.cpython-311.pyc +0 -0
- app.py +29 -0
- requirements.txt +4 -0
BIB_Extraction.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from akaocr import TextEngine, BoxEngine
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def transform_image(image, box):
|
| 9 |
+
# Get perspective transform image
|
| 10 |
+
assert len(box) == 4, "Shape of points must be 4x2"
|
| 11 |
+
img_crop_width = int(
|
| 12 |
+
max(
|
| 13 |
+
np.linalg.norm(box[0] - box[1]),
|
| 14 |
+
np.linalg.norm(box[2] - box[3])))
|
| 15 |
+
img_crop_height = int(
|
| 16 |
+
max(
|
| 17 |
+
np.linalg.norm(box[0] - box[3]),
|
| 18 |
+
np.linalg.norm(box[1] - box[2])))
|
| 19 |
+
pts_std = np.float32([[0, 0],
|
| 20 |
+
[img_crop_width, 0],
|
| 21 |
+
[img_crop_width, img_crop_height],
|
| 22 |
+
[0, img_crop_height]])
|
| 23 |
+
box = np.array(box, dtype="float32")
|
| 24 |
+
M = cv2.getPerspectiveTransform(box, pts_std)
|
| 25 |
+
dst_img = cv2.warpPerspective(
|
| 26 |
+
image,
|
| 27 |
+
M, (img_crop_width, img_crop_height),
|
| 28 |
+
borderMode=cv2.BORDER_REPLICATE,
|
| 29 |
+
flags=cv2.INTER_CUBIC)
|
| 30 |
+
|
| 31 |
+
img_height, img_width = dst_img.shape[0:2]
|
| 32 |
+
if img_height/img_width >= 1.25:
|
| 33 |
+
dst_img = np.rot90(dst_img, k=3)
|
| 34 |
+
|
| 35 |
+
return dst_img
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def two_pts(bounding_box):
|
| 39 |
+
# convert 4-points-bounding-box to 2-points-bounding-box
|
| 40 |
+
return (
|
| 41 |
+
(
|
| 42 |
+
round(min([x[0] for x in bounding_box])),
|
| 43 |
+
round(min([x[1] for x in bounding_box]))
|
| 44 |
+
),
|
| 45 |
+
(
|
| 46 |
+
round(max([x[0] for x in bounding_box])),
|
| 47 |
+
round(max([x[1] for x in bounding_box]))
|
| 48 |
+
)
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
class BIB_Extract:
|
| 53 |
+
def __init__(self):
|
| 54 |
+
# Initialize the OCR engines
|
| 55 |
+
self.box_engine = BoxEngine()
|
| 56 |
+
self.text_engine = TextEngine()
|
| 57 |
+
|
| 58 |
+
def __call__(self, image, bib_length):
|
| 59 |
+
boxes = self.box_engine(image)
|
| 60 |
+
images = []
|
| 61 |
+
# crop and transform images for recognition
|
| 62 |
+
for box in boxes[::-1]:
|
| 63 |
+
# org_image = cv2.polylines(org_image, [box.astype(
|
| 64 |
+
# np.int32)], isClosed=True, color=(0, 255, 0), thickness=2)
|
| 65 |
+
crop_img = transform_image(image, box)
|
| 66 |
+
images.append(crop_img)
|
| 67 |
+
|
| 68 |
+
# Get the texts from the boxes
|
| 69 |
+
texts = self.text_engine(images)
|
| 70 |
+
return self.BIB_filter(texts, bib_length)
|
| 71 |
+
|
| 72 |
+
def BIB_filter(self, texts, bib_length):
|
| 73 |
+
pattern = rf'^\d{{{bib_length}}}$'
|
| 74 |
+
return [s[0] for s in texts if re.match(pattern, s[0])]
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if __name__ == '__main__':
|
| 78 |
+
image = cv2.imread("1.jpg")
|
| 79 |
+
engine = BIB_Extract()
|
| 80 |
+
print(engine(image, bib_length=4))
|
README.md
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: blue
|
| 6 |
sdk: streamlit
|
| 7 |
-
sdk_version: 1.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Akaocr
|
| 3 |
+
emoji: 🐨
|
| 4 |
+
colorFrom: pink
|
| 5 |
colorTo: blue
|
| 6 |
sdk: streamlit
|
| 7 |
+
sdk_version: 1.37.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
__pycache__/BIB_Extraction.cpython-311.pyc
ADDED
|
Binary file (5.13 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from BIB_Extraction import BIB_Extract, two_pts
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
engine = BIB_Extract()
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def main():
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="BIB Detection",
|
| 12 |
+
page_icon=":flag-vn:",
|
| 13 |
+
# layout="wide"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
uploaded_file = st.file_uploader(
|
| 17 |
+
"Choose an image...", type=["jpg", "jpeg", "png"])
|
| 18 |
+
|
| 19 |
+
if uploaded_file is not None:
|
| 20 |
+
# Convert the uploaded file to an OpenCV image
|
| 21 |
+
file_bytes = np.asarray(
|
| 22 |
+
bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 23 |
+
org_image = cv2.imdecode(file_bytes, 1)
|
| 24 |
+
st.image(org_image, channels="BGR", caption='Uploaded Image')
|
| 25 |
+
st.text(f"BIB Numbers: {engine(org_image, bib_length=4)}")
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
if __name__ == '__main__':
|
| 29 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
akaocr
|
| 3 |
+
opencv-python-headless
|
| 4 |
+
numpy
|