| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
import os |
| 20 |
import subprocess |
| 21 |
import sys |
| 22 |
import time |
| 23 |
import shutil |
| 24 |
import re |
| 25 |
from functools import wraps |
| 26 |
|
| 27 |
from lios import scanner, editor, imageview, cam, ocr, preferences, speech |
| 28 |
from lios.ui.gtk import widget, containers, loop, menu, \ |
| 29 |
window, icon_view, dialog, about |
| 30 |
|
| 31 |
from lios.ui.gtk.file_chooser import FileChooserDialog |
| 32 |
from lios import macros |
| 33 |
from lios import preferences |
| 34 |
from lios import capture_screen |
| 35 |
from lios import dictionary |
| 36 |
from lios import localization |
| 37 |
_ = localization._ |
| 38 |
|
| 39 |
import multiprocessing |
| 40 |
import threading |
| 41 |
|
| 42 |
import webbrowser |
| 43 |
|
| 44 |
def on_thread(function): |
| 45 |
@wraps(function) |
| 46 |
def inner(*args): |
| 47 |
print(function.__name__+" Started") |
| 48 |
|
| 49 |
threading.Thread(target=function,args=args).start() |
| 50 |
return inner |
| 51 |
|
| 52 |
|
| 53 |
class linux_intelligent_ocr_solution(): |
| 54 |
def __init__ (self,file_list=[]): |
| 55 |
try: |
| 56 |
os.mkdir(macros.tmp_dir) |
| 57 |
except: |
| 58 |
pass |
| 59 |
|
| 60 |
try: |
| 61 |
os.mkdir(macros.config_dir) |
| 62 |
except: |
| 63 |
pass |
| 64 |
|
| 65 |
try: |
| 66 |
os.mkdir(macros.bookmarks_dir) |
| 67 |
except: |
| 68 |
pass |
| 69 |
|
| 70 |
|
| 71 |
scroll_box_iconview = containers.ScrollBox() |
| 72 |
self.iconview = icon_view.IconView() |
| 73 |
self.iconview.set_vexpand(True) |
| 74 |
scroll_box_iconview.add(self.iconview) |
| 75 |
box_iconview = containers.Box(containers.Box.VERTICAL) |
| 76 |
toolbar_iconview = containers.Toolbar(containers.Toolbar.HORIZONTAL, |
| 77 |
[(_('Open'),self.open_files),(_("Take-Screenshot"),self.take_rectangle_screenshot), |
| 78 |
(_("Scan-Using-Webcam"),self.scan_using_cam),(_("Recognize"),self.ocr_selected_images), |
| 79 |
(_("Clear"),self.iconview_remove_all_images),]) |
| 80 |
|
| 81 |
self.context_menu_iconview = menu.ContextMenu([ |
| 82 |
(_("Recognize"),self.ocr_selected_images), |
| 83 |
(_("Recognize-With-Rotation"),self.ocr_selected_images_with_rotation), |
| 84 |
menu.SEPARATOR, |
| 85 |
(_("Rotate-Left"),self.rotate_selected_images_to_left), |
| 86 |
(_("Rotate-Twice"),self.rotate_selected_images_to_twice), |
| 87 |
(_("Rotate-Right"),self.rotate_selected_images_to_right), |
| 88 |
menu.SEPARATOR, (_("Invert-List"),self.iconview.invert_list), menu.SEPARATOR, |
| 89 |
(_("Save"),self.save_selected_images), |
| 90 |
(_("Export-As-Pdf"),self.save_selected_images_as_pdf), |
| 91 |
menu.SEPARATOR, |
| 92 |
(_("Delete"),self.iconview_remove_selected_images)]) |
| 93 |
|
| 94 |
self.iconview.connect_on_selected_callback(self.on_iconview_item_selected) |
| 95 |
self.iconview.connect_context_menu_button_callback(self.iconview_popup_context_menu) |
| 96 |
box_iconview.add(toolbar_iconview) |
| 97 |
box_iconview.add(scroll_box_iconview) |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
self.imageview = imageview.ImageViewer() |
| 102 |
self.imageview.set_label_entry_visible(False) |
| 103 |
self.imageview.connect("list_updated",self.list_updated_event_handler); |
| 104 |
self.imageview.set_vexpand(True) |
| 105 |
self.imageview.set_hexpand(True) |
| 106 |
box_imageview = containers.Box(containers.Box.HORIZONTAL) |
| 107 |
toolbar_imageview = containers.Toolbar(containers.Toolbar.VERTICAL, |
| 108 |
[(_("Rotate-Right"),self.rotate_current_images_to_right), |
| 109 |
(_("Rotate-Twice"),self.rotate_current_images_to_twice), |
| 110 |
(_("Rotate-Left"),self.rotate_current_images_to_left),containers.Toolbar.SEPARATOR, |
| 111 |
(_("Zoom-In"),self.imageview.zoom_in),(_("Zoom-Fit"),self.imageview.zoom_fit), |
| 112 |
(_("Zoom-Out"),self.imageview.zoom_out),containers.Toolbar.SEPARATOR, |
| 113 |
(_("Recognize-Selected-Areas"),self.ocr_selected_areas), |
| 114 |
(_("Recognize-Current-Image"),self.ocr_current_image), |
| 115 |
(_("Recognize-Current-Image-With-Rotation"),self.ocr_current_image_with_rotation)]); |
| 116 |
|
| 117 |
box_imageview.add(toolbar_imageview) |
| 118 |
box_imageview.add(self.imageview) |
| 119 |
self.imageview.load_image(macros.logo_file,[],imageview.ImageViewer.ZOOM_FIT) |
| 120 |
|
| 121 |
self.context_menu_imageview = menu.ContextMenu( |
| 122 |
[(_("Recognize-Current-Image"),self.ocr_current_image), |
| 123 |
(_("Recognize-Selected-Areas"),self.ocr_selected_areas), |
| 124 |
(_("Recognize-Current-Image-With-Rotation"),self.ocr_current_image_with_rotation),menu.SEPARATOR, |
| 125 |
(_("Rotate-Right"),self.rotate_current_images_to_right), |
| 126 |
(_("Rotate-Twice"),self.rotate_current_images_to_twice), |
| 127 |
(_("Rotate-Left"),self.rotate_current_images_to_left),menu.SEPARATOR, |
| 128 |
(_("Zoom-In"),self.imageview.zoom_in),(_("Zoom-Fit"),self.imageview.zoom_fit), |
| 129 |
(_("Zoom-Out"),self.imageview.zoom_out), menu.SEPARATOR, |
| 130 |
(_("Save-Selected-Areas"),self.save_selected_areas), |
| 131 |
]); |
| 132 |
self.imageview.connect_context_menu_button_callback(self.imageview_popup_context_menu) |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
self.textview = editor.BasicTextView() |
| 137 |
self.textview.set_vexpand(True) |
| 138 |
self.textview.set_hexpand(True) |
| 139 |
self.textview.set_accepts_tab(False) |
| 140 |
box_editor = containers.Box(containers.Box.HORIZONTAL) |
| 141 |
toolbar_editor = containers.Toolbar(containers.Toolbar.VERTICAL, |
| 142 |
[(_("New"),self.new),(_('Open'),self.open_files), |
| 143 |
(_("Save"),self.textview.save),containers.Toolbar.SEPARATOR, |
| 144 |
(_("Spell-Check"),self.textview.open_spell_check),containers.Toolbar.SEPARATOR, |
| 145 |
(_("Undo"),self.textview.undo),(_("Redo"),self.textview.redo), |
| 146 |
containers.Toolbar.SEPARATOR, |
| 147 |
(_("Find"),self.textview.open_find_dialog), |
| 148 |
(_("Find-Replace"),self.textview.open_find_and_replace_dialog), |
| 149 |
containers.Toolbar.SEPARATOR, |
| 150 |
(_("Start-Reader"),self.start_reader), |
| 151 |
(_("Stop-Reader"),self.stop_reader), |
| 152 |
containers.Toolbar.SEPARATOR, |
| 153 |
(_("Go-To-Line"),self.textview.go_to_line), |
| 154 |
(_("Go-To-Page"),self.go_to_page), |
| 155 |
|
| 156 |
]) |
| 157 |
box_editor.add(toolbar_editor) |
| 158 |
scroll_box_editor = containers.ScrollBox() |
| 159 |
scroll_box_editor.add(self.textview) |
| 160 |
box_editor.add(scroll_box_editor) |
| 161 |
|
| 162 |
|
| 163 |
if(not self.textview.set_text_cleaner_list_from_file(macros.local_text_cleaner_list_file_path)): |
| 164 |
self.textview.set_text_cleaner_list_from_file(macros.default_text_cleaner_list_file_path) |
| 165 |
self.textview.save_text_cleaner_list_to_file(macros.local_text_cleaner_list_file_path) |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
self.available_ocr_engine_list = ocr.get_available_engines() |
| 171 |
|
| 172 |
|
| 173 |
self.available_scanner_driver_list = scanner.get_available_drivers() |
| 174 |
self.scanner_objects = [] |
| 175 |
self.is_updating_scanner_list = False |
| 176 |
|
| 177 |
|
| 178 |
loop.threads_init() |
| 179 |
|
| 180 |
|
| 181 |
self.preferences = preferences.lios_preferences() |
| 182 |
self.preferences.set_from_file(macros.preferences_file_path) |
| 183 |
self.preferences.set_default_speech_module_and_language() |
| 184 |
self.preferences.set_avalable_scanner_drivers([ item.name for item in self.available_scanner_driver_list]) |
| 185 |
self.preferences.set_avalable_ocr_engines([ (item.name, item.get_available_languages(),item.support_multiple_languages()) |
| 186 |
for item in self.available_ocr_engine_list ]) |
| 187 |
|
| 188 |
|
| 189 |
menubar = menu.MenuBar( |
| 190 |
[[_("_File"),(_("New"),self.textview.new,"<Control>N"),menu.SEPARATOR, |
| 191 |
(_("Open"),self.open_files,"<Control>O"), |
| 192 |
(_("Save"),self.textview.save,"<Control>S"),(_("Save As"),self.textview.save_as,"<Shift><Control>N"), |
| 193 |
(_("Export Text As Pdf"),self.textview.print_to_pdf,"<Control>E"),(_("Print"),self.textview.open_print_dialog,"None"), |
| 194 |
(_("Print Preview"),self.textview.print_preview,"None"),menu.SEPARATOR, |
| 195 |
(_("Quit"),self.quit,"<Control>Q")], |
| 196 |
[_("_Edit"),(_("Undo"),self.textview.undo,"<Control>Z"),(_("Redo"),self.textview.redo,"<Control>Y"), |
| 197 |
menu.SEPARATOR, |
| 198 |
(_("Punch Text"),self.textview.punch,"None"),(_("Append Text"),self.textview.append,"None"), |
| 199 |
menu.SEPARATOR,(_("Find"),self.textview.open_find_dialog,"<Control>F"), |
| 200 |
(_("Find Replace"),self.textview.open_find_and_replace_dialog,"<Control>R") |
| 201 |
,menu.SEPARATOR, |
| 202 |
(_("Spell Check"),self.textview.open_spell_check,"<Control>F7"), |
| 203 |
menu.SEPARATOR, |
| 204 |
(_("Go To Line"),self.textview.go_to_line,"<Control>L"),(_("Go To Page"),self.go_to_page,"<Control>G"), |
| 205 |
menu.SEPARATOR,(_("Preferences"),self.open_preferences_general_page,"<Control>P")], |
| 206 |
[_("_Image"),[_("Rotate Left"),(_("Current"),self.rotate_current_images_to_left,"None"), |
| 207 |
(_("Selected"),self.rotate_selected_images_to_left,"None"), |
| 208 |
(_("All"),self.rotate_all_images_to_left,"None")], |
| 209 |
[_("Rotate Twice"),(_("Current"),self.rotate_current_images_to_twice,"None"), |
| 210 |
(_("Selected"),self.rotate_selected_images_to_twice,"None"), |
| 211 |
(_("All"),self.rotate_all_images_to_twice,"None")], |
| 212 |
[_("Rotate Right"),(_("Current"),self.rotate_current_images_to_right,"None"), |
| 213 |
(_("Selected"),self.rotate_selected_images_to_right,"None"), |
| 214 |
(_("All"),self.rotate_all_images_to_right,"None")], |
| 215 |
menu.SEPARATOR, (_("Invert List"),self.iconview.invert_list,"None"), |
| 216 |
menu.SEPARATOR, |
| 217 |
[_("Save"),(_("Selected Images"),self.save_selected_images,"None"), |
| 218 |
(_("All Images"),self.save_all_images,"None")], |
| 219 |
[_("Export As Pdf"),(_("Selected Images"),self.save_selected_images_as_pdf,"None"), |
| 220 |
(_("All Images"),self.save_all_images_as_pdf,"None")], menu.SEPARATOR, |
| 221 |
[_("Delete"),(_("Selected Images"),self.iconview_remove_selected_images,"None"), |
| 222 |
(_("All Images"),self.iconview_remove_all_images,"None")],], |
| 223 |
[_("_Scan"),(_("Update Scanner List"),self.update_scanner_list,"None"), |
| 224 |
(_("Scan Image"),self.scan_single_image,"F8"), |
| 225 |
(_("Scan Image Repeatedly"),self.scan_image_repeatedly,"<Control>F8"), |
| 226 |
(_("Scan and Ocr"),self.scan_and_ocr,"F9"), |
| 227 |
(_("Scan and Ocr Repeatedly"),self.scan_and_ocr_repeatedly,"<Control>F9"), |
| 228 |
(_("Optimize Scanner Brightness"),self.optimize_brightness,"None"),menu.SEPARATOR, |
| 229 |
(_("Scan Using Webcam"),self.scan_using_cam,"F6"),menu.SEPARATOR, |
| 230 |
[_("Take Screenshot"), |
| 231 |
(_("Selection"),self.take_rectangle_screenshot,"<Control>F2"), |
| 232 |
(_("Full"),self.take_full_screenshot,"F2")], |
| 233 |
[_("Take and Recognize Screenshot"), |
| 234 |
(_("Selection"),self.take_and_recognize_rectangle_screenshot,"<Control>F3"), |
| 235 |
(_("Full"),self.take_and_recognize_full_screenshot,"F3")]], |
| 236 |
[_("_Recognize"), |
| 237 |
(_("Recognize Current Image"),self.ocr_current_image,"None"), |
| 238 |
(_("Recognize Current Image With Rotation"),self.ocr_current_image_with_rotation,"None"), |
| 239 |
(_("Recognize Selected Areas"),self.ocr_selected_areas,"None"), |
| 240 |
(_("Recognize Selected Images"),self.ocr_selected_images,"None"), |
| 241 |
(_("Recognize All Images"),self.ocr_all_images,"None"), |
| 242 |
(_("Recognize Selected with rotation"),self.ocr_selected_images_with_rotation,"None"), |
| 243 |
(_("Recognize All with rotation"),self.ocr_all_images_with_rotation,"None")], |
| 244 |
[_("_Tools"),(_("Spell Check"),self.textview.open_spell_check,"<Control>F7"), |
| 245 |
[_("Text Cleaner"), |
| 246 |
(_("Text Cleaner"),self.textview.open_text_cleaner,"None"), |
| 247 |
(_("Import"),self.textview.import_text_cleaner_list,"None"), |
| 248 |
(_("Export"),self.textview.export_text_cleaner_list,"None"), |
| 249 |
(_("Apply From Cursor"),self.textview.apply_text_cleaner_from_cursor,"None"), |
| 250 |
(_("Apply Entire"),self.textview.apply_text_cleaner_entire_text,"None")], |
| 251 |
(_("Audio Converter"),self.audio_converter,"None"), |
| 252 |
(_("Dictionary"),self.artha,"<Control><Alt>W"), |
| 253 |
(_("Bookmark"),self.textview.create_bookmark,"<Control>B"), |
| 254 |
(_("Bookmark Table"),self.textview.open_bookmark_table,"<Alt>B"), |
| 255 |
(_("Import Bookmarks"),self.textview.import_bookmarks_from_file,"None"), |
| 256 |
(_("Bookmark Table Complete"),self.textview.open_all_bookmark_table,"<Super>B"), |
| 257 |
(_("Start Reader"),self.start_reader,"F5"), |
| 258 |
(_("Stop Reader"),self.stop_reader,"<Control>F5"), |
| 259 |
(_("Increase Reader Speed"),self.increase_reader_speed,"<Ctrl>Prior"), |
| 260 |
(_("Decrease Reader Speed"),self.decrease_reader_speed,"<Ctrl>Next"), |
| 261 |
(_("Stop All Process"),self.stop_all_process,"<Control>F4")], |
| 262 |
[_("_Preferences"),(_("Preferences General"),self.open_preferences_general_page,"None"), |
| 263 |
(_("Preferences Recognition"),self.open_preferences_recognition_page,"None"), |
| 264 |
(_("Preferences Scanning"),self.open_preferences_scanning_page,"None"), |
| 265 |
menu.SEPARATOR, (_("Save"),self.save_preferences,"None"), |
| 266 |
(_("Load"),self.load_preferences,"None"), |
| 267 |
(_("Restore"),self.restore_preferences,"None")], |
| 268 |
[_("Help"),(_("Open Readme"),self.open_readme,"None"), |
| 269 |
(_("Video Tutorials"),self.open_video_tutorials,"None"), |
| 270 |
(_("Open Home Page"),self.open_home_page,"None"), |
| 271 |
(_("Get Source Code"),self.get_source_code,"None"), |
| 272 |
menu.SEPARATOR,(_("About"),self.about,"None")]]) |
| 273 |
menubar.show() |
| 274 |
|
| 275 |
self.combobox_scanners = widget.ComboBox() |
| 276 |
button_update_scanner_list = widget.Button(_("Update Scanner List")) |
| 277 |
button_update_scanner_list.connect_function(self.update_scanner_list) |
| 278 |
button_scan = widget.Button(_("Scan")) |
| 279 |
button_scan.connect_function(self.scan_single_image) |
| 280 |
toolbar_main = containers.Toolbar(containers.Toolbar.HORIZONTAL, |
| 281 |
[(_("Preferences"),self.open_preferences_general_page), |
| 282 |
(_("Video Tutorials"),self.open_video_tutorials), |
| 283 |
(_("About"),self.about), |
| 284 |
(_("Quit"),self.quit)]) |
| 285 |
|
| 286 |
|
| 287 |
self.paned_image_text = containers.Paned(containers.Paned.VERTICAL) |
| 288 |
self.paned_image_text.add1(box_imageview) |
| 289 |
self.paned_image_text.add2(box_editor) |
| 290 |
|
| 291 |
self.paned_main = containers.Paned(containers.Paned.HORIZONTAL) |
| 292 |
self.paned_main.add1(box_iconview) |
| 293 |
self.paned_main.add2(self.paned_image_text) |
| 294 |
|
| 295 |
self.statusbar = widget.Statusbar() |
| 296 |
self.progressbar = widget.ProgressBar() |
| 297 |
|
| 298 |
self.window = window.Window(macros.app_name) |
| 299 |
self.window.set_taskbar_icon(macros.logo_file) |
| 300 |
|
| 301 |
grid_main = containers.Grid() |
| 302 |
grid_main.add_widgets([ |
| 303 |
(menubar,5,1,containers.Grid.HEXPAND,containers.Grid.NO_VEXPAND),containers.Grid.NEW_ROW, |
| 304 |
(self.combobox_scanners,1,1,containers.Grid.HEXPAND,containers.Grid.NO_VEXPAND), |
| 305 |
(button_update_scanner_list,1,1,containers.Grid.HEXPAND,containers.Grid.NO_VEXPAND), |
| 306 |
(button_scan,1,1,containers.Grid.HEXPAND,containers.Grid.NO_VEXPAND), |
| 307 |
(toolbar_main,2,1,containers.Grid.HEXPAND,containers.Grid.NO_VEXPAND,containers.Grid.ALIGN_END), |
| 308 |
containers.Grid.NEW_ROW, |
| 309 |
(self.paned_main,5,1), |
| 310 |
containers.Grid.NEW_ROW, |
| 311 |
(self.statusbar,4,1,containers.Grid.HEXPAND,containers.Grid.NO_VEXPAND), |
| 312 |
(self.progressbar,1,1,containers.Grid.HEXPAND,containers.Grid.NO_VEXPAND),]) |
| 313 |
|
| 314 |
|
| 315 |
grid_main.set_focus_chain([self.combobox_scanners,button_update_scanner_list,button_scan, self.paned_main ]) |
| 316 |
self.paned_main.set_focus_chain([box_iconview, self.paned_image_text]) |
| 317 |
self.paned_image_text.set_focus_chain([self.textview]) |
| 318 |
box_iconview.set_focus_chain([self.iconview]) |
| 319 |
|
| 320 |
if(len(file_list) > 0 ): |
| 321 |
self.open_list_of_files(file_list) |
| 322 |
try: |
| 323 |
self.textview.save_file_name |
| 324 |
except: |
| 325 |
try: |
| 326 |
file = open(macros.recent_file_path,encoding="utf-8") |
| 327 |
self.textview.set_text(file.read()) |
| 328 |
file2 = open(macros.recent_cursor_position_file_path,encoding="utf-8") |
| 329 |
self.textview.move_cursor_to_line(int(file2.read())) |
| 330 |
except: |
| 331 |
pass |
| 332 |
else: |
| 333 |
try: |
| 334 |
file = open(macros.recent_file_path,encoding="utf-8") |
| 335 |
self.textview.set_text(file.read()) |
| 336 |
file2 = open(macros.recent_cursor_position_file_path,encoding="utf-8") |
| 337 |
self.textview.move_cursor_to_line(int(file2.read())) |
| 338 |
except: |
| 339 |
pass |
| 340 |
|
| 341 |
|
| 342 |
self.old_language = -1 |
| 343 |
self.old_scan_driver = self.preferences.scan_driver; |
| 344 |
self.old_scanner_mode_switching = self.preferences.scanner_mode_switching |
| 345 |
|
| 346 |
|
| 347 |
self.make_preferences_effective() |
| 348 |
|
| 349 |
|
| 350 |
self.is_reading = False |
| 351 |
self.reader_stop_pressed = False |
| 352 |
|
| 353 |
self.notify_information(_("Welcome to {} Version {}").format(macros.app_name,macros.version),0) |
| 354 |
|
| 355 |
|
| 356 |
self.window.connect_menubar(menubar) |
| 357 |
|
| 358 |
self.window.connect_configure_event_handler(self.window_reconfigure) |
| 359 |
self.window.connect_close_function(self.quit) |
| 360 |
grid_main.show_all() |
| 361 |
self.window.add(grid_main) |
| 362 |
self.window.maximize() |
| 363 |
self.textview.grab_focus() |
| 364 |
self.window.show() |
| 365 |
loop.start_main_loop() |
| 366 |
|
| 367 |
|
| 368 |
def notify_information(self,text,percentage = -1): |
| 369 |
self.statusbar.set_text(text) |
| 370 |
if(percentage == -1): |
| 371 |
self.progressbar.set_pulse_mode(True) |
| 372 |
self.progressbar.set_pulse_step(0.030) |
| 373 |
else: |
| 374 |
self.progressbar.set_pulse_mode(False) |
| 375 |
self.progressbar.set_fraction(percentage) |
| 376 |
|
| 377 |
def list_updated_event_handler(self,*data): |
| 378 |
filename = self.imageview.get_filename() |
| 379 |
if(filename == macros.logo_file): |
| 380 |
self.imageview.clear_selection(None) |
| 381 |
else: |
| 382 |
file = open(filename+".box","w") |
| 383 |
for item in self.imageview.get_list(): |
| 384 |
file.write("{0} {1} {2} {3} {4} 0\n".format(str(item[0]), |
| 385 |
str(int(item[1])),str(int(item[2])), |
| 386 |
str(int(item[3])),str(int(item[4])))) |
| 387 |
|
| 388 |
@on_thread |
| 389 |
def save_selected_areas(self,*data): |
| 390 |
for item in self.imageview.get_selection_list(): |
| 391 |
destination = self.get_feesible_filename_from_filename("{}{}.png".format(macros.tmp_dir,self.preferences.starting_page_number)) |
| 392 |
self.imageview.save_sub_image(destination,item[0],item[1],item[2],item[3]) |
| 393 |
loop.acquire_lock() |
| 394 |
self.iconview.add_item(destination) |
| 395 |
loop.release_lock() |
| 396 |
self.preferences.update_page_number() |
| 397 |
|
| 398 |
def new(self,*data): |
| 399 |
if(self.textview.new()): |
| 400 |
self.preferences.starting_page_number = 1 |
| 401 |
with open(macros.recent_file_path,"w") as file: |
| 402 |
file.write("") |
| 403 |
with open(macros.recent_cursor_position_file_path,"w") as file: |
| 404 |
file.write("0") |
| 405 |
|
| 406 |
def audio_converter(self,*data): |
| 407 |
self.textview.audio_converter(voice=self.preferences.speech_language) |
| 408 |
|
| 409 |
def go_to_page(self,*data): |
| 410 |
spinbutton_page = widget.SpinButton(0,0,self.preferences.starting_page_number,1,5,0) |
| 411 |
dlg = dialog.Dialog(_("Go to page"),(_("Go"), dialog.Dialog.BUTTON_ID_1,_("Close!"), dialog.Dialog.BUTTON_ID_2)) |
| 412 |
dlg.add_widget_with_label(spinbutton_page,_("Page Number: ")) |
| 413 |
spinbutton_page.grab_focus() |
| 414 |
dlg.show_all() |
| 415 |
response = dlg.run() |
| 416 |
if response == dialog.Dialog.BUTTON_ID_1: |
| 417 |
to_go = spinbutton_page.get_value() |
| 418 |
|
| 419 |
self.textview.move_cursor_to_line(1) |
| 420 |
if (self.preferences.page_numbering_type == 0): |
| 421 |
word = "Page-{0}".format(to_go) |
| 422 |
else: |
| 423 |
if (to_go % 2 == 0): |
| 424 |
word = "Page-{0}-{1}".format(to_go-1,to_go) |
| 425 |
else: |
| 426 |
word = "Page-{0}-{1}".format(to_go,to_go+1) |
| 427 |
if(not self.textview.move_forward_to_word(word)): |
| 428 |
self.textview.move_backward_to_word(word) |
| 429 |
dlg.destroy() |
| 430 |
else: |
| 431 |
dlg.destroy() |
| 432 |
|
| 433 |
def open_video_tutorials(self,*data): |
| 434 |
webbrowser.open(macros.video_tutorials_link) |
| 435 |
|
| 436 |
def open_home_page(self,*data): |
| 437 |
webbrowser.open(macros.home_page_link) |
| 438 |
|
| 439 |
def get_source_code(self,*data): |
| 440 |
webbrowser.open(macros.source_link) |
| 441 |
|
| 442 |
def iconview_popup_context_menu(self,*data): |
| 443 |
if (self.iconview.get_selected_item_names() == []): |
| 444 |
self.iconview.select_all() |
| 445 |
if (self.iconview.get_selected_item_names() != []): |
| 446 |
self.context_menu_iconview.pop_up() |
| 447 |
|
| 448 |
def imageview_popup_context_menu(self,*data): |
| 449 |
self.context_menu_imageview.pop_up() |
| 450 |
|
| 451 |
def window_reconfigure(self,*arg): |
| 452 |
width,height = self.window.get_size() |
| 453 |
self.paned_image_text.set_position(height/2) |
| 454 |
self.paned_main.set_position(200) |
| 455 |
self.imageview.set_position(width-500) |
| 456 |
|
| 457 |
|
| 458 |
def get_feesible_filename_from_filename(self,filename): |
| 459 |
if (os.path.exists(re.sub('[^.-/#0-9a-zA-Z]+', '#', filename))): |
| 460 |
return self.get_feesible_filename_from_filename(filename.replace('.','#.')) |
| 461 |
else: |
| 462 |
return re.sub('[^.-/#0-9a-zA-Z]+', '#', filename) |
| 463 |
|
| 464 |
def add_image_to_list(self,file_name_with_directory,destination,move,lock=False): |
| 465 |
if (move): |
| 466 |
shutil.move(file_name_with_directory,destination) |
| 467 |
else: |
| 468 |
shutil.copyfile(file_name_with_directory,destination) |
| 469 |
self.iconview.add_item(destination) |
| 470 |
self.iconview.select_item(destination) |
| 471 |
|
| 472 |
@on_thread |
| 473 |
def import_images_from_pdf(self,pdf_filename_full): |
| 474 |
|
| 475 |
pdf_filename = pdf_filename_full.split("/")[-1:][0] |
| 476 |
filename = pdf_filename.split(".")[0] |
| 477 |
pdf_filename = re.sub('[^.-/#0-9a-zA-Z]+', '#', pdf_filename) |
| 478 |
destination = "{0}{1}".format(macros.tmp_dir,pdf_filename) |
| 479 |
shutil.copyfile(pdf_filename_full,destination) |
| 480 |
os.makedirs(destination.split(".")[0],exist_ok=True) |
| 481 |
|
| 482 |
self.notify_information(_("Extracting images from Pdf")) |
| 483 |
|
| 484 |
p = multiprocessing.Process(target=lambda : os.system("pdftoppm {} {}/{} -png" |
| 485 |
.format(destination,destination.split(".")[0],pdf_filename.split(".")[0])) , args=()) |
| 486 |
|
| 487 |
p.start() |
| 488 |
while(p.is_alive()): |
| 489 |
pass |
| 490 |
os.remove(destination) |
| 491 |
|
| 492 |
file_list = os.listdir(destination.split(".")[0]) |
| 493 |
file_list = sorted(file_list) |
| 494 |
|
| 495 |
recently_added_list = [] |
| 496 |
|
| 497 |
for image in file_list: |
| 498 |
if(len(image.split("."))>1): |
| 499 |
if (image.split(".")[1] in macros.supported_image_formats): |
| 500 |
filename = "{}{}".format(macros.tmp_dir,image) |
| 501 |
filename = self.get_feesible_filename_from_filename(filename) |
| 502 |
loop.acquire_lock() |
| 503 |
self.add_image_to_list("{}/{}".format(destination.split(".")[0],image),filename,True) |
| 504 |
loop.release_lock() |
| 505 |
recently_added_list.append(filename) |
| 506 |
os.rmdir(destination.split(".")[0]) |
| 507 |
self.notify_information(_("Completed!"),0) |
| 508 |
|
| 509 |
self.recognize_recently_added_images(recently_added_list) |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
@on_thread |
| 514 |
def take_full_screenshot(self,data): |
| 515 |
destination = self.get_feesible_filename_from_filename("{}{}.png".format(macros.tmp_dir,self.preferences.starting_page_number)) |
| 516 |
self.window.iconify() |
| 517 |
os.system("sleep 1") |
| 518 |
capture_screen.capture_entire_screen(destination) |
| 519 |
loop.acquire_lock() |
| 520 |
self.iconview.add_item(destination) |
| 521 |
loop.release_lock() |
| 522 |
self.preferences.update_page_number() |
| 523 |
|
| 524 |
@on_thread |
| 525 |
def take_rectangle_screenshot(self,data): |
| 526 |
destination = self.get_feesible_filename_from_filename("{}{}.png".format(macros.tmp_dir,self.preferences.starting_page_number)) |
| 527 |
self.window.iconify() |
| 528 |
capture_screen.capture_rectangle_selection(destination) |
| 529 |
loop.acquire_lock() |
| 530 |
self.iconview.add_item(destination) |
| 531 |
loop.release_lock() |
| 532 |
self.preferences.update_page_number() |
| 533 |
|
| 534 |
@on_thread |
| 535 |
def take_and_recognize_full_screenshot(self,data): |
| 536 |
destination = self.get_feesible_filename_from_filename("{}{}.png".format(macros.tmp_dir,self.preferences.starting_page_number)) |
| 537 |
self.window.iconify() |
| 538 |
os.system("sleep 1") |
| 539 |
capture_screen.capture_entire_screen(destination) |
| 540 |
loop.acquire_lock() |
| 541 |
self.iconview.add_item(destination) |
| 542 |
loop.release_lock() |
| 543 |
text,angle = self.ocr(destination,2,00) |
| 544 |
self.insert_text_to_textview(text,self.preferences.insert_position) |
| 545 |
self.preferences.update_page_number() |
| 546 |
|
| 547 |
@on_thread |
| 548 |
def take_and_recognize_rectangle_screenshot(self,data): |
| 549 |
destination = self.get_feesible_filename_from_filename("{}{}.png".format(macros.tmp_dir,self.preferences.starting_page_number)) |
| 550 |
self.window.iconify() |
| 551 |
capture_screen.capture_rectangle_selection(destination) |
| 552 |
loop.acquire_lock() |
| 553 |
self.iconview.add_item(destination) |
| 554 |
loop.release_lock() |
| 555 |
text,angle = self.ocr(destination,2,00) |
| 556 |
self.insert_text_to_textview(text,self.preferences.insert_position) |
| 557 |
self.preferences.update_page_number() |
| 558 |
|
| 559 |
|
| 560 |
def on_iconview_item_selected(self,data): |
| 561 |
name = self.iconview.get_selected_item_names() |
| 562 |
if(name): |
| 563 |
self.imageview.load_image(name[0],[],imageview.ImageViewer.ZOOM_FIT) |
| 564 |
list_ = [] |
| 565 |
if (not os.path.exists(name[0]+".box")): |
| 566 |
return |
| 567 |
|
| 568 |
for line in open(name[0]+".box"): |
| 569 |
spl = line.split(" ") |
| 570 |
try: |
| 571 |
list_.append((int(spl[0]),float(spl[1]),float(spl[2]),float(spl[3]),float(spl[4]),str(spl[5]))) |
| 572 |
except: |
| 573 |
pass |
| 574 |
self.imageview.set_list(list_,0) |
| 575 |
else: |
| 576 |
self.imageview.load_image(macros.logo_file,[],imageview.ImageViewer.ZOOM_FIT) |
| 577 |
|
| 578 |
|
| 579 |
@on_thread |
| 580 |
def update_scanner_list(self,*data): |
| 581 |
|
| 582 |
|
| 583 |
self.is_updating_scanner_list = True |
| 584 |
|
| 585 |
try: |
| 586 |
self.combobox_scanners.clear() |
| 587 |
for item in self.scanner_objects: |
| 588 |
item.close() |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
self.scanner_objects = [] |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
loop.acquire_lock() |
| 598 |
self.notify_information(_("Getting devices")) |
| 599 |
loop.release_lock() |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
parent_conn, child_conn = multiprocessing.Pipe() |
| 604 |
p = multiprocessing.Process(target=(lambda parent_conn, child_conn : |
| 605 |
child_conn.send(tuple(self.available_scanner_driver_list[self.preferences.scan_driver].get_available_devices()))), |
| 606 |
args=(parent_conn, child_conn)) |
| 607 |
|
| 608 |
p.start() |
| 609 |
while(p.is_alive()): |
| 610 |
pass |
| 611 |
|
| 612 |
|
| 613 |
driver = self.available_scanner_driver_list[self.preferences.scan_driver] |
| 614 |
list_ = list(parent_conn.recv()) |
| 615 |
for device in list_: |
| 616 |
self.notify_information(_("Setting Scanner {}").format(device)) |
| 617 |
scanner = driver(device,self.preferences.scanner_mode_switching, |
| 618 |
self.preferences.scan_resolution,self.preferences.scan_brightness, |
| 619 |
self.preferences.scan_area) |
| 620 |
|
| 621 |
self.scanner_objects.append(scanner) |
| 622 |
self.combobox_scanners.add_item(scanner.device_name) |
| 623 |
print(self.scanner_objects) |
| 624 |
|
| 625 |
loop.acquire_lock() |
| 626 |
|
| 627 |
|
| 628 |
if (len(self.scanner_objects) != 0): |
| 629 |
self.combobox_scanners.set_active(0) |
| 630 |
|
| 631 |
self.notify_information(_("Completed!"),0) |
| 632 |
else: |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
self.notify_information(_("No Scanner Detected!"),0) |
| 637 |
pass |
| 638 |
|
| 639 |
loop.release_lock() |
| 640 |
except Exception as ex: |
| 641 |
loop.acquire_lock() |
| 642 |
self.notify_information("Scanner list update error : "+str(ex),0) |
| 643 |
loop.release_lock() |
| 644 |
finally: |
| 645 |
self.is_updating_scanner_list = False |
| 646 |
|
| 647 |
@on_thread |
| 648 |
def scan_single_image(self,widget): |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
if(not self.is_updating_scanner_list and len(self.scanner_objects) == 0): |
| 653 |
self.update_scanner_list(); |
| 654 |
while(self.is_updating_scanner_list): |
| 655 |
pass |
| 656 |
if(len(self.scanner_objects) == 0): |
| 657 |
return; |
| 658 |
|
| 659 |
destination = "{0}{1}.jpg".format(macros.tmp_dir,self.preferences.get_page_number_as_string()) |
| 660 |
destination = self.get_feesible_filename_from_filename(destination) |
| 661 |
t = threading.Thread(target=self.scan,args=(destination,)) |
| 662 |
t.start() |
| 663 |
while(t.is_alive()): |
| 664 |
pass |
| 665 |
self.preferences.update_page_number() |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
@on_thread |
| 671 |
def scan_image_repeatedly(self,widget): |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
if(not self.is_updating_scanner_list and len(self.scanner_objects) == 0): |
| 676 |
self.update_scanner_list(); |
| 677 |
while(self.is_updating_scanner_list): |
| 678 |
pass |
| 679 |
if(len(self.scanner_objects) == 0): |
| 680 |
return; |
| 681 |
|
| 682 |
self.process_breaker = False |
| 683 |
for i in range(0,self.preferences.number_of_pages_to_scan): |
| 684 |
destination = "{0}{1}.jpg".format(macros.tmp_dir,self.preferences.get_page_number_as_string()) |
| 685 |
destination = self.get_feesible_filename_from_filename(destination) |
| 686 |
t = threading.Thread(target=self.scan,args=(destination,)) |
| 687 |
t.start() |
| 688 |
while(t.is_alive()): |
| 689 |
pass |
| 690 |
self.preferences.update_page_number() |
| 691 |
if(self.process_breaker): |
| 692 |
break |
| 693 |
time.sleep(self.preferences.time_between_repeated_scanning) |
| 694 |
if(self.process_breaker): |
| 695 |
break |
| 696 |
self.notify_information(_("Completed!"),0) |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
def scan(self,filename): |
| 701 |
self.process_breaker = False |
| 702 |
selected_scanner = self.combobox_scanners.get_active() |
| 703 |
|
| 704 |
self.notify_information(_("Scanning")+" "+filename.split("/")[-1]) |
| 705 |
|
| 706 |
p = multiprocessing.Process(target=(self.scanner_objects[selected_scanner].scan), |
| 707 |
args=(filename,self.preferences.scan_resolution, |
| 708 |
self.preferences.scan_brightness,self.preferences.scan_area)) |
| 709 |
|
| 710 |
p.start() |
| 711 |
while(p.is_alive()): |
| 712 |
pass |
| 713 |
self.notify_information(_("Scan Completed!"),0) |
| 714 |
|
| 715 |
if(self.process_breaker): |
| 716 |
return |
| 717 |
loop.acquire_lock() |
| 718 |
self.iconview.add_item(filename) |
| 719 |
loop.release_lock() |
| 720 |
if(self.process_breaker): |
| 721 |
return |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
def ocr(self,file_name,mode,angle): |
| 727 |
self.process_breaker = False |
| 728 |
|
| 729 |
ocr_engine_object = self.available_ocr_engine_list[self.preferences.ocr_engine]() |
| 730 |
languages_available = self.available_ocr_engine_list[self.preferences.ocr_engine].get_available_languages() |
| 731 |
|
| 732 |
language = languages_available[self.preferences.language] |
| 733 |
ocr_engine_object.set_language(language) |
| 734 |
|
| 735 |
|
| 736 |
languages_available.insert(0,"---"); |
| 737 |
|
| 738 |
language_2 = languages_available[self.preferences.language_2] |
| 739 |
ocr_engine_object.set_language_2(language_2) |
| 740 |
|
| 741 |
language_3 = languages_available[self.preferences.language_3] |
| 742 |
ocr_engine_object.set_language_3(language_3) |
| 743 |
|
| 744 |
print(language) |
| 745 |
rotation_list = [00,90,180,270] |
| 746 |
if mode == 2: |
| 747 |
if(angle not in rotation_list): |
| 748 |
os.system("convert -rotate {0} {1} {1}".format(rotation_list[angle],file_name)) |
| 749 |
else: |
| 750 |
os.system("convert -rotate {0} {1} {1}".format(angle,file_name)) |
| 751 |
text = ocr_engine_object.ocr_image_to_text_with_multiprocessing(file_name) |
| 752 |
return (text,angle) |
| 753 |
else: |
| 754 |
list_ = [] |
| 755 |
for angle in rotation_list: |
| 756 |
os.system("convert -rotate {0} {1} {1}_test".format(angle,file_name)) |
| 757 |
text = ocr_engine_object.ocr_image_to_text_with_multiprocessing(file_name+"_test") |
| 758 |
count = self.count_dict_words(text) |
| 759 |
list_.append((text,count,angle)) |
| 760 |
if(self.process_breaker): |
| 761 |
return True; |
| 762 |
list_ = sorted(list_, key=lambda item: item[1],reverse=True) |
| 763 |
os.system("convert -rotate {0} {1} {1}".format(list_[0][2],file_name)) |
| 764 |
return (list_[0][0],list_[0][2]) |
| 765 |
|
| 766 |
def count_dict_words(self,text): |
| 767 |
count = 0 |
| 768 |
for word in text.split(" "): |
| 769 |
if (len(word) > 1): |
| 770 |
if (self.dict.check(word) == True): |
| 771 |
count += 1 |
| 772 |
return count |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
def insert_text_to_textview(self,text,place_cursor = False,give_page_number = False): |
| 777 |
if(self.preferences.run_text_cleaner): |
| 778 |
text = self.textview.get_text_cleaner_out(text) |
| 779 |
loop.acquire_lock() |
| 780 |
if (give_page_number): |
| 781 |
text = "\nPage-{}\n{}".format(self.preferences.get_page_number_as_string(),text) |
| 782 |
self.textview.insert_text(text,self.preferences.insert_position) |
| 783 |
text = self.textview.get_text() |
| 784 |
cursor_position = self.textview.get_cursor_line_number() |
| 785 |
loop.release_lock() |
| 786 |
with open(macros.recent_file_path,"w",encoding="utf-8") as file: |
| 787 |
file.write(text) |
| 788 |
with open(macros.recent_cursor_position_file_path,"w",encoding="utf-8") as file: |
| 789 |
file.write(str(cursor_position)) |
| 790 |
|
| 791 |
@on_thread |
| 792 |
def scan_and_ocr(self,widget): |
| 793 |
|
| 794 |
if(not self.is_updating_scanner_list and len(self.scanner_objects) == 0): |
| 795 |
self.update_scanner_list(); |
| 796 |
while(self.is_updating_scanner_list): |
| 797 |
pass |
| 798 |
if(len(self.scanner_objects) == 0): |
| 799 |
return; |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
self.process_breaker = False |
| 805 |
destination = "{0}{1}.jpg".format(macros.tmp_dir,self.preferences.get_page_number_as_string()) |
| 806 |
destination = self.get_feesible_filename_from_filename(destination) |
| 807 |
t = threading.Thread(target=self.scan,args=(destination,)) |
| 808 |
t.start() |
| 809 |
while(t.is_alive()): |
| 810 |
pass |
| 811 |
if(self.process_breaker): |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
return |
| 816 |
self.notify_information(_("Recognizing {}").format(destination.split("/")[-1])) |
| 817 |
text,angle = self.ocr(destination,self.preferences.mode_of_rotation,self.preferences.rotation_angle) |
| 818 |
self.insert_text_to_textview(text,self.preferences.insert_position) |
| 819 |
self.imageview.redraw() |
| 820 |
loop.acquire_lock() |
| 821 |
self.iconview.reload_preview(destination) |
| 822 |
loop.release_lock() |
| 823 |
self.notify_information(_("Page {}").format(self.preferences.get_page_number_as_string()),0) |
| 824 |
self.preferences.update_page_number() |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
@on_thread |
| 832 |
def scan_and_ocr_repeatedly(self,widget): |
| 833 |
if(not self.is_updating_scanner_list and len(self.scanner_objects) == 0): |
| 834 |
self.update_scanner_list(); |
| 835 |
while(self.is_updating_scanner_list): |
| 836 |
pass |
| 837 |
if(len(self.scanner_objects) == 0): |
| 838 |
return; |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
mode = self.preferences.mode_of_rotation |
| 844 |
angle = self.preferences.rotation_angle |
| 845 |
self.process_breaker = False |
| 846 |
for i in range(0,self.preferences.number_of_pages_to_scan): |
| 847 |
destination = "{0}{1}.jpg".format(macros.tmp_dir,self.preferences.get_page_number_as_string()) |
| 848 |
destination = self.get_feesible_filename_from_filename(destination) |
| 849 |
t = threading.Thread(target=self.scan,args=(destination,)) |
| 850 |
t.start() |
| 851 |
while(t.is_alive()): |
| 852 |
pass |
| 853 |
if(self.process_breaker): |
| 854 |
break |
| 855 |
time.sleep(self.preferences.time_between_repeated_scanning) |
| 856 |
if(self.process_breaker): |
| 857 |
break |
| 858 |
self.notify_information(_("Recognizing {}").format(destination.split("/")[-1])) |
| 859 |
text,angle = self.ocr(destination,mode,angle) |
| 860 |
if (i == 0): |
| 861 |
self.insert_text_to_textview(text,True,self.preferences.give_page_number) |
| 862 |
else: |
| 863 |
self.insert_text_to_textview(text,False,self.preferences.give_page_number) |
| 864 |
self.imageview.redraw() |
| 865 |
loop.acquire_lock() |
| 866 |
self.iconview.reload_preview(destination) |
| 867 |
self.notify_information(_("Page {}").format(self.preferences.get_page_number_as_string()),0) |
| 868 |
loop.release_lock() |
| 869 |
self.preferences.update_page_number() |
| 870 |
|
| 871 |
if mode == 1: |
| 872 |
mode = 2 |
| 873 |
|
| 874 |
|
| 875 |
if(self.process_breaker): |
| 876 |
break |
| 877 |
self.notify_information(_("Completed"),0) |
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
@on_thread |
| 889 |
def optimize_brightness(self,wedget): |
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
if(not self.is_updating_scanner_list and len(self.scanner_objects) == 0): |
| 895 |
self.update_scanner_list(); |
| 896 |
while(self.is_updating_scanner_list): |
| 897 |
pass |
| 898 |
if(len(self.scanner_objects) == 0): |
| 899 |
return; |
| 900 |
|
| 901 |
selected_scanner = self.combobox_scanners.get_active() |
| 902 |
|
| 903 |
self.process_breaker = False |
| 904 |
mode = self.preferences.mode_of_rotation |
| 905 |
if (mode == 0 or mode == 1): |
| 906 |
self.notify_information(_("Scanning with resolution {}, brightness {}, for detecting angle of rotation.") |
| 907 |
.format(self.preferences.scan_resolution,self.preferences.scan_brightness)) |
| 908 |
|
| 909 |
p = multiprocessing.Process(target=(self.scanner_objects[selected_scanner].scan), |
| 910 |
args=("{0}rotate.pnm".format(macros.tmp_dir),self.preferences.scan_resolution, |
| 911 |
self.preferences.scan_brightness,self.preferences.scan_area)) |
| 912 |
|
| 913 |
p.start() |
| 914 |
while(p.is_alive()): |
| 915 |
pass |
| 916 |
text,angle = self.ocr("{0}rotate.pnm".format(macros.tmp_dir),mode,00) |
| 917 |
self.notify_information("Image at {} angle.".format(angle),0) |
| 918 |
else: |
| 919 |
angle = self.preferences.rotation_angle |
| 920 |
value = self.preferences.scan_brightness; |
| 921 |
distance = 10; start = 10; end = 90; |
| 922 |
count = None |
| 923 |
result_text = _("<b>Click 'Optimize' to start optimisation </b>") |
| 924 |
while(1): |
| 925 |
loop.acquire_lock() |
| 926 |
dlg = dialog.Dialog(_("Optimize Scanner Brightness"), |
| 927 |
(_("Optimize"), dialog.Dialog.BUTTON_ID_3, |
| 928 |
_("Apply"), dialog.Dialog.BUTTON_ID_2, |
| 929 |
_("Cancel"), dialog.Dialog.BUTTON_ID_1)) |
| 930 |
|
| 931 |
label_rotation = widget.Label(_("Angle to be rotated: ")) |
| 932 |
spinbutton_rotation = widget.SpinButton(angle,00,360,90,90,90) |
| 933 |
label_rotation.set_mnemonic_widget(spinbutton_rotation) |
| 934 |
try: |
| 935 |
spinbutton_rotation.set_value([00,90,180,270][angle]) |
| 936 |
except: |
| 937 |
spinbutton_rotation.set_value(angle) |
| 938 |
|
| 939 |
label_value = widget.Label(_("Current Value")) |
| 940 |
spinbutton_value = widget.SpinButton(value,0,200,1,5,0) |
| 941 |
label_value.set_mnemonic_widget(spinbutton_value) |
| 942 |
|
| 943 |
label_start = widget.Label(_("Start")) |
| 944 |
spinbutton_start = widget.SpinButton(start,0,100,10,5,0) |
| 945 |
label_start.set_mnemonic_widget(spinbutton_start) |
| 946 |
|
| 947 |
label_distance = widget.Label(_("Distance")) |
| 948 |
spinbutton_distance = widget.SpinButton(distance,0,40,5,5,0) |
| 949 |
label_distance.set_mnemonic_widget(spinbutton_distance) |
| 950 |
|
| 951 |
label_end = widget.Label(_("End")) |
| 952 |
spinbutton_end = widget.SpinButton(end,0,100,10,5,0) |
| 953 |
label_end.set_mnemonic_widget(spinbutton_end) |
| 954 |
|
| 955 |
label_result = widget.Label(_("Result")) |
| 956 |
label_result.set_use_markup(True) |
| 957 |
label_result.set_label(result_text) |
| 958 |
|
| 959 |
grid = containers.Grid() |
| 960 |
grid.add_widgets([(label_rotation,1,1),(spinbutton_rotation,1,1), |
| 961 |
containers.Grid.NEW_ROW, |
| 962 |
(label_value,1,1),(spinbutton_value,1,1),containers.Grid.NEW_ROW, |
| 963 |
(label_start,1,1),(spinbutton_start,1,1),containers.Grid.NEW_ROW, |
| 964 |
(label_distance,1,1),(spinbutton_distance,1,1),containers.Grid.NEW_ROW, |
| 965 |
(label_end,1,1),(spinbutton_end,1,1),containers.Grid.NEW_ROW, |
| 966 |
(label_result,2,1)]); |
| 967 |
dlg.add_widget(grid) |
| 968 |
grid.show_all() |
| 969 |
|
| 970 |
response = dlg.run() |
| 971 |
dlg.destroy() |
| 972 |
loop.release_lock() |
| 973 |
if (response == dialog.Dialog.BUTTON_ID_2): |
| 974 |
self.preferences.scan_brightness = spinbutton_value.get_value() |
| 975 |
angle = spinbutton_rotation.get_value() |
| 976 |
loop.acquire_lock() |
| 977 |
dlg_set_mode = dialog.Dialog(_("Set Mode of rotation"), |
| 978 |
(_("Yes set this rotation"), dialog.Dialog.BUTTON_ID_1, |
| 979 |
_("No continue with existing mode"), dialog.Dialog.BUTTON_ID_2)) |
| 980 |
label = widget.Label(_("Do you want to fix the angle at {}\ |
| 981 |
\ndegree manual rotation ?").format(angle)) |
| 982 |
dlg_set_mode.add_widget(label) |
| 983 |
label.show() |
| 984 |
response = dlg_set_mode.run() |
| 985 |
if(response == dialog.Dialog.BUTTON_ID_1): |
| 986 |
self.preferences.mode_of_rotation = 2 |
| 987 |
self.preferences.rotation_angle = [00,90,180,270].index(angle) |
| 988 |
self.notify_information(_("Rotation mode changed to manual at angle {} degree"). |
| 989 |
format(self.preferences.rotation_angle),0) |
| 990 |
dlg_set_mode.destroy() |
| 991 |
loop.release_lock() |
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
return True |
| 996 |
elif (response == dialog.Dialog.BUTTON_ID_3): |
| 997 |
value = spinbutton_value.get_value() |
| 998 |
start = spinbutton_start.get_value() |
| 999 |
distance = spinbutton_distance.get_value() |
| 1000 |
end = spinbutton_end.get_value() |
| 1001 |
angle = spinbutton_rotation.get_value() |
| 1002 |
self.preferences.scan_brightness = value |
| 1003 |
else: |
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
return True |
| 1008 |
|
| 1009 |
list = self.optimize_with_model(value,start,distance,end,angle,count) |
| 1010 |
if (not list): |
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
return True |
| 1015 |
count, value = list[0][0],list[0][1]; |
| 1016 |
result_text = _("<b>Optimisation Result </b>") |
| 1017 |
for item in list: |
| 1018 |
result_text += _("\nGot {} Words at brightness {}").format(item[0], item[1]) |
| 1019 |
result_text += "</b>" |
| 1020 |
start = value-distance; |
| 1021 |
end = value+distance; |
| 1022 |
distance = distance / 2; |
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
def optimize_with_model(self,value,start,distance,end,angle,previous_optimised_count=None): |
| 1027 |
selected_scanner = self.combobox_scanners.get_active() |
| 1028 |
list = [] |
| 1029 |
count = -1 |
| 1030 |
pos = start |
| 1031 |
while(pos <= end): |
| 1032 |
if (pos == value and previous_optimised_count != None): |
| 1033 |
list.append((previous_optimised_count,value)) |
| 1034 |
|
| 1035 |
else: |
| 1036 |
if (count != -1): |
| 1037 |
self.notify_information(_("Got {} words at brightness {}. Scanning with resolution {}, brightness {}.") |
| 1038 |
.format(count,pos-distance,self.preferences.scan_resolution,pos)) |
| 1039 |
else: |
| 1040 |
self.notify_information(_("Scanning with resolution {}, brightness {}.") |
| 1041 |
.format(self.preferences.scan_resolution,pos)) |
| 1042 |
|
| 1043 |
p = multiprocessing.Process(target=(self.scanner_objects[selected_scanner].scan), |
| 1044 |
args=("{0}test.pnm".format(macros.tmp_dir,self.preferences.get_page_number_as_string()), |
| 1045 |
self.preferences.scan_resolution,pos,self.preferences.scan_area)) |
| 1046 |
|
| 1047 |
p.start() |
| 1048 |
while(p.is_alive()): |
| 1049 |
pass |
| 1050 |
if(self.process_breaker): |
| 1051 |
list = sorted(list, key=lambda item: item[0],reverse=True) |
| 1052 |
return (list) |
| 1053 |
self.notify_information(_("Recognizing {0}test.pnm").format(macros.tmp_dir)) |
| 1054 |
text,angle = self.ocr("{0}test.pnm".format(macros.tmp_dir),2,angle) |
| 1055 |
count = self.count_dict_words(text) |
| 1056 |
list.append((count,pos)) |
| 1057 |
|
| 1058 |
pos = pos + distance |
| 1059 |
self.notify_information(_("Completed!"),0) |
| 1060 |
list = sorted(list, key=lambda item: item[0],reverse=True) |
| 1061 |
return (list) |
| 1062 |
|
| 1063 |
def stop_all_process(self,widget): |
| 1064 |
self.process_breaker = True |
| 1065 |
|
| 1066 |
|
| 1067 |
os.system("pkill convert") |
| 1068 |
self.available_ocr_engine_list[self.preferences.ocr_engine].cancel() |
| 1069 |
self.notify_information(_("Terminated"),0) |
| 1070 |
|
| 1071 |
def open_readme(self,*data): |
| 1072 |
if(self.textview.get_modified()): |
| 1073 |
dlg = dialog.Dialog(_("Warning!"), |
| 1074 |
(_("No"),dialog.Dialog.BUTTON_ID_1,_("Yes"),dialog.Dialog.BUTTON_ID_2)) |
| 1075 |
label = widget.Label(_("Current text not saved! do you want to load readme without saving?")) |
| 1076 |
label.show() |
| 1077 |
dlg.add_widget(label) |
| 1078 |
response = dlg.run() |
| 1079 |
if response == dialog.Dialog.BUTTON_ID_2: |
| 1080 |
with open(macros.readme_file) as file: |
| 1081 |
self.textview.set_text(file.read()) |
| 1082 |
dlg.destroy() |
| 1083 |
else: |
| 1084 |
with open(macros.readme_file) as file: |
| 1085 |
self.textview.set_text(file.read()) |
| 1086 |
|
| 1087 |
|
| 1088 |
@on_thread |
| 1089 |
def ocr_selected_images_with_rotation(self,widget): |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
items = self.iconview.get_selected_item_names(); |
| 1094 |
length = len(items) |
| 1095 |
if (length > 0): |
| 1096 |
progress_step = 1/len(self.iconview.get_selected_item_names()) |
| 1097 |
progress = 0; |
| 1098 |
mode = self.preferences.mode_of_rotation |
| 1099 |
angle = self.preferences.rotation_angle |
| 1100 |
for item in self.iconview.get_selected_item_names(): |
| 1101 |
self.notify_information(_("Recognizing {}").format(item.split("/")[-1])) |
| 1102 |
progress = progress + progress_step; |
| 1103 |
text,angle = self.ocr(item,mode,angle) |
| 1104 |
self.insert_text_to_textview(text,self.preferences.insert_position,self.preferences.give_page_number) |
| 1105 |
self.preferences.update_page_number() |
| 1106 |
loop.acquire_lock() |
| 1107 |
self.iconview.reload_preview(item) |
| 1108 |
loop.release_lock() |
| 1109 |
if mode == 1: |
| 1110 |
mode = 2 |
| 1111 |
|
| 1112 |
if(self.process_breaker): |
| 1113 |
break |
| 1114 |
self.imageview.redraw() |
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
self.notify_information(_("Completed!"),0) |
| 1119 |
else: |
| 1120 |
self.notify_information(_("No item selected!"),0) |
| 1121 |
|
| 1122 |
|
| 1123 |
def ocr_all_images_with_rotation(self,widget): |
| 1124 |
self.iconview.select_all() |
| 1125 |
self.ocr_selected_images(None) |
| 1126 |
|
| 1127 |
|
| 1128 |
@on_thread |
| 1129 |
def ocr_selected_images(self,widget): |
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
items = self.iconview.get_selected_item_names(); |
| 1134 |
length = len(items) |
| 1135 |
if (length > 0): |
| 1136 |
progress_step = 1/length |
| 1137 |
progress = 0; |
| 1138 |
for item in items: |
| 1139 |
self.notify_information(_("Recognizing {}").format(item.split("/")[-1])) |
| 1140 |
progress = progress + progress_step; |
| 1141 |
text,angle = self.ocr(item,2,00) |
| 1142 |
self.insert_text_to_textview(text,self.preferences.insert_position,self.preferences.give_page_number) |
| 1143 |
self.preferences.update_page_number() |
| 1144 |
if(self.process_breaker): |
| 1145 |
break |
| 1146 |
self.notify_information(_("Completed!"),0) |
| 1147 |
else: |
| 1148 |
self.notify_information(_("No item selected!"),0) |
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
def ocr_all_images(self,widget): |
| 1155 |
self.iconview.select_all() |
| 1156 |
self.ocr_selected_images(self) |
| 1157 |
|
| 1158 |
|
| 1159 |
def iconview_remove_all_images(self,widget): |
| 1160 |
self.iconview.select_all() |
| 1161 |
self.iconview_remove_selected_images() |
| 1162 |
|
| 1163 |
def iconview_remove_selected_images(self,*data): |
| 1164 |
if (len(self.iconview.get_selected_item_names()) >= 1): |
| 1165 |
dlg = dialog.Dialog(_("Deleting!"),(_("Cancel"),dialog.Dialog.BUTTON_ID_1,_("Yes Delete"), dialog.Dialog.BUTTON_ID_2)) |
| 1166 |
label = widget.Label(_("Are you sure you want to delete selected images?")) |
| 1167 |
dlg.add_widget(label) |
| 1168 |
label.show() |
| 1169 |
response = dlg.run() |
| 1170 |
dlg.destroy() |
| 1171 |
if (response == dialog.Dialog.BUTTON_ID_2): |
| 1172 |
self.iconview.remove_selected_items() |
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
@on_thread |
| 1177 |
def rotate_selected_images_to_angle(self,angle): |
| 1178 |
if(len(self.iconview.get_selected_item_names()) == 0): |
| 1179 |
self.notify_information(_("Nothing selected"),0) |
| 1180 |
return |
| 1181 |
length = len(self.iconview.get_selected_item_names()) |
| 1182 |
if length > 0: |
| 1183 |
progress_step = 1/length |
| 1184 |
progress = 0; |
| 1185 |
for item in reversed(self.iconview.get_selected_item_names()): |
| 1186 |
os.system("convert -rotate {0} {1} {1}".format(angle,item)) |
| 1187 |
loop.acquire_lock() |
| 1188 |
self.iconview.reload_preview(item) |
| 1189 |
loop.release_lock() |
| 1190 |
self.notify_information(_("Rotating selected image {} to {}") |
| 1191 |
.format(item,angle),progress) |
| 1192 |
progress = progress + progress_step; |
| 1193 |
self.imageview.redraw() |
| 1194 |
self.notify_information(_("Completed!"),0) |
| 1195 |
|
| 1196 |
def rotate_selected_images_to_right(self,widget): |
| 1197 |
self.rotate_selected_images_to_angle(90) |
| 1198 |
|
| 1199 |
def rotate_selected_images_to_left(self,widget): |
| 1200 |
self.rotate_selected_images_to_angle(270) |
| 1201 |
|
| 1202 |
def rotate_selected_images_to_twice(self,widget): |
| 1203 |
self.rotate_selected_images_to_angle(180) |
| 1204 |
|
| 1205 |
|
| 1206 |
def rotate_all_images_to_right(self,widget): |
| 1207 |
self.iconview.select_all() |
| 1208 |
self.rotate_selected_images_to_right(None) |
| 1209 |
|
| 1210 |
def rotate_all_images_to_left(self,widget): |
| 1211 |
self.iconview.select_all() |
| 1212 |
self.rotate_selected_images_to_left(None) |
| 1213 |
|
| 1214 |
def rotate_all_images_to_twice(self,widget): |
| 1215 |
self.iconview.select_all() |
| 1216 |
self.rotate_selected_images_to_twice(None) |
| 1217 |
|
| 1218 |
def rotate_current_images_to_right(self,widget): |
| 1219 |
filename = self.imageview.get_filename() |
| 1220 |
self.iconview.select_item(filename) |
| 1221 |
self.rotate_selected_images_to_right(None) |
| 1222 |
|
| 1223 |
def rotate_current_images_to_left(self,widget): |
| 1224 |
filename = self.imageview.get_filename() |
| 1225 |
self.iconview.select_item(filename) |
| 1226 |
self.rotate_selected_images_to_left(None) |
| 1227 |
|
| 1228 |
def rotate_current_images_to_twice(self,widget): |
| 1229 |
filename = self.imageview.get_filename() |
| 1230 |
self.iconview.select_item(filename) |
| 1231 |
self.rotate_selected_images_to_twice(None) |
| 1232 |
|
| 1233 |
|
| 1234 |
def save_selected_images(self,widget): |
| 1235 |
item_list = self.iconview.get_selected_item_names() |
| 1236 |
if (len(item_list) == 0): |
| 1237 |
pass |
| 1238 |
elif (len(item_list) == 1): |
| 1239 |
dlg = FileChooserDialog(_("Filename please"), |
| 1240 |
FileChooserDialog.SAVE,macros.supported_image_formats, |
| 1241 |
macros.user_home_path); |
| 1242 |
response = dlg.run() |
| 1243 |
if response == FileChooserDialog.ACCEPT: |
| 1244 |
shutil.copy(item_list[0],dlg.get_filename()) |
| 1245 |
dlg.destroy() |
| 1246 |
else: |
| 1247 |
dlg = FileChooserDialog(_("Select Folder to save images"), |
| 1248 |
FileChooserDialog.OPEN_FOLDER,macros.supported_image_formats, |
| 1249 |
macros.user_home_path); |
| 1250 |
response = dlg.run() |
| 1251 |
if response == FileChooserDialog.ACCEPT: |
| 1252 |
directory = dlg.get_current_folder() |
| 1253 |
for item in reversed(item_list): |
| 1254 |
shutil.copy(item,directory) |
| 1255 |
dlg.destroy() |
| 1256 |
|
| 1257 |
|
| 1258 |
def save_all_images(self,widget): |
| 1259 |
self.iconview.select_all() |
| 1260 |
self.save_selected_images(None) |
| 1261 |
|
| 1262 |
def save_selected_images_as_pdf(self,widget): |
| 1263 |
dlg = FileChooserDialog(_("Give pdf filename(with extension) to save images"), |
| 1264 |
FileChooserDialog.SAVE,macros.supported_pdf_formats,macros.user_home_path) |
| 1265 |
response = dlg.run() |
| 1266 |
if response == FileChooserDialog.ACCEPT: |
| 1267 |
file_name = dlg.get_filename() |
| 1268 |
command = "convert " |
| 1269 |
for item in self.iconview.get_selected_item_names(): |
| 1270 |
command += item + " " |
| 1271 |
command += '"'+file_name+'"' |
| 1272 |
os.system(command) |
| 1273 |
dlg.destroy() |
| 1274 |
|
| 1275 |
def save_all_images_as_pdf(self,widget): |
| 1276 |
self.iconview.select_all() |
| 1277 |
self.save_selected_images_as_pdf(None) |
| 1278 |
|
| 1279 |
@on_thread |
| 1280 |
def ocr_current_image(self,widget): |
| 1281 |
filename = self.imageview.get_filename() |
| 1282 |
self.iconview.select_item(filename) |
| 1283 |
self.ocr_selected_images(None) |
| 1284 |
|
| 1285 |
@on_thread |
| 1286 |
def ocr_current_image_with_rotation(self,widget): |
| 1287 |
filename = self.imageview.get_filename() |
| 1288 |
self.iconview.select_item(filename) |
| 1289 |
self.ocr_selected_images_with_rotation(None) |
| 1290 |
|
| 1291 |
@on_thread |
| 1292 |
def ocr_selected_areas(self,widget): |
| 1293 |
self.process_breaker = False |
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
length = len(self.imageview.get_selection_list()) |
| 1298 |
if length > 0: |
| 1299 |
progress_step = 1/length |
| 1300 |
progress = 0; |
| 1301 |
for item in self.imageview.get_selection_list(): |
| 1302 |
self.notify_information(_("Running OCR on selected Area [ X={} Y={} Width={} Height={} ]") |
| 1303 |
.format(item[0],item[1],item[2],item[3])) |
| 1304 |
|
| 1305 |
progress = progress + progress_step; |
| 1306 |
self.imageview.save_sub_image("{0}tmp".format(macros.tmp_dir), |
| 1307 |
item[0],item[1],item[2],item[3]) |
| 1308 |
|
| 1309 |
|
| 1310 |
text,angle = self.ocr("{0}tmp".format(macros.tmp_dir),2,00) |
| 1311 |
self.insert_text_to_textview(text,False,False) |
| 1312 |
if(self.process_breaker): |
| 1313 |
break; |
| 1314 |
|
| 1315 |
self.notify_information(_("Completed!"),0) |
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
def make_preferences_effective(self,*data): |
| 1322 |
if (self.old_language != self.preferences.language): |
| •1323 |
languages = self.available_ocr_engine_list[self.preferences.ocr_engine].get_available_languages() or ['en'] |
| 1324 |
self.old_language = self.preferences.language |
| 1325 |
if (self.preferences.language >= len(languages)): |
| 1326 |
self.old_language = 0; |
| 1327 |
self.preferences.language = 0; |
| 1328 |
lang = languages[self.preferences.language] |
| 1329 |
try: |
| 1330 |
langdict = dictionary.dictionary_language_dict[lang] |
| 1331 |
except: |
| 1332 |
langdict = lang |
| 1333 |
try: |
| 1334 |
self.dict = dictionary.Dict(langdict) |
| 1335 |
except: |
| 1336 |
self.dict = dictionary.Dict("en") |
| 1337 |
dlg = dialog.Dialog(_("Dictionary not found!"), (_("Ok"),dialog.Dialog.BUTTON_ID_1)) |
| 1338 |
label = widget.Label(_( |
| 1339 |
"""Please install aspell, ispell, hunspell, myspell, or uspell |
| 1340 |
dictionary for your language({0}) and restart Lios! |
| 1341 |
Otherwise spellchecker and auto-rotation will work with english(fallback). |
| 1342 |
|
| 1343 |
For example on debian based system one can install aspell or |
| 1344 |
hunspell french dictionary using following commands |
| 1345 |
apt-get install aspell-{1} |
| 1346 |
apt-get install hunspell-{1} |
| 1347 |
|
| 1348 |
or ispell dict using |
| 1349 |
apt-get install ifrench |
| 1350 |
|
| 1351 |
On rpm based system use |
| 1352 |
yum install aspell-{1} |
| 1353 |
|
| 1354 |
On arch based system use |
| 1355 |
pacman -S aspell-{1}""").format(lang, langdict, langdict, langdict, langdict)) |
| 1356 |
dlg.add_widget(label) |
| 1357 |
label.show() |
| 1358 |
dlg.run() |
| 1359 |
dlg.destroy() |
| 1360 |
|
| 1361 |
if (self.old_scan_driver != self.preferences.scan_driver or |
| 1362 |
self.old_scanner_mode_switching != self.preferences.scanner_mode_switching ): |
| 1363 |
self.old_scan_driver = self.preferences.scan_driver |
| 1364 |
self.old_scanner_mode_switching = self.preferences.scanner_mode_switching |
| 1365 |
self.update_scanner_list() |
| 1366 |
|
| 1367 |
self.textview.set_dictionary(self.dict) |
| 1368 |
self.textview.set_font(self.preferences.font) |
| 1369 |
self.textview.set_highlight_font(self.preferences.highlight_font) |
| 1370 |
self.textview.set_highlight_color(self.preferences.highlight_color) |
| 1371 |
self.textview.set_highlight_background(self.preferences.background_highlight_color) |
| 1372 |
|
| 1373 |
def save_preferences(self,*data): |
| 1374 |
save_preferences_dlg = FileChooserDialog(_("Save preferences as "), |
| 1375 |
FileChooserDialog.SAVE,["cfg"],macros.user_home_path) |
| 1376 |
response = save_preferences_dlg.run() |
| 1377 |
if response == FileChooserDialog.ACCEPT: |
| 1378 |
self.preferences.save_to_file(save_preferences_dlg.get_filename()+".cfg") |
| 1379 |
self.notify_information(_("Preferences saved to ")+save_preferences_dlg.get_filename()+".cfg",0) |
| 1380 |
save_preferences_dlg.destroy() |
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
def load_preferences(self,*data): |
| 1385 |
load_preferences_dlg = FileChooserDialog(_("Select the image"), |
| 1386 |
FileChooserDialog.OPEN,["cfg"],macros.user_home_path) |
| 1387 |
response = load_preferences_dlg.run() |
| 1388 |
if response == FileChooserDialog.ACCEPT: |
| 1389 |
self.preferences.set_from_file(load_preferences_dlg.get_filename()) |
| 1390 |
self.make_preferences_effective() |
| 1391 |
self.notify_information(_("Preferences loaded from ")+load_preferences_dlg.get_filename(),0) |
| 1392 |
load_preferences_dlg.destroy() |
| 1393 |
|
| 1394 |
|
| 1395 |
def restore_preferences(self,*data): |
| 1396 |
self.preferences.__init__() |
| 1397 |
self.preferences.set_default_speech_module_and_language() |
| 1398 |
self.make_preferences_effective() |
| 1399 |
self.notify_information(_("Preferences Restored"),0) |
| 1400 |
|
| 1401 |
def open_preferences_general_page(self,*data): |
| 1402 |
if(self.preferences.open_configure_dialog(0)): |
| 1403 |
self.make_preferences_effective() |
| 1404 |
self.preferences.save_to_file(macros.preferences_file_path) |
| 1405 |
|
| 1406 |
def open_preferences_recognition_page(self,*data): |
| 1407 |
if(self.preferences.open_configure_dialog(1)): |
| 1408 |
self.make_preferences_effective() |
| 1409 |
self.preferences.save_to_file(macros.preferences_file_path) |
| 1410 |
|
| 1411 |
def open_preferences_scanning_page(self,*data): |
| 1412 |
if(self.preferences.open_configure_dialog(2)): |
| 1413 |
self.make_preferences_effective() |
| 1414 |
self.preferences.save_to_file(macros.preferences_file_path) |
| 1415 |
|
| 1416 |
def open_files(self,widget,data=None): |
| 1417 |
file_chooser_open_files = FileChooserDialog(_("Select files to open"), |
| 1418 |
FileChooserDialog.OPEN,macros.supported_image_formats+ |
| 1419 |
macros.supported_text_formats+macros.supported_pdf_formats, |
| 1420 |
macros.user_home_path) |
| 1421 |
file_chooser_open_files.set_current_folder(macros.user_home_path) |
| 1422 |
file_chooser_open_files.set_select_multiple(True) |
| 1423 |
response = file_chooser_open_files.run() |
| 1424 |
if response == FileChooserDialog.ACCEPT: |
| 1425 |
file_list = file_chooser_open_files.get_filenames() |
| 1426 |
file_chooser_open_files.destroy() |
| 1427 |
self.open_list_of_files(file_list) |
| 1428 |
else: |
| 1429 |
file_chooser_open_files.destroy() |
| 1430 |
|
| 1431 |
def open_list_of_files(self,file_list): |
| 1432 |
recently_added_list = [] |
| 1433 |
for item in file_list: |
| 1434 |
if item.split('.')[-1] in macros.supported_image_formats: |
| 1435 |
filename = item.split("/")[-1:][0] |
| 1436 |
destination = "{0}{1}".format(macros.tmp_dir,filename.replace(' ','-')) |
| 1437 |
destination = self.get_feesible_filename_from_filename(destination) |
| 1438 |
self.add_image_to_list(item,destination,False) |
| 1439 |
recently_added_list.append(destination) |
| 1440 |
|
| 1441 |
if item.split('.')[-1] in ["pdf","Pdf"]: |
| 1442 |
self.import_images_from_pdf(item) |
| 1443 |
|
| 1444 |
|
| 1445 |
return; |
| 1446 |
|
| 1447 |
if item.split('.')[-1] in macros.supported_text_formats: |
| 1448 |
text = editor.read_text_from_file(item) |
| 1449 |
if(len(file_list) == 1): |
| 1450 |
self.textview.set_text(text) |
| 1451 |
self.textview.save_file_name = file_list[0] |
| 1452 |
self.textview.import_bookmarks_using_filename() |
| 1453 |
else: |
| 1454 |
self.textview.insert_text(text,editor.BasicTextView.AT_END) |
| 1455 |
if(len(recently_added_list) > 0): |
| 1456 |
self.recognize_recently_added_images_on_thread(recently_added_list) |
| 1457 |
|
| 1458 |
@on_thread |
| 1459 |
def recognize_recently_added_images_on_thread(self,recently_added_list): |
| 1460 |
time.sleep(1) |
| 1461 |
self.recognize_recently_added_images(recently_added_list); |
| 1462 |
|
| 1463 |
def recognize_recently_added_images(self,recently_added_list): |
| 1464 |
loop.acquire_lock() |
| 1465 |
|
| 1466 |
if(self.textview.get_text() != ""): |
| 1467 |
dlg = dialog.Dialog(_("Recognize imported images?"), |
| 1468 |
(_("Yes (also clear previous text)"),dialog.Dialog.BUTTON_ID_1,_("Yes"), |
| 1469 |
dialog.Dialog.BUTTON_ID_2,("No"),dialog.Dialog.BUTTON_ID_3)) |
| 1470 |
else: |
| 1471 |
dlg = dialog.Dialog(_("Recognize imported images?"), |
| 1472 |
(_("Yes"),dialog.Dialog.BUTTON_ID_2,_("No"), |
| 1473 |
dialog.Dialog.BUTTON_ID_3)) |
| 1474 |
|
| 1475 |
label = widget.Label(_("Do you want to recognize imported images?")) |
| 1476 |
label.show() |
| 1477 |
dlg.add_widget(label) |
| 1478 |
response = dlg.run() |
| 1479 |
if response == dialog.Dialog.BUTTON_ID_1 or response == dialog.Dialog.BUTTON_ID_2: |
| 1480 |
dlg.destroy(); |
| 1481 |
if (response == dialog.Dialog.BUTTON_ID_1): |
| 1482 |
self.textview.set_text(""); |
| 1483 |
loop.release_lock() |
| 1484 |
|
| 1485 |
length = len(recently_added_list) |
| 1486 |
if (length > 0): |
| 1487 |
progress_step = 1/length |
| 1488 |
progress = 0; |
| 1489 |
for item in recently_added_list: |
| 1490 |
self.notify_information(_("Recognizing {}").format(item.split("/")[-1])) |
| 1491 |
progress = progress + progress_step; |
| 1492 |
text,angle = self.ocr(item,2,00) |
| 1493 |
self.insert_text_to_textview(text,self.preferences.insert_position,self.preferences.give_page_number) |
| 1494 |
self.preferences.update_page_number() |
| 1495 |
if(self.process_breaker): |
| 1496 |
break |
| 1497 |
self.notify_information(_("Completed!"),0) |
| 1498 |
dlg.destroy(); |
| 1499 |
loop.release_lock() |
| 1500 |
|
| 1501 |
|
| 1502 |
@on_thread |
| 1503 |
def start_reader(self,*data): |
| 1504 |
if(self.is_reading == False and self.reader_stop_pressed == False): |
| 1505 |
self.is_reading = True |
| 1506 |
speaker = speech.Speech() |
| 1507 |
speaker.set_output_module(speaker.list_output_modules()[self.preferences.speech_module]) |
| 1508 |
language_person_dict = speaker.get_language_person_dict() |
| 1509 |
|
| 1510 |
if(self.preferences.speech_module != -1 and len(language_person_dict.keys()) > 1): |
| 1511 |
voice = language_person_dict[list(language_person_dict)[self.preferences.speech_language]][self.preferences.speech_person] |
| 1512 |
speaker.set_synthesis_voice(voice) |
| 1513 |
|
| 1514 |
speaker.set_volume(self.preferences.speech_volume) |
| 1515 |
speaker.set_pitch(self.preferences.speech_pitch) |
| 1516 |
while(not self.textview.is_cursor_at_end()): |
| 1517 |
loop.acquire_lock() |
| 1518 |
speaker.set_rate(self.preferences.speech_rate) |
| 1519 |
sentence = self.textview.get_next_sentence() |
| 1520 |
speaker.say(sentence) |
| 1521 |
loop.release_lock() |
| 1522 |
speaker.wait() |
| 1523 |
if(self.reader_stop_pressed == True): |
| 1524 |
speaker.close() |
| 1525 |
self.is_reading = False; |
| 1526 |
self.reader_stop_pressed = False |
| 1527 |
break |
| 1528 |
|
| 1529 |
self.is_reading = False; |
| 1530 |
self.reader_stop_pressed = False |
| 1531 |
|
| 1532 |
|
| 1533 |
def stop_reader(self, *data): |
| 1534 |
if(self.is_reading): |
| 1535 |
self.reader_stop_pressed = True |
| 1536 |
|
| 1537 |
def increase_reader_speed(self,*data): |
| 1538 |
if (self.preferences.speech_rate < 100): |
| 1539 |
self.preferences.speech_rate = self.preferences.speech_rate + 10; |
| 1540 |
|
| 1541 |
def decrease_reader_speed(self,*data): |
| 1542 |
if (self.preferences.speech_rate > -100): |
| 1543 |
self.preferences.speech_rate = self.preferences.speech_rate - 10; |
| 1544 |
|
| 1545 |
def scan_using_cam(self,widget): |
| 1546 |
devices = cam.Cam.get_available_devices() |
| 1547 |
if(devices): |
| 1548 |
ob = cam.Cam(devices[-1],1024,768) |
| 1549 |
ob.connect_image_captured(self.cam_image_captured) |
| 1550 |
|
| 1551 |
def cam_image_captured(self,widget,filename): |
| 1552 |
self.add_image_to_list(filename,"/tmp/Lios/{}".format(filename.split("/")[2]),True,False) |
| 1553 |
|
| 1554 |
def about(self,*data): |
| 1555 |
dlg = about.AboutDialog(_("Lios"),None) |
| 1556 |
dlg.set_name(_("Linux-Intelligent-Ocr-Solution")) |
| 1557 |
dlg.set_program_name(_("Linux-Intelligent-Ocr-Solution")) |
| 1558 |
dlg.set_version(macros.version) |
| 1559 |
dlg.set_logo_from_file(macros.logo_file) |
| 1560 |
dlg.set_comments(_("Lios is a free and open source software\n \ |
| 1561 |
for converting print into text using a scanner or camara.\n\ |
| 1562 |
It can also produce text from other sources. Such as images,\n\ |
| 1563 |
Pdf, or screenshot. Program is given total accessibility \n\ |
| 1564 |
for visually impaired. Lios is written in python3 and we release \n\ |
| 1565 |
it under GPL3 licence. There are great many possibilities\n\ |
| 1566 |
for this program. Feedback is the key to it.")) |
| 1567 |
dlg.set_copyright("Copyright (C) 2011-2015 Nalin.x.Linux") |
| 1568 |
dlg.set_license("GPL-V3") |
| 1569 |
dlg.set_website(macros.home_page_link) |
| 1570 |
dlg.set_website_label(_("Visit Home Page")) |
| 1571 |
dlg.set_authors(["Nalin"]) |
| 1572 |
dlg.set_documenters(["Shalini S","Sathyaseelan K"]) |
| 1573 |
dlg.set_artists(["Nahar", "Manuel Eduardo Cortez Vallejo", |
| 1574 |
"C V Jawahar","Naveen t.s", "Vimal Joseph","Hakkeem IT@School", |
| 1575 |
"Sreejith Mathil","James Mathew","Don Marang","Vinod-Kollam", |
| 1576 |
"Rizal Muttaqin","Sunny Kallada"]) |
| 1577 |
dlg.run() |
| 1578 |
dlg.destroy() |
| 1579 |
|
| 1580 |
def artha(self,*data): |
| 1581 |
os.system("artha &") |
| 1582 |
|
| 1583 |
def quit(self,data=None,da2=None): |
| 1584 |
if (self.is_updating_scanner_list): |
| 1585 |
return True |
| 1586 |
try: |
| 1587 |
shutil.rmtree(macros.tmp_dir) |
| 1588 |
except FileNotFoundError: |
| 1589 |
pass |
| 1590 |
|
| 1591 |
for item in self.scanner_objects: |
| 1592 |
item.close() |
| 1593 |
|
| 1594 |
cursor_position = self.textview.get_cursor_line_number() |
| 1595 |
loop.stop_main_loop() |
| 1596 |
self.preferences.save_to_file(macros.preferences_file_path) |
| 1597 |
|
| 1598 |
with open(macros.recent_cursor_position_file_path,"w",encoding="utf-8") as file: |
| 1599 |
file.write(str(cursor_position)) |
| 1600 |
|
| 1601 |
if __name__ == "__main__": |
| 1602 |
linux_intelligent_ocr_solution() |
| 1603 |
|