Skip to content

Commit a3ed6fd

Browse files
Add Text Extraction (OCR) via an extension. (#216)
* Add initial new OCR implementation with extension support * Update be.alexandervanhee.gradia.ocr.json * update dialog * Add Managment of new models * make selected OCR model persistent * change extension id * improve build files * fix cropping on padded images * Update ocr_dialog.blp * Add download sizes * Update be.alexandervanhee.gradia.Plugin.ocr.json * update plugin to 49 * move ocr folder constants to build files
1 parent c072ceb commit a3ed6fd

28 files changed

+1613
-214
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,6 @@ If you'd like Gradia to **open automatically** after taking a screenshot, you ca
7676
1. Install Builder from [Flathub](https://flathub.org/apps/org.gnome.Builder).
7777
2. Click the **Clone Repository** button at the bottom right and enter the repository URL.
7878
3. Once cloned, locate the dropdown menu next to the `be.alexandervanhee.gradia.json` text at the top of the screen.
79-
4. Use the dropdown to press **Build** to compile the project. From the same menu, you can also **Run** the project or **Export** it as a Flatpak bundle.
79+
4. In the dropdown, use the **Active Configuration** option to select either the *Devel* Flatpak build file or the regular one, then press the **Run** button.
80+
The *Devel* configuration includes everything required for OCR.
81+
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
{
2+
"id": "be.alexandervanhee.gradia.Devel",
3+
"runtime": "org.gnome.Platform",
4+
"runtime-version": "49",
5+
"sdk": "org.gnome.Sdk",
6+
"command": "gradia",
7+
"finish-args": [
8+
"--share=ipc",
9+
"--socket=fallback-x11",
10+
"--device=dri",
11+
"--socket=wayland",
12+
"--filesystem=xdg-pictures",
13+
"--share=network",
14+
"--own-name=be.alexandervanhee.gradia.Devel"
15+
],
16+
"cleanup": [
17+
"/include",
18+
"/lib/pkgconfig",
19+
"/man",
20+
"/share/doc",
21+
"/share/gtk-doc",
22+
"/share/man",
23+
"/share/pkgconfig",
24+
"/lib/cmake",
25+
"/lib/girepository-1.0",
26+
"/share/gir-1.0",
27+
"/share/vala",
28+
"/share/aclocal",
29+
"*.la",
30+
"*.a"
31+
],
32+
"cleanup-commands": [
33+
"mkdir -p ${FLATPAK_DEST}/extensions/ocr"
34+
],
35+
"modules": [
36+
"pypi-dependencies.json",
37+
{
38+
"name": "blueprint-compiler",
39+
"buildsystem": "meson",
40+
"cleanup": [
41+
"*"
42+
],
43+
"sources": [
44+
{
45+
"type": "git",
46+
"url": "https://gitlab.gnome.org/GNOME/blueprint-compiler",
47+
"tag": "v0.18.0"
48+
}
49+
]
50+
},
51+
{
52+
"name": "libportal",
53+
"buildsystem": "meson",
54+
"config-opts": [
55+
"-Ddocs=false",
56+
"-Dbackend-gtk4=enabled"
57+
],
58+
"sources": [
59+
{
60+
"type": "archive",
61+
"url": "https://github.com/flatpak/libportal/archive/refs/tags/0.9.1.tar.gz",
62+
"sha256": "ea422b789ae487e04194d387bea031fd7485bf88a18aef8c767f7d1c29496a4e",
63+
"x-checker-data": {
64+
"type": "anitya",
65+
"project-id": 230124,
66+
"url-template": "https://github.com/flatpak/libportal/archive/refs/tags/$version.tar.gz"
67+
}
68+
}
69+
]
70+
},
71+
{
72+
"name": "leptonica",
73+
"buildsystem": "autotools",
74+
"config-opts": [
75+
"--disable-programs",
76+
"--disable-static",
77+
"--prefix=/app/extensions/ocr"
78+
],
79+
"sources": [
80+
{
81+
"type": "archive",
82+
"url": "https://github.com/DanBloomberg/leptonica/releases/download/1.85.0/leptonica-1.85.0.tar.gz",
83+
"sha256": "3745ae3bf271a6801a2292eead83ac926e3a9bc1bf622e9cd4dd0f3786e17205"
84+
}
85+
]
86+
},
87+
{
88+
"name": "tesseract",
89+
"buildsystem": "cmake-ninja",
90+
"builddir": true,
91+
"build-options": {
92+
"env": {
93+
"PKG_CONFIG_PATH": "/app/extensions/ocr/lib/pkgconfig",
94+
"LD_LIBRARY_PATH": "/app/extensions/ocr/lib"
95+
}
96+
},
97+
"config-opts": [
98+
"-DCMAKE_BUILD_TYPE=Release",
99+
"-DBUILD_TRAINING_TOOLS=OFF",
100+
"-DUSE_SYSTEM_ICU=ON",
101+
"-DCMAKE_INSTALL_PREFIX=/app/extensions/ocr",
102+
"-DLeptonica_DIR=/app/extensions/ocr/lib/cmake",
103+
"-DCMAKE_INSTALL_RPATH=/app/extensions/ocr/lib"
104+
],
105+
"sources": [
106+
{
107+
"type": "archive",
108+
"url": "https://github.com/tesseract-ocr/tesseract/archive/5.5.1.tar.gz",
109+
"sha256": "a7a3f2a7420cb6a6a94d80c24163e183cf1d2f1bed2df3bbc397c81808a57237"
110+
}
111+
]
112+
},
113+
{
114+
"name": "tesseract-language-data",
115+
"buildsystem": "simple",
116+
"build-commands": [
117+
"mkdir -p ${FLATPAK_DEST}/extensions/ocr/share/tessdata",
118+
"install -Dm644 eng.traineddata ${FLATPAK_DEST}/extensions/ocr/share/tessdata/"
119+
],
120+
"sources": [
121+
{
122+
"type": "file",
123+
"url": "https://github.com/tesseract-ocr/tessdata_best/raw/4.1.0/eng.traineddata",
124+
"sha256": "8280aed0782fe27257a68ea10fe7ef324ca0f8d85bd2fd145d1c2b560bcb66ba",
125+
"dest-filename": "eng.traineddata"
126+
}
127+
]
128+
},
129+
{
130+
"name": "gradia",
131+
"builddir": true,
132+
"buildsystem": "meson",
133+
"config-opts": [
134+
"-Dbuildtype=debug"
135+
],
136+
"sources": [
137+
{
138+
"type": "dir",
139+
"path": "../../."
140+
}
141+
]
142+
}
143+
]
144+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{
2+
"id": "be.alexandervanhee.gradia.Plugin.ocr",
3+
"runtime": "be.alexandervanhee.gradia",
4+
"sdk": "org.gnome.Sdk//49",
5+
"separate-locales": false,
6+
"appstream-compose": false,
7+
"build-extension": true,
8+
"build-options": {
9+
"prefix": "/app/extensions/ocr",
10+
"prepend-pkg-config-path": "/app/extensions/ocr/lib/pkgconfig",
11+
"prepend-ld-library-path": "/app/extensions/ocr/lib",
12+
"env": {
13+
"TESSDATA_PREFIX": "/app/extensions/ocr/share/tessdata"
14+
}
15+
},
16+
"cleanup": [
17+
"/include",
18+
"/lib/pkgconfig",
19+
"/man",
20+
"/share/doc",
21+
"/share/gtk-doc",
22+
"/share/man",
23+
"/share/pkgconfig",
24+
"/lib/cmake",
25+
"*.la",
26+
"*.a"
27+
],
28+
"modules": [
29+
{
30+
"name": "leptonica",
31+
"buildsystem": "autotools",
32+
"config-opts": [
33+
"--disable-programs",
34+
"--disable-static"
35+
],
36+
"sources": [
37+
{
38+
"type": "archive",
39+
"url": "https://github.com/DanBloomberg/leptonica/releases/download/1.85.0/leptonica-1.85.0.tar.gz",
40+
"sha256": "3745ae3bf271a6801a2292eead83ac926e3a9bc1bf622e9cd4dd0f3786e17205"
41+
}
42+
]
43+
},
44+
{
45+
"name": "tesseract",
46+
"buildsystem": "cmake-ninja",
47+
"builddir": true,
48+
"config-opts": [
49+
"-DCMAKE_BUILD_TYPE=Release",
50+
"-DBUILD_TRAINING_TOOLS=OFF",
51+
"-DUSE_SYSTEM_ICU=ON",
52+
"-DCMAKE_INSTALL_PREFIX=/app/extensions/ocr",
53+
"-DCMAKE_INSTALL_RPATH=/app/extensions/ocr/lib"
54+
],
55+
"sources": [
56+
{
57+
"type": "archive",
58+
"url": "https://github.com/tesseract-ocr/tesseract/archive/5.5.1.tar.gz",
59+
"sha256": "a7a3f2a7420cb6a6a94d80c24163e183cf1d2f1bed2df3bbc397c81808a57237"
60+
}
61+
]
62+
},
63+
{
64+
"name": "tesseract-language-data",
65+
"buildsystem": "simple",
66+
"build-commands": [
67+
"mkdir -p ${FLATPAK_DEST}/share/tessdata",
68+
"install -Dm644 *.traineddata ${FLATPAK_DEST}/share/tessdata/",
69+
"install -Dm644 be.alexandervanhee.gradia.Plugin.ocr.metainfo.xml -t ${FLATPAK_DEST}/share/metainfo"
70+
],
71+
"sources": [
72+
{
73+
"type": "file",
74+
"url": "https://github.com/tesseract-ocr/tessdata_best/raw/4.1.0/eng.traineddata",
75+
"sha256": "8280aed0782fe27257a68ea10fe7ef324ca0f8d85bd2fd145d1c2b560bcb66ba",
76+
"dest-filename": "eng.traineddata"
77+
},
78+
{
79+
"type": "file",
80+
"path": "be.alexandervanhee.gradia.Plugin.ocr.metainfo.xml"
81+
}
82+
]
83+
}
84+
]
85+
}
86+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<component type="addon">
3+
<id>be.alexandervanhee.gradia.Plugin.ocr</id>
4+
<extends>be.alexandervanhee.gradia</extends>
5+
<name>Gradia OCR Extension</name>
6+
<summary>OCR extension for Gradia</summary>
7+
<description>
8+
<p>
9+
This extension adds OCR functionality to Gradia using Tesseract OCR engine. It includes English training data.
10+
</p>
11+
</description>
12+
<project_license>Apache-2.0</project_license>
13+
<metadata_license>CC0-1.0</metadata_license>
14+
<developer id="be.alexandervanhee">
15+
<name>Alexander Vanhee</name>
16+
</developer>
17+
<url type="homepage">https://github.com/alexandervanhee/gradia</url>
18+
</component>

build-aux/flatpak/be.alexandervanhee.gradia.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
"--share=network",
1414
"--own-name=be.alexandervanhee.gradia"
1515
],
16+
"add-extensions": {
17+
"be.alexandervanhee.gradia.Plugin.ocr": {
18+
"directory": "extensions/ocr",
19+
"subdirectories": true,
20+
"no-autodownload": true,
21+
"autodelete": true
22+
}
23+
},
1624
"cleanup": [
1725
"/include",
1826
"/lib/pkgconfig",
@@ -24,6 +32,9 @@
2432
"*.la",
2533
"*.a"
2634
],
35+
"cleanup-commands": [
36+
"mkdir -p ${FLATPAK_DEST}/extensions/ocr"
37+
],
2738
"modules": [
2839
"pypi-dependencies.json",
2940
{
@@ -65,7 +76,7 @@
6576
"builddir": true,
6677
"buildsystem": "meson",
6778
"config-opts": [
68-
"-Dbuildtype=debug"
79+
"-Dbuildtype=release"
6980
],
7081
"sources": [
7182
{

build-aux/flatpak/pypi-dependencies.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22
"name": "python3-Pillow",
33
"buildsystem": "simple",
44
"build-commands": [
5-
"pip3 install --verbose --exists-action=i --no-index --find-links=\"file://${PWD}\" --prefix=${FLATPAK_DEST} \"Pillow\" --no-build-isolation"
5+
"pip3 install --verbose --exists-action=i --no-index --find-links=\"file://${PWD}\" --prefix=${FLATPAK_DEST} \"Pillow\" \"pytesseract\" --no-build-isolation"
66
],
77
"sources": [
88
{
99
"type": "file",
1010
"url": "https://files.pythonhosted.org/packages/source/p/pillow/pillow-11.3.0.tar.gz",
1111
"sha256": "3828ee7586cd0b2091b6209e5ad53e20d0649bbe87164a459d0676e035e8f523"
12+
},
13+
{
14+
"type": "file",
15+
"url": "https://files.pythonhosted.org/packages/source/p/pytesseract/pytesseract-0.3.13.tar.gz",
16+
"sha256": "4bf5f880c99406f52a3cfc2633e42d9dc67615e69d8a509d74867d3baddb5db9"
1217
}
1318
]
1419
}

data/be.alexandervanhee.gradia.gschema.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@
118118
<default>'Adwaita'</default>
119119
<summary>Color scheme of the source snippet</summary>
120120
</key>
121-
121+
<key name="trained-data" type="s">
122+
<default>'eng'</default>
123+
<summary>Last used OCR Language</summary>
124+
</key>
122125
</schema>
123126
</schemalist>
124127

data/gradia.gresource.xml.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
<file preprocess="xml-stripblanks">ui/confirm_close_dialog.ui</file>
2121
<file preprocess="xml-stripblanks">ui/image_generator_window.ui</file>
2222
<file preprocess="xml-stripblanks">ui/gradient_step_dialog.ui</file>
23+
<file preprocess="xml-stripblanks">ui/ocr_dialog.ui</file>
24+
<file preprocess="xml-stripblanks">ui/preferences/ocr_model_page.ui</file>
2325
<file>style.css</file>
2426
<file>style-dark.css</file>
2527
<file>style-hc.css</file>
@@ -81,6 +83,7 @@
8183
<file preprocess="xml-stripblanks">icons/scalable/actions/fill-symbolic.svg</file>
8284
<file preprocess="xml-stripblanks">icons/scalable/actions/outline-color-symbolic.svg</file>
8385
<file preprocess="xml-stripblanks">icons/scalable/actions/engineering-symbolic.svg</file>
86+
<file preprocess="xml-stripblanks">icons/scalable/actions/scanner-symbolic.svg</file>
8487
</gresource>
8588
</gresources>
8689

Lines changed: 7 additions & 0 deletions
Loading

data/style.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,12 @@ image.symbolic-circular {
260260
border-top-right-radius: 8px;
261261
}
262262

263+
.semi-transparent {
264+
opacity: 0.8;
265+
transition: opacity 200ms ease;
266+
}
267+
268+
.semi-transparent:hover, .semi-transparent:active{
269+
opacity: 1;
270+
}
271+

0 commit comments

Comments
 (0)