Skip to content

Commit

Permalink
Merge pull request #7 from mo-mo-666/develop
Browse files Browse the repository at this point in the history
For v1.0.0
  • Loading branch information
mo-mo-666 committed May 21, 2021
2 parents a86dd13 + 7b8bb02 commit 124be44
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/image_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from PIL import Image
import os
import glob
from typing import Union, Tuple, Iterator
from typing import Union, Tuple, Iterator, Optional
import logging

logger = logging.getLogger("adjust-scan-images")


def read_image(
path: str, resize_ratio: Union[float, None] = None
path: str, resize_ratio: Optional[float] = None
) -> Union[Tuple[None, None], Tuple[np.ndarray, Tuple[int, int]]]:
"""
Read image in grayscale.
Expand Down Expand Up @@ -40,7 +40,7 @@ def read_image(


def read_images(
dirname: str, ext: Union[str, None] = None, resize_ratio: Union[float, None] = None
dirname: str, ext: Optional[str] = None, resize_ratio: Optional[float] = None
) -> Iterator[Tuple[str, np.ndarray, Tuple[int, int]]]:
"""
Read images and return iterator.
Expand Down
4 changes: 2 additions & 2 deletions src/log_setting.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import logging
from logging import getLogger, StreamHandler, FileHandler, Formatter
from typing import Union
from typing import Optional

from .const import NOW

Expand All @@ -10,7 +10,7 @@

