jayllfpt commited on
Commit
a11676a
·
1 Parent(s): daa904a

update copy features

Browse files
Files changed (2) hide show
  1. app.py +47 -6
  2. requirements.txt +2 -0
app.py CHANGED
@@ -4,6 +4,9 @@ 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()
@@ -38,8 +41,16 @@ def transform_image(image, box):
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
 
@@ -55,19 +66,49 @@ def main():
55
  processing_time = time.perf_counter() - start
56
  st.write(f"Box detection took {processing_time:.2f} seconds.")
57
 
 
 
 
58
  for box in boxes[::-1]:
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
  # Convert back to PIL Image for displaying
64
  output_image = Image.fromarray(cv2.cvtColor(org_image, cv2.COLOR_BGR2RGB))
65
- st.image(output_image, caption='Detected Text Boxes', use_column_width=True)
66
 
67
- texts = text_engine(images)
68
-
69
- st.write("Extracted Texts:")
70
- st.write(texts)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  if __name__ == '__main__':
73
  main()
 
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
  # Initialize the OCR engines
12
  box_engine = BoxEngine()
 
41
 
42
  return dst_img
43
 
44
+ def is_point_in_box(point, box):
45
+ """
46
+ Check if a point is inside a bounding box.
47
+ """
48
+ x, y = point
49
+ contour = box.reshape((-1, 1, 2)).astype(np.int32)
50
+ return cv2.pointPolygonTest(contour, (x, y), False) >= 0
51
+
52
  def main():
53
+ st.title("Quick Copy")
54
 
55
  uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
56
 
 
66
  processing_time = time.perf_counter() - start
67
  st.write(f"Box detection took {processing_time:.2f} seconds.")
68
 
69
+ # Extracted text for each box
70
+ extracted_texts = []
71
+
72
  for box in boxes[::-1]:
73
  org_image = cv2.polylines(org_image, [box.astype(np.int32)], isClosed=True, color=(0, 255, 0), thickness=2)
74
  image = transform_image(org_image, box)
75
  images.append(image)
76
 
77
+ # Get the texts from the boxes
78
+ texts = text_engine(images)
79
+
80
+ # Store extracted texts with corresponding boxes
81
+ for text, box in zip(texts, boxes[::-1]):
82
+ extracted_texts.append((text, box))
83
+
84
  # Convert back to PIL Image for displaying
85
  output_image = Image.fromarray(cv2.cvtColor(org_image, cv2.COLOR_BGR2RGB))
86
+ # st.image(output_image, caption='Detected Text Boxes', use_column_width=True)
87
 
88
+ # Create a drawable canvas
89
+ canvas_result = st_canvas(
90
+ background_image=output_image,
91
+ stroke_width=0,
92
+ update_streamlit=True,
93
+ height=output_image.height,
94
+ width=output_image.width,
95
+ drawing_mode="freedraw", # Disable drawing to make it click only
96
+ key="canvas",
97
+ )
98
+
99
+ # Check if any clicks are detected
100
+ if canvas_result.json_data is not None:
101
+ for annotation in canvas_result.json_data["objects"]:
102
+ x = annotation["left"]
103
+ y = annotation["top"]
104
+ clicked_point = (x, y)
105
+
106
+ # Check which bounding box the user clicked on
107
+ for text, box in extracted_texts:
108
+ if is_point_in_box(clicked_point, box):
109
+ pyperclip.copy(text[0]) # Copy text to clipboard
110
+ st.toast(f"Text copied to clipboard: {text}") # Show popup message
111
+ break # Exit after finding the first match
112
 
113
  if __name__ == '__main__':
114
  main()
requirements.txt CHANGED
@@ -2,3 +2,5 @@ streamlit
2
  akaocr
3
  opencv-python-headless
4
  numpy
 
 
 
2
  akaocr
3
  opencv-python-headless
4
  numpy
5
+ pyperclip
6
+ streamlit_drawable_canvas