Skip to content

Commit

Permalink
Fixed type errors which came up with the recent version of cv2
Browse files Browse the repository at this point in the history
  • Loading branch information
liebharc committed Jan 5, 2024
1 parent a37c53b commit c53ca1a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install ruff==0.0.285 mypy==1.5.1
pip install ruff==0.0.285 mypy==1.7.1
- name: List packages
run: pip list
Expand Down
3 changes: 2 additions & 1 deletion oemer/bbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Union, Any, List, Tuple, Dict

import cv2
from cv2.typing import RotatedRect
import numpy as np
from numpy import ndarray
from sklearn.cluster import AgglomerativeClustering
Expand Down Expand Up @@ -160,7 +161,7 @@ def draw_bounding_boxes(
return img


def get_rotated_bbox(data: ndarray) -> List[Tuple[Tuple[float, float], Tuple[float, float], float]]:
def get_rotated_bbox(data: ndarray) -> List[RotatedRect]:
contours, _ = cv2.findContours(data.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
bboxes = []
for cnt in contours:
Expand Down
4 changes: 2 additions & 2 deletions oemer/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def inference(
image_pil = Image.open(img_path)
if "GIF" != image_pil.format:
# Tricky workaround to avoid random mistery transpose when loading with 'Image'.
image_pil = cv2.imread(img_path)
image_pil = Image.fromarray(image_pil)
image_cv = cv2.imread(img_path)
image_pil = Image.fromarray(image_cv)

image_pil = image_pil.convert("RGB")
image = np.array(resize_image(image_pil))
Expand Down
17 changes: 9 additions & 8 deletions oemer/rhythm_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import math

import cv2
from cv2.typing import RotatedRect
import scipy.ndimage
import numpy as np
from numpy import ndarray
Expand Down Expand Up @@ -139,7 +140,7 @@ def parse_beams(
min_area_ratio: float = 0.07,
min_tp_ratio: float = 0.4,
min_width_ratio: float = 0.2
) -> Tuple[ndarray, List[Tuple[Tuple[float, float], Tuple[float, float], float]], ndarray]:
) -> Tuple[ndarray, List[RotatedRect], ndarray]:
# Fetch parameters
symbols = layers.get_layer('symbols_pred')
staff_pred = layers.get_layer('staff_pred')
Expand All @@ -161,14 +162,14 @@ def parse_beams(
ratio_map = np.copy(poly_map)

null_color = (255, 255, 255)
valid_box = []
valid_box: List[RotatedRect] = []
valid_idxs = []
idx_map = np.zeros_like(poly_map) - 1
for idx, rbox in enumerate(rboxes): # type: ignore
for box_idx, rbox in enumerate(rboxes): # type: ignore
# Used to find indexes of contour areas later. Must be check before
# any 'continue' statement.
idx %= 255 # type: ignore
if idx == 0:
box_idx %= 255 # type: ignore
if box_idx == 0:
idx_map = np.zeros_like(poly_map) - 1

# Get the contour of the rotated box
Expand All @@ -191,8 +192,8 @@ def parse_beams(
continue

# Tricky way to get the index of the contour area
cv2.fillPoly(idx_map, [cnt], color=(idx, 0, 0))
yi, xi = np.where(idx_map[..., 0] == idx)
cv2.fillPoly(idx_map, [cnt], color=(box_idx, 0, 0))
yi, xi = np.where(idx_map[..., 0] == box_idx)
pts = beams[yi, xi]
meta_idx = np.where(pts>0)[0]
ryi = yi[meta_idx]
Expand Down Expand Up @@ -615,7 +616,7 @@ def extract(
dot_max_area_ratio: float = 0.2,
beam_min_area_ratio: float = 0.07,
agree_th: float = 0.15
) -> Tuple[ndarray, List[Tuple[Tuple[float, float], Tuple[float, float], float]]]:
) -> Tuple[ndarray, List[RotatedRect]]:

logger.debug("Parsing dot")
parse_dot(max_area_ratio=dot_max_area_ratio, min_area_ratio=dot_min_area_ratio)
Expand Down
2 changes: 1 addition & 1 deletion oemer/symbol_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def parse_clefs_keys(
for box in bboxes:
w = box[2] - box[0]
h = box[3] - box[1]
region = clefs_keys[box[1]:box[3], box[0]:box[2]]
region: ndarray = clefs_keys[box[1]:box[3], box[0]:box[2]]
usize = get_unit_size(*get_center(box))
area_size_ratio = w * h / usize**2
area_tp_ratio = region[region>0].size / (w * h)
Expand Down

0 comments on commit c53ca1a

Please sign in to comment.