mirror of
https://github.com/283375/arcaea-offline-ocr.git
synced 2025-04-18 13:00:18 +00:00
impr: minor improvements
This commit is contained in:
parent
5e0642c832
commit
02599780e3
@ -13,6 +13,6 @@ class DeviceOcrResult:
|
|||||||
max_recall: Optional[int] = None
|
max_recall: Optional[int] = None
|
||||||
song_id: Optional[str] = None
|
song_id: Optional[str] = None
|
||||||
song_id_possibility: Optional[float] = None
|
song_id_possibility: Optional[float] = None
|
||||||
clear_status: Optional[str] = None
|
clear_status: Optional[int] = None
|
||||||
partner_id: Optional[str] = None
|
partner_id: Optional[str] = None
|
||||||
partner_id_possibility: Optional[float] = None
|
partner_id_possibility: Optional[float] = None
|
||||||
|
@ -47,8 +47,7 @@ class DeviceOcr:
|
|||||||
continue
|
continue
|
||||||
roi_ocr = cv2.fillPoly(roi_ocr, [contour], [0])
|
roi_ocr = cv2.fillPoly(roi_ocr, [contour], [0])
|
||||||
digit_rois = [
|
digit_rois = [
|
||||||
resize_fill_square(crop_xywh(roi_ocr, r), 20)
|
resize_fill_square(crop_xywh(roi_ocr, r), 20) for r in filtered_rects
|
||||||
for r in sorted(filtered_rects, key=lambda r: r[0])
|
|
||||||
]
|
]
|
||||||
|
|
||||||
samples = preprocess_hog(digit_rois)
|
samples = preprocess_hog(digit_rois)
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import math
|
import math
|
||||||
from copy import deepcopy
|
|
||||||
from typing import Optional, Sequence, Tuple
|
from typing import Optional, Sequence, Tuple
|
||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
@ -64,8 +63,7 @@ class FixRects:
|
|||||||
new_h = new_bottom - new_y
|
new_h = new_bottom - new_y
|
||||||
new_rects.append((new_x, new_y, new_w, new_h))
|
new_rects.append((new_x, new_y, new_w, new_h))
|
||||||
|
|
||||||
return_rects = deepcopy(rects)
|
return_rects = [r for r in rects if r not in consumed_rects]
|
||||||
return_rects = [r for r in return_rects if r not in consumed_rects]
|
|
||||||
return_rects.extend(new_rects)
|
return_rects.extend(new_rects)
|
||||||
return return_rects
|
return return_rects
|
||||||
|
|
||||||
@ -80,42 +78,42 @@ class FixRects:
|
|||||||
new_rects = []
|
new_rects = []
|
||||||
for rect in rects:
|
for rect in rects:
|
||||||
rx, ry, rw, rh = rect
|
rx, ry, rw, rh = rect
|
||||||
if rw / rh > rect_wh_ratio:
|
if rw / rh <= rect_wh_ratio:
|
||||||
# consider this is a connected contour
|
continue
|
||||||
connected_rects.append(rect)
|
|
||||||
|
|
||||||
# find the thinnest part
|
connected_rects.append(rect)
|
||||||
border_ignore = round(rw * width_range_ratio)
|
|
||||||
img_cropped = crop_xywh(
|
|
||||||
img_masked,
|
|
||||||
(border_ignore, ry, rw - border_ignore, rh),
|
|
||||||
)
|
|
||||||
white_pixels = {} # dict[x, white_pixel_number]
|
|
||||||
for i in range(img_cropped.shape[1]):
|
|
||||||
col = img_cropped[:, i]
|
|
||||||
white_pixels[rx + border_ignore + i] = np.count_nonzero(col > 200)
|
|
||||||
least_white_pixels = min(v for v in white_pixels.values() if v > 0)
|
|
||||||
x_values = [
|
|
||||||
x
|
|
||||||
for x, pixel in white_pixels.items()
|
|
||||||
if pixel == least_white_pixels
|
|
||||||
]
|
|
||||||
# select only middle values
|
|
||||||
x_mean = np.mean(x_values)
|
|
||||||
x_std = np.std(x_values)
|
|
||||||
x_values = [
|
|
||||||
x
|
|
||||||
for x in x_values
|
|
||||||
if x_mean - x_std * 1.5 <= x <= x_mean + x_std * 1.5
|
|
||||||
]
|
|
||||||
x_mid = round(np.median(x_values))
|
|
||||||
|
|
||||||
# split the rect
|
# find the thinnest part
|
||||||
new_rects.extend(
|
border_ignore = round(rw * width_range_ratio)
|
||||||
[(rx, ry, x_mid - rx, rh), (x_mid, ry, rx + rw - x_mid, rh)]
|
img_cropped = crop_xywh(
|
||||||
)
|
img_masked,
|
||||||
|
(border_ignore, ry, rw - border_ignore, rh),
|
||||||
|
)
|
||||||
|
white_pixels = {} # dict[x, white_pixel_number]
|
||||||
|
for i in range(img_cropped.shape[1]):
|
||||||
|
col = img_cropped[:, i]
|
||||||
|
white_pixels[rx + border_ignore + i] = np.count_nonzero(col > 200)
|
||||||
|
|
||||||
|
if all(v == 0 for v in white_pixels.values()):
|
||||||
|
return rects
|
||||||
|
|
||||||
|
least_white_pixels = min(v for v in white_pixels.values() if v > 0)
|
||||||
|
x_values = [
|
||||||
|
x for x, pixel in white_pixels.items() if pixel == least_white_pixels
|
||||||
|
]
|
||||||
|
# select only middle values
|
||||||
|
x_mean = np.mean(x_values)
|
||||||
|
x_std = np.std(x_values)
|
||||||
|
x_values = [
|
||||||
|
x for x in x_values if x_mean - x_std * 1.5 <= x <= x_mean + x_std * 1.5
|
||||||
|
]
|
||||||
|
x_mid = round(np.median(x_values))
|
||||||
|
|
||||||
|
# split the rect
|
||||||
|
new_rects.extend(
|
||||||
|
[(rx, ry, x_mid - rx, rh), (x_mid, ry, rx + rw - x_mid, rh)]
|
||||||
|
)
|
||||||
|
|
||||||
return_rects = deepcopy(rects)
|
|
||||||
return_rects = [r for r in rects if r not in connected_rects]
|
return_rects = [r for r in rects if r not in connected_rects]
|
||||||
return_rects.extend(new_rects)
|
return_rects.extend(new_rects)
|
||||||
return return_rects
|
return return_rects
|
||||||
|
Loading…
x
Reference in New Issue
Block a user