| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
import os |
| 21 |
import subprocess |
| 22 |
from lios.ocr.ocr_engine_base import OcrEngineBase |
| 23 |
|
| 24 |
|
| 25 |
class OcrEngineGocr(OcrEngineBase): |
| 26 |
name = "Gocr" |
| 27 |
|
| 28 |
def __init__(self,language=None): |
| 29 |
self.set_language(language) |
| 30 |
|
| 31 |
def is_available(): |
| 32 |
if ("/bin/gocr" in subprocess.getoutput("whereis gocr")): |
| 33 |
return True |
| 34 |
else: |
| 35 |
return False |
| 36 |
|
| 37 |
def ocr_image_to_text(self,file_name): |
| 38 |
os.system("convert {} /tmp/{}_for_ocr.pnm".format(file_name,file_name.split("/")[-1])) |
| 39 |
os.system("gocr -i /tmp/{0}_for_ocr.pnm -o /tmp/{0}_output.txt ".format(file_name.split("/")[-1])) |
| 40 |
os.remove("/tmp/{0}_for_ocr.pnm".format(file_name.split("/")[-1])) |
| 41 |
try: |
| 42 |
with open("/tmp/{0}_output.txt".format(file_name.split("/")[-1]),encoding="utf-8") as file: |
| 43 |
text = file.read().strip() |
| 44 |
os.remove("/tmp/{0}_output.txt".format(file_name.split("/")[-1])) |
| 45 |
return text |
| 46 |
except: |
| 47 |
return "" |
| 48 |
def cancel(): |
| 49 |
os.system("pkill convert") |
| 50 |
os.system("pkill gocr") |
| 51 |
|
| 52 |
|
| 53 |
def get_available_languages(): |
| 54 |
langs = ["eng"] |
| 55 |
return langs |
| 56 |
|
| 57 |
def support_multiple_languages(): |
| 58 |
return False |