Spaces:
Sleeping
Sleeping
locate boxes and green highlightthem
Browse files
app.py
CHANGED
@@ -22,7 +22,7 @@ if 'is_initialized' not in st.session_state:
|
|
22 |
st.session_state['is_initialized'] = True
|
23 |
page_count = utils.get_pdf_page_count(pdf_path)
|
24 |
print("page_count=",page_count)
|
25 |
-
page_count=
|
26 |
print("new page_count=",page_count)
|
27 |
read_pdf_progress_bar = st.progress(0)
|
28 |
st.session_state.color_image_list = []
|
@@ -39,20 +39,29 @@ if 'is_initialized' not in st.session_state:
|
|
39 |
st.session_state.gray_image_np_list.append(cv2.cvtColor(np.array(image_np), cv2.COLOR_BGR2GRAY))
|
40 |
progress_percentage = (index) / (page_count - 1)
|
41 |
read_pdf_progress_bar.progress(progress_percentage)
|
|
|
|
|
|
|
|
|
42 |
st.session_state.img_index = 0
|
43 |
st.session_state.stop_button_clicked=False
|
|
|
44 |
|
|
|
|
|
|
|
|
|
45 |
#if 'img_index' not in st.session_state:
|
46 |
|
47 |
-
if st.button("Stop"):
|
48 |
-
|
49 |
-
st.write(str(st.session_state.img_index+1) +"/" + str(len(st.session_state.color_image_list)))
|
50 |
-
st.image(st.session_state.gray_image_np_list[st.session_state.img_index], use_column_width=True)
|
51 |
-
if not st.session_state.stop_button_clicked:
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
# col1, col2 = st.columns(2)
|
57 |
# with col1:
|
58 |
# if st.button("Previous"):
|
|
|
22 |
st.session_state['is_initialized'] = True
|
23 |
page_count = utils.get_pdf_page_count(pdf_path)
|
24 |
print("page_count=",page_count)
|
25 |
+
page_count=5
|
26 |
print("new page_count=",page_count)
|
27 |
read_pdf_progress_bar = st.progress(0)
|
28 |
st.session_state.color_image_list = []
|
|
|
39 |
st.session_state.gray_image_np_list.append(cv2.cvtColor(np.array(image_np), cv2.COLOR_BGR2GRAY))
|
40 |
progress_percentage = (index) / (page_count - 1)
|
41 |
read_pdf_progress_bar.progress(progress_percentage)
|
42 |
+
# cv2.line(st.session_state.gray_image_np_list[37], (174, 227), (174, 1790), 0, 2)
|
43 |
+
# cv2.line(st.session_state.gray_image_np_list[37], (1550, 227), (1550, 1790), 0, 2)
|
44 |
+
# cv2.line(st.session_state.gray_image_np_list[38], (226,227),(226,1444), 0,3)
|
45 |
+
# cv2.line(st.session_state.gray_image_np_list[38], (1601,227),(1601,1444), 0,2)
|
46 |
st.session_state.img_index = 0
|
47 |
st.session_state.stop_button_clicked=False
|
48 |
+
# st.image(st.session_state.gray_image_np_list[38])
|
49 |
|
50 |
+
for index, gray_pdf_image_np in enumerate(st.session_state.gray_image_np_list[0:5], start=0):
|
51 |
+
print("index="+str(index))
|
52 |
+
|
53 |
+
text=utils.gray_pdf_image_np_to_text(index,gray_pdf_image_np, debug=True)
|
54 |
#if 'img_index' not in st.session_state:
|
55 |
|
56 |
+
# if st.button("Stop"):
|
57 |
+
# st.session_state.stop_button_clicked = True
|
58 |
+
# st.write(str(st.session_state.img_index+1) +"/" + str(len(st.session_state.color_image_list)))
|
59 |
+
# st.image(st.session_state.gray_image_np_list[st.session_state.img_index], use_column_width=True)
|
60 |
+
# if not st.session_state.stop_button_clicked:
|
61 |
+
# if st.session_state.img_index < len(st.session_state.color_image_list) - 1:
|
62 |
+
# st.session_state.img_index += 1
|
63 |
+
# time.sleep(3)
|
64 |
+
# st.rerun()
|
65 |
# col1, col2 = st.columns(2)
|
66 |
# with col1:
|
67 |
# if st.button("Previous"):
|
utils.py
CHANGED
@@ -1,5 +1,8 @@
|
|
1 |
import subprocess
|
2 |
-
|
|
|
|
|
|
|
3 |
def get_pdf_page_count(pdf_path):
|
4 |
try:
|
5 |
# Running pdfinfo command to get information about the PDF
|
@@ -10,4 +13,81 @@ def get_pdf_page_count(pdf_path):
|
|
10 |
return int(line.split(':')[1].strip())
|
11 |
except Exception as e:
|
12 |
print(f"An error occurred: {e}")
|
13 |
-
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import subprocess
|
2 |
+
import streamlit as st
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
def get_pdf_page_count(pdf_path):
|
7 |
try:
|
8 |
# Running pdfinfo command to get information about the PDF
|
|
|
13 |
return int(line.split(':')[1].strip())
|
14 |
except Exception as e:
|
15 |
print(f"An error occurred: {e}")
|
16 |
+
return None
|
17 |
+
#configurable extract rectange rectangle size
|
18 |
+
|
19 |
+
def extract_rectangle_from_image(gray, min_width, min_height):
|
20 |
+
bounding_boxes = []
|
21 |
+
#gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
22 |
+
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
|
23 |
+
#edges = cv2.Canny(gray, 10, 200, apertureSize=3)
|
24 |
+
kernel = np.ones((3,3), np.uint8)
|
25 |
+
dilated_edges = cv2.dilate(edges, kernel, iterations=1)
|
26 |
+
contours, _ = cv2.findContours(dilated_edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
27 |
+
#contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
28 |
+
|
29 |
+
|
30 |
+
index = 0
|
31 |
+
for cnt in contours:
|
32 |
+
approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True)
|
33 |
+
#approx = cv2.approxPolyDP(cnt, 0.1*cv2.arcLength(cnt, True), True)
|
34 |
+
if len(approx) == 4: # Rectangle check
|
35 |
+
x, y, w, h = cv2.boundingRect(approx)
|
36 |
+
# print(f"x: {x}, y: {y}, w: {w}, h: {h}")
|
37 |
+
if w >= min_width and h >= min_height:
|
38 |
+
bounding_boxes.append((x, y, w, h))
|
39 |
+
#print(x, y, w, h)
|
40 |
+
return bounding_boxes
|
41 |
+
def is_close(box1, box2, threshold=10):
|
42 |
+
# Calculate the distance between the top-left corners of the two boxes
|
43 |
+
distance = ((box1[0] - box2[0]) ** 2 + (box1[1] - box2[1]) ** 2) ** 0.5
|
44 |
+
return distance < threshold
|
45 |
+
def remove_close_boxes(boxes, threshold=10):
|
46 |
+
kept_boxes = []
|
47 |
+
for box in boxes:
|
48 |
+
# Assume the box is not close to others by default
|
49 |
+
is_close_to_others = False
|
50 |
+
for kept_box in kept_boxes:
|
51 |
+
if is_close(box, kept_box, threshold):
|
52 |
+
is_close_to_others = True
|
53 |
+
break
|
54 |
+
# If the box is not close to any box we've kept, add it to the list of kept boxes
|
55 |
+
if not is_close_to_others:
|
56 |
+
kept_boxes.append(box)
|
57 |
+
return kept_boxes
|
58 |
+
def is_contained(box1, box2):
|
59 |
+
"""
|
60 |
+
Check if box1 is contained within box2.
|
61 |
+
Each box is defined as (x, y, w, h).
|
62 |
+
"""
|
63 |
+
x1, y1, w1, h1 = box1
|
64 |
+
x2, y2, w2, h2 = box2
|
65 |
+
|
66 |
+
# Check if all corners of box1 are inside box2
|
67 |
+
return x2 <= x1 and y2 <= y1 and x2 + w2 >= x1 + w1 and y2 + h2 >= y1 + h1
|
68 |
+
|
69 |
+
def remove_contained_boxes(boxes):
|
70 |
+
"""
|
71 |
+
Remove boxes that are contained within other boxes.
|
72 |
+
"""
|
73 |
+
non_contained_boxes = []
|
74 |
+
|
75 |
+
for i, box1 in enumerate(boxes):
|
76 |
+
# Check if there's another box that contains box1
|
77 |
+
if not any(is_contained(box1, box2) for j, box2 in enumerate(boxes) if i != j):
|
78 |
+
non_contained_boxes.append(box1)
|
79 |
+
|
80 |
+
return non_contained_boxes
|
81 |
+
def draw_colored_boxes_on_image_np(image, boxes_list,color_tuple):
|
82 |
+
for x, y, w, h in boxes_list:
|
83 |
+
#x, y, w, h = box[0]
|
84 |
+
cv2.rectangle(image, (x, y), (x + w, y + h), color_tuple, thickness=5)
|
85 |
+
def gray_pdf_image_np_to_text(image_index,gray_pdf_image_np, debug=False):
|
86 |
+
bounding_boxes_list = extract_rectangle_from_image(gray_pdf_image_np, 500, 20)
|
87 |
+
bounding_boxes_list = remove_close_boxes (bounding_boxes_list, 10)
|
88 |
+
bounding_boxes_list = remove_contained_boxes(bounding_boxes_list)
|
89 |
+
if debug:
|
90 |
+
bgr_image = cv2.cvtColor(gray_pdf_image_np, cv2.COLOR_GRAY2BGR)
|
91 |
+
color_tuple = (0, 255, 0)
|
92 |
+
draw_colored_boxes_on_image_np(bgr_image, bounding_boxes_list, color_tuple)
|
93 |
+
st.image(Image.fromarray(bgr_image))
|