def set_logger(
console_mode: int = logging.INFO,
logpath: Union[str, None] = None,
logpath: Optional[str] = None,
file_mode: int = logging.WARN,
):
"""
Expand Down
20 changes: 10 additions & 10 deletions src/pipeline.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import logging
from typing import Union
from typing import Optional, List, Tuple

from .setting_io import MarksheetResultWriter
from .setting_io_ds import read_metadata, read_marksheet_setting, decide_save_filename
Expand All @@ -15,7 +15,7 @@


def pipeline(
img_dir: str, metadata_path: Union[None, str], save_dir: str, baseimg_path: str
img_dir: str, metadata_path: Optional[str], save_dir: str, baseimg_path: str
):
"""
Process pipeline.
Expand All @@ -42,12 +42,12 @@ def pipeline(
# metadata = read_metadata(metadata_path, pt2px = dpi[0])
baseimg, dpi = read_image(baseimg_path)
metadata = read_metadata(None, pt2px=dpi[0])
resize_ratio = metadata["resize_ratio"]
is_align = metadata["is_align"]
is_marksheet = metadata["is_marksheet"]
is_marksheet_fit = metadata["is_marksheet_fit"]
resize_ratio: float = metadata["resize_ratio"]
is_align: bool = metadata["is_align"]
is_marksheet: bool = metadata["is_marksheet"]
is_marksheet_fit: bool = metadata["is_marksheet_fit"]
coord_unit = metadata["coord_unit"]
pt2px = dpi[0] if coord_unit == "pt" else None
pt2px: Optional[int] = dpi[0] if coord_unit == "pt" else None

# read base image
logger.debug(f"Begin reading the base image {baseimg_path}")
Expand All @@ -65,8 +65,8 @@ def pipeline(
mark_reader = MarkReader(metadata)
marksheet_result_path = os.path.join(save_dir, f"marksheet_result_{NOW}.csv")
logger.info(f"Marksheet result is saved at {marksheet_result_path}")
marksheet_result_header = ["origin_filename", "save_filename"] + list(
metadata["sheet"].keys()
marksheet_result_header = tuple(
["origin_filename", "save_filename"] + list(metadata["sheet"].keys())
)
marksheet_result_writer = MarksheetResultWriter(
marksheet_result_path, marksheet_result_header
Expand All @@ -82,7 +82,7 @@ def pipeline(
mark_reader.fit(baseimg)

img_iter = read_images(img_dir, resize_ratio=resize_ratio)
error_paths = []
error_paths: List[Tuple[str, str]] = []
image_saver = ImageSaver(save_dir)
for p, img, dpi in img_iter:
is_error = False
Expand Down
14 changes: 7 additions & 7 deletions src/read_marksheet.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import cv2
import logging
from typing import Union
from typing import Union, Optional

logger = logging.getLogger("adjust-scan-images")

Expand Down Expand Up @@ -41,7 +41,7 @@ def __init__(self, metadata: dict):
Image metadata. See Note.
"""
self.metadata = metadata
self.sheet = self.metadata.get("sheet", {})
self.sheet: dict = self.metadata.get("sheet", {})
self.is_sheet = bool(self.sheet)
self.coord_style = self.metadata.get("sheet_coord_style", "circle")
if not self.is_sheet:
Expand All @@ -52,9 +52,9 @@ def __init__(self, metadata: dict):
if self.coord_style == "circle":
self.sheet = self.circle2bbox(self.sheet)
logger.debug(f"MarkReader: circle -> bbox: {self.sheet}")
self.g_ksize = self.metadata.get("sheet_gaussian_ksize", 0)
self.g_std = self.metadata.get("sheet_gaussian_std", 0)
self.threshold = self.metadata.get("sheet_score_threshold", 0)
self.g_ksize: int = self.metadata.get("sheet_gaussian_ksize", 0)
self.g_std: int = self.metadata.get("sheet_gaussian_std", 0)
self.threshold: int = self.metadata.get("sheet_score_threshold", 0)
self.is_fitted = False
self.base_scores = None

Expand Down Expand Up @@ -124,7 +124,7 @@ def _preprocess(self, img: np.ndarray) -> np.ndarray:
return preprocessed

def _one_mark_score(
self, img: np.ndarray, coords: dict, base_score: Union[dict, None] = None
self, img: np.ndarray, coords: dict, base_score: Optional[dict] = None
) -> dict:
"""
Read one mark score.
Expand Down Expand Up @@ -153,7 +153,7 @@ def _one_mark_score(
return scores

def _one_mark(
self, img: np.ndarray, coords: dict, base_score: Union[dict, None] = None
self, img: np.ndarray, coords: dict, base_score: Optional[dict] = None
) -> Union[None, str]:
"""
Read one category.
Expand Down
12 changes: 7 additions & 5 deletions src/setting_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from openpyxl import load_workbook
from collections import defaultdict
import logging
from typing import Union, Iterable
from typing import Iterable, Optional

logger = logging.getLogger("adjust-scan-images")

Expand All @@ -24,10 +24,10 @@


def read_metadata(
filepath: Union[None, str] = None,
filepath: Optional[str] = None,
mode: str = "excel",
excel_sheet_name: str = "image_setting",
pt2px: Union[None, float] = None,
pt2px: Optional[float] = None,
*args,
**kargs,
) -> dict:
Expand All @@ -36,12 +36,14 @@ def read_metadata(
Parameters
----------
filepath : str
Setting file path.
filepath : str | None, optional
Setting file path, by default None
mode : str, optional
Mode, by default "excel"
excel_sheet_name : str, optional
Excel sheet name, by default "image_setting"
pt2px : float | None, optional
if coord unit is pt, you should set the value dpi, optinal by defalut None
Returns
-------
Expand Down
14 changes: 7 additions & 7 deletions src/setting_io_ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
from collections import defaultdict
import logging
from typing import Union, Iterable
from typing import Iterable, Optional

logger = logging.getLogger("adjust-scan-images")

Expand All @@ -25,10 +25,10 @@


def read_metadata(
filepath: Union[None, str] = None,
filepath: Optional[str] = None,
mode: str = "excel",
excel_sheet_name: str = "image_setting",
pt2px: Union[None, float] = None,
pt2px: Optional[float] = None,
*args,
**kargs,
) -> dict:
Expand Down Expand Up @@ -96,10 +96,10 @@ def read_metadata(


def read_marksheet_setting(
filepath: Union[None, str],
filepath: Optional[str],
scale: float = 1,
categories: Union[None, Iterable[str]] = MARK_CATEGORIES,
pt2px: Union[None, float] = None,
categories: Optional[Iterable[str]] = MARK_CATEGORIES,
pt2px: Optional[float] = None,
*args,
**kargs,
) -> dict:
Expand Down Expand Up @@ -150,7 +150,7 @@ def read_marksheet_setting(


def decide_save_filename(
read_path: str, save_dir: str, data: Union[dict, None] = None
read_path: str, save_dir: str, data: Optional[dict] = None
) -> str:
"""
Decide file name when saving an image. OVERRIDE THIS TO CHANGE THE FILENAME.
Expand Down

0 comments on commit 124be44

Please sign in to comment.