generated from maycuatroi/python-project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f81e7d
commit a199ace
Showing
85 changed files
with
2,226 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,3 +130,4 @@ dmypy.json | |
|
||
# templates | ||
.github/templates/* | ||
/.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ FROM python:3.7-alpine | |
COPY . /app | ||
WORKDIR /app | ||
RUN pip install . | ||
CMD ["evo_flow"] | ||
CMD ["evoflow"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ include LICENSE | |
include HISTORY.md | ||
include Containerfile | ||
graft tests | ||
graft evo_flow | ||
graft evoflow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import abc | ||
|
||
|
||
class AbstractService: | ||
def __init__(self): | ||
pass | ||
|
||
@abc.abstractmethod | ||
def run(self, data, **args): | ||
pass | ||
|
||
@abc.abstractmethod | ||
def start(self, **args): | ||
pass | ||
|
||
@abc.abstractmethod | ||
def kill(self, **args): | ||
pass |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Copyright (c) 2021. Copyright belongs to evoflow team | ||
|
||
import logging | ||
import os.path | ||
from typing import Iterator | ||
from urllib.error import URLError | ||
|
||
import numpy as np | ||
|
||
from evoflow import logger | ||
from evoflow.Services.ocr.result import OCRResult | ||
|
||
try: | ||
import easyocr | ||
except ImportError: | ||
logger.error("Can't import easyocr. Try to install with: pip install easyocr") | ||
|
||
MODEL_PATH = "/ocr" | ||
|
||
|
||
class EasyOCREngine: | ||
""" | ||
Engine OCR sử dụng thư viện easyocr | ||
""" | ||
|
||
def __init__(self, languages=None): | ||
if languages is None: | ||
languages = ["en"] | ||
try: | ||
model_storage_directory_evoflow = f'{os.getenv("userprofile")}/.evoflow/ocr' | ||
model_storage_directory_data = "./data/.evoflow/ocr" | ||
if os.path.isfile(model_storage_directory_evoflow): | ||
model_storage_directory = model_storage_directory_evoflow | ||
else: | ||
model_storage_directory = model_storage_directory_data | ||
self.reader = easyocr.Reader( | ||
languages, | ||
download_enabled=True, | ||
model_storage_directory=model_storage_directory, | ||
) | ||
except URLError as url_error: | ||
logger.error("Can't download model, internet blocked") | ||
raise URLError( | ||
reason="Can't download model, internet blocked" | ||
) from url_error | ||
logging.info("Initiated EasyOCREngine") | ||
|
||
def ocr(self, image: np.array, detail=0) -> Iterator[OCRResult]: | ||
""" | ||
:param image: opencv image format | ||
:param detail: | ||
0 for simpler output. | ||
1 for detail output. | ||
""" | ||
results = self.reader.readtext(np.array(image), detail=detail) | ||
|
||
for i, result in enumerate(results): | ||
bbox = result[0] | ||
bbox = [list(map(int, z)) for z in bbox] # convert np.int32 to int | ||
res = (bbox, result[1], result[2]) | ||
results[i] = OCRResult(*res) | ||
return results | ||
|
||
@staticmethod | ||
def supported_languages() -> Iterator[str]: | ||
all_lang_list = easyocr.easyocr.all_lang_list | ||
return all_lang_list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from typing import Iterator | ||
|
||
import PIL | ||
import numpy as np | ||
from PIL import Image | ||
|
||
from evoflow.controller.log_controller import logger | ||
from evoflow.entities.data_manipulate.file_operator.file import File | ||
from evoflow.entities.data_manipulate.file_operator.image_file import ImageFile | ||
from evoflow.Services.abstract_service import AbstractService | ||
from evoflow.Services.ocr.Result import OCRResult | ||
|
||
|
||
def get_image(data: File): | ||
pil_image = np.array(Image.open(data.file)) | ||
return pil_image | ||
|
||
|
||
class OCRService(AbstractService): | ||
def start(self, **args): | ||
pass | ||
|
||
def kill(self, **args): | ||
pass | ||
|
||
def run(self, data: File, **args): | ||
|
||
image = get_image(data) | ||
result = self.ocr(image, **args) | ||
return result | ||
|
||
def __init__(self, engine_name="easyocr", languages=None): | ||
if languages is None: | ||
languages = ["en", "ja"] | ||
self.engine = None | ||
if engine_name == "easyocr": | ||
# pylint:disable=import-outside-toplevel | ||
from evoflow.Services.OCR.EasyOCREngine import EasyOCREngine | ||
|
||
self.engine = EasyOCREngine(languages=languages) | ||
else: | ||
logger.error("OCR engine name not valid") | ||
|
||
super().__init__() | ||
|
||
def ocr(self, image_file: ImageFile, **args) -> Iterator[OCRResult]: | ||
""" | ||
OCR text from image. | ||
:param image_file: Image file format | ||
""" | ||
if isinstance(image_file, str): | ||
image = PIL.Image.open(image_file) | ||
else: | ||
image = image_file.to_pil() | ||
results = self.engine.ocr(image, **args) | ||
return results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
easyocr |
Oops, something went wrong.