jayllfpt commited on
Commit
baa6acd
·
1 Parent(s): eaf0748
Files changed (2) hide show
  1. app.py +3 -121
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,124 +1,6 @@
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
- import pyperclip # Import pyperclip for clipboard operations
8
 
9
- from streamlit_drawable_canvas import st_canvas
10
 
11
-
12
- # Initialize the OCR engines
13
- box_engine = BoxEngine()
14
- text_engine = TextEngine()
15
-
16
-
17
- def transform_image(image, box):
18
- # Get perspective transform image
19
- assert len(box) == 4, "Shape of points must be 4x2"
20
- img_crop_width = int(
21
- max(
22
- np.linalg.norm(box[0] - box[1]),
23
- np.linalg.norm(box[2] - box[3])))
24
- img_crop_height = int(
25
- max(
26
- np.linalg.norm(box[0] - box[3]),
27
- np.linalg.norm(box[1] - box[2])))
28
- pts_std = np.float32([[0, 0],
29
- [img_crop_width, 0],
30
- [img_crop_width, img_crop_height],
31
- [0, img_crop_height]])
32
- box = np.array(box, dtype="float32")
33
- M = cv2.getPerspectiveTransform(box, pts_std)
34
- dst_img = cv2.warpPerspective(
35
- image,
36
- M, (img_crop_width, img_crop_height),
37
- borderMode=cv2.BORDER_REPLICATE,
38
- flags=cv2.INTER_CUBIC)
39
-
40
- img_height, img_width = dst_img.shape[0:2]
41
- if img_height/img_width >= 1.25:
42
- dst_img = np.rot90(dst_img, k=3)
43
-
44
- return dst_img
45
-
46
-
47
- def is_point_in_box(point, box):
48
- """
49
- Check if a point is inside a bounding box.
50
- """
51
- x, y = point
52
- contour = box.reshape((-1, 1, 2)).astype(np.int32)
53
- return cv2.pointPolygonTest(contour, (x, y), False) >= 0
54
-
55
-
56
- def main():
57
- st.title("Quick OCR Copy")
58
-
59
- uploaded_file = st.file_uploader(
60
- "Choose an image...", type=["jpg", "jpeg", "png"])
61
-
62
- if uploaded_file is not None:
63
- # Convert the uploaded file to an OpenCV image
64
- file_bytes = np.asarray(
65
- bytearray(uploaded_file.read()), dtype=np.uint8)
66
- org_image = cv2.imdecode(file_bytes, 1)
67
- # st.image(org_image, channels="BGR", caption='Uploaded Image')
68
-
69
- images = []
70
- start = time.perf_counter()
71
- boxes = box_engine(org_image)
72
- processing_time = time.perf_counter() - start
73
- st.write(f"Box detection took {processing_time:.2f} seconds.")
74
-
75
- # Extracted text for each box
76
- extracted_texts = []
77
-
78
- for box in boxes[::-1]:
79
- org_image = cv2.polylines(org_image, [box.astype(
80
- np.int32)], isClosed=True, color=(0, 255, 0), thickness=2)
81
- image = transform_image(org_image, box)
82
- images.append(image)
83
-
84
- # Get the texts from the boxes
85
- texts = text_engine(images)
86
-
87
- # Store extracted texts with corresponding boxes
88
- for text, box in zip(texts, boxes[::-1]):
89
- extracted_texts.append((text, box))
90
-
91
- # Convert back to PIL Image for displaying
92
- output_image = Image.fromarray(
93
- cv2.cvtColor(org_image, cv2.COLOR_BGR2RGB))
94
- # st.image(output_image, caption='Detected Text Boxes', use_column_width=True)
95
-
96
- # Create a drawable canvas
97
- canvas_result = st_canvas(
98
- background_image=output_image,
99
- stroke_width=0,
100
- update_streamlit=True,
101
- height=output_image.height,
102
- width=output_image.width,
103
- drawing_mode="freedraw", # Disable drawing to make it click only
104
- key="canvas",
105
- )
106
-
107
- # Check if any clicks are detected
108
- if canvas_result.json_data is not None:
109
- for annotation in canvas_result.json_data["objects"]:
110
- x = annotation["left"]
111
- y = annotation["top"]
112
- clicked_point = (x, y)
113
-
114
- # Check which bounding box the user clicked on
115
- for text, box in extracted_texts:
116
- if is_point_in_box(clicked_point, box):
117
- # pyperclip.copy(text[0]) # Copy text to clipboard
118
- # Show popup message
119
- st.toast(f"Text copied to clipboard: {text}")
120
- break # Exit after finding the first match
121
-
122
-
123
- if __name__ == '__main__':
124
- main()
 
1
  import streamlit as st
 
 
 
 
 
 
2
 
3
+ from st_copy_to_clipboard import st_copy_to_clipboard
4
 
5
+ # Render copy to clipboard button
6
+ st_copy_to_clipboard("Copy this to clipboard")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -3,4 +3,5 @@ akaocr
3
  opencv-python-headless
4
  numpy
5
  pyperclip
6
- streamlit_drawable_canvas
 
 
3
  opencv-python-headless
4
  numpy
5
  pyperclip
6
+ streamlit_drawable_canvas
7
+ st-copy-to-clipboard