brody715 commited on
Commit
06ba3f6
·
0 Parent(s):

feat: init

Browse files
.gitignore ADDED
@@ -0,0 +1,221 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Created by https://www.toptal.com/developers/gitignore/api/python,visualstudiocode,linux,opencv
2
+ # Edit at https://www.toptal.com/developers/gitignore?templates=python,visualstudiocode,linux,opencv
3
+
4
+ ### Linux ###
5
+ *~
6
+
7
+ # temporary files which can be created if a process still has a handle open of a deleted file
8
+ .fuse_hidden*
9
+
10
+ # KDE directory preferences
11
+ .directory
12
+
13
+ # Linux trash folder which might appear on any partition or disk
14
+ .Trash-*
15
+
16
+ # .nfs files are created when an open file is removed but is still being accessed
17
+ .nfs*
18
+
19
+ ### OpenCV ###
20
+ #OpenCV for Mac and Linux
21
+ #build and release folders
22
+ */CMakeFiles
23
+ */CMakeCache.txt
24
+ */Makefile
25
+ */cmake_install.cmake
26
+ .DS_Store
27
+
28
+ ### Python ###
29
+ # Byte-compiled / optimized / DLL files
30
+ __pycache__/
31
+ *.py[cod]
32
+ *$py.class
33
+
34
+ # C extensions
35
+ *.so
36
+
37
+ # Distribution / packaging
38
+ .Python
39
+ build/
40
+ develop-eggs/
41
+ dist/
42
+ downloads/
43
+ eggs/
44
+ .eggs/
45
+ lib/
46
+ lib64/
47
+ parts/
48
+ sdist/
49
+ var/
50
+ wheels/
51
+ share/python-wheels/
52
+ *.egg-info/
53
+ .installed.cfg
54
+ *.egg
55
+ MANIFEST
56
+
57
+ # PyInstaller
58
+ # Usually these files are written by a python script from a template
59
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
60
+ *.manifest
61
+ *.spec
62
+
63
+ # Installer logs
64
+ pip-log.txt
65
+ pip-delete-this-directory.txt
66
+
67
+ # Unit test / coverage reports
68
+ htmlcov/
69
+ .tox/
70
+ .nox/
71
+ .coverage
72
+ .coverage.*
73
+ .cache
74
+ nosetests.xml
75
+ coverage.xml
76
+ *.cover
77
+ *.py,cover
78
+ .hypothesis/
79
+ .pytest_cache/
80
+ cover/
81
+
82
+ # Translations
83
+ *.mo
84
+ *.pot
85
+
86
+ # Django stuff:
87
+ *.log
88
+ local_settings.py
89
+ db.sqlite3
90
+ db.sqlite3-journal
91
+
92
+ # Flask stuff:
93
+ instance/
94
+ .webassets-cache
95
+
96
+ # Scrapy stuff:
97
+ .scrapy
98
+
99
+ # Sphinx documentation
100
+ docs/_build/
101
+
102
+ # PyBuilder
103
+ .pybuilder/
104
+ target/
105
+
106
+ # Jupyter Notebook
107
+ .ipynb_checkpoints
108
+
109
+ # IPython
110
+ profile_default/
111
+ ipython_config.py
112
+
113
+ # pyenv
114
+ # For a library or package, you might want to ignore these files since the code is
115
+ # intended to run in multiple environments; otherwise, check them in:
116
+ # .python-version
117
+
118
+ # pipenv
119
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
120
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
121
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
122
+ # install all needed dependencies.
123
+ #Pipfile.lock
124
+
125
+ # poetry
126
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
127
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
128
+ # commonly ignored for libraries.
129
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
130
+ #poetry.lock
131
+
132
+ # pdm
133
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
134
+ #pdm.lock
135
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
136
+ # in version control.
137
+ # https://pdm.fming.dev/#use-with-ide
138
+ .pdm.toml
139
+
140
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
141
+ __pypackages__/
142
+
143
+ # Celery stuff
144
+ celerybeat-schedule
145
+ celerybeat.pid
146
+
147
+ # SageMath parsed files
148
+ *.sage.py
149
+
150
+ # Environments
151
+ .env
152
+ .venv
153
+ env/
154
+ venv/
155
+ ENV/
156
+ env.bak/
157
+ venv.bak/
158
+
159
+ # Spyder project settings
160
+ .spyderproject
161
+ .spyproject
162
+
163
+ # Rope project settings
164
+ .ropeproject
165
+
166
+ # mkdocs documentation
167
+ /site
168
+
169
+ # mypy
170
+ .mypy_cache/
171
+ .dmypy.json
172
+ dmypy.json
173
+
174
+ # Pyre type checker
175
+ .pyre/
176
+
177
+ # pytype static type analyzer
178
+ .pytype/
179
+
180
+ # Cython debug symbols
181
+ cython_debug/
182
+
183
+ # PyCharm
184
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
185
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
186
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
187
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
188
+ #.idea/
189
+
190
+ ### Python Patch ###
191
+ # Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
192
+ poetry.toml
193
+
194
+ # ruff
195
+ .ruff_cache/
196
+
197
+ # LSP config files
198
+ pyrightconfig.json
199
+
200
+ ### VisualStudioCode ###
201
+ .vscode/*
202
+ !.vscode/settings.json
203
+ !.vscode/tasks.json
204
+ !.vscode/launch.json
205
+ !.vscode/extensions.json
206
+ !.vscode/*.code-snippets
207
+
208
+ # Local History for Visual Studio Code
209
+ .history/
210
+
211
+ # Built Visual Studio Code Extensions
212
+ *.vsix
213
+
214
+ ### VisualStudioCode Patch ###
215
+ # Ignore all local history of files
216
+ .history
217
+ .ionide
218
+
219
+ # End of https://www.toptal.com/developers/gitignore/api/python,visualstudiocode,linux,opencv
220
+
221
+ .venv
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: PythonTools
3
+ emoji: 🚀
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 4.12.0
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
+ # PythonTools
13
+
_hacked.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ from pathlib import Path
4
+
5
+ script_dir = Path(os.path.dirname(os.path.realpath(__file__)))
6
+ sys.path.append(script_dir.as_posix())
app.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ import _hacked # noqa
2
+ import gradio as gr
3
+
4
+ from python_tools.tools import image_20231225
5
+
6
+ iface = gr.TabbedInterface(
7
+ interface_list=[image_20231225.get_ui()], tab_names=["image_20231225"]
8
+ )
9
+
10
+ iface.launch()
python_tools/shared/__init__.py ADDED
File without changes
python_tools/tools/image_20231225/__init__.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image
4
+ import cv2
5
+
6
+
7
+ def get_max_contours(contours):
8
+ max_rect = None
9
+ max_area = 0
10
+ for cnt in contours:
11
+ rect = cv2.minAreaRect(cnt)
12
+ area = cv2.contourArea(cnt)
13
+ if area > max_area:
14
+ max_area = area
15
+ max_rect = rect
16
+
17
+ return max_rect, cnt
18
+
19
+
20
+ def find_rotation_angle(image):
21
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
22
+
23
+ _, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
24
+
25
+ contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
26
+
27
+ max_rect, cnt = get_max_contours(contours)
28
+ if not max_rect:
29
+ return None
30
+
31
+ angle = max_rect[2]
32
+ if angle < -45:
33
+ angle = 90 + angle
34
+
35
+ return angle
36
+
37
+
38
+ # rotate image to correct text orientation
39
+ def image_rotate_text(img):
40
+ angle = find_rotation_angle(img)
41
+ print(f"rotated angle: {angle}")
42
+
43
+ h, w = img.shape[:2]
44
+ center = (w / 2, h / 2)
45
+ M = cv2.getRotationMatrix2D(center, angle, 1.0)
46
+
47
+ # 旋转图像
48
+ rotated = cv2.warpAffine(
49
+ img,
50
+ M,
51
+ (w, h),
52
+ flags=cv2.INTER_LINEAR,
53
+ borderMode=cv2.BORDER_CONSTANT,
54
+ borderValue=(0, 0, 0),
55
+ )
56
+ return rotated
57
+
58
+
59
+ # crop image to remove unnecessary borders
60
+ def image_crop_border(img):
61
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
62
+
63
+ _, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
64
+
65
+ contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
66
+
67
+ if not contours:
68
+ return None
69
+
70
+ counters = max(contours, key=cv2.contourArea)
71
+
72
+ x, y, w, h = cv2.boundingRect(counters)
73
+
74
+ def with_delta(x, y, w, h, delta):
75
+ return x - delta, y - delta, w + delta * 2, h + delta * 2
76
+
77
+ x, y, w, h = with_delta(x, y, w, h, 100)
78
+
79
+ cropped_image = img[y : y + h, x : x + w]
80
+ return cropped_image
81
+
82
+
83
+ def process(img):
84
+ img = image_rotate_text(img)
85
+ img = image_crop_border(img)
86
+
87
+ return img
88
+
89
+
90
+ def pil_process_image(image):
91
+ img = np.array(image)
92
+ processed = process(img)
93
+ return Image.fromarray(processed)
94
+
95
+
96
+ def get_ui():
97
+ return gr.Interface(
98
+ fn=pil_process_image,
99
+ inputs=gr.Image(),
100
+ outputs=gr.Image(type="pil"),
101
+ title="2023-12-25 Image Processing",
102
+ description="旋转和裁剪图像以纠正文本方向并去除不必要的边框",
103
+ )
104
+
105
+
106
+ if __name__ == "__main__":
107
+ get_ui()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ opencv-python>=1.26
2
+ gradio
3
+ pillow>=10.1.0