2026-09-12 03:25:38 +03:00
|
|
|
|
import os, sys, argparse, warnings
|
|
|
|
|
|
import torch, torch.nn as nn, torch.nn.functional as F
|
|
|
|
|
|
import numpy as np, cv2
|
|
|
|
|
|
from PIL import Image
|
2026-09-12 03:25:35 +03:00
|
|
|
|
from model import DocScanner
|
|
|
|
|
|
from seg import U2NETP
|
|
|
|
|
|
|
|
|
|
|
|
warnings.filterwarnings('ignore')
|
|
|
|
|
|
|
2026-09-12 03:25:38 +03:00
|
|
|
|
# Запоминаем CWD пользователя ДО смены директории
|
|
|
|
|
|
USER_CWD = os.getcwd()
|
|
|
|
|
|
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
os.chdir(SCRIPT_DIR)
|
|
|
|
|
|
|
2026-09-12 03:25:35 +03:00
|
|
|
|
|
|
|
|
|
|
class Net(nn.Module):
|
|
|
|
|
|
def __init__(self):
|
2026-09-12 03:25:38 +03:00
|
|
|
|
super().__init__()
|
2026-09-12 03:25:35 +03:00
|
|
|
|
self.msk = U2NETP(3, 1)
|
2026-09-12 03:25:38 +03:00
|
|
|
|
self.bm = DocScanner()
|
2026-09-12 03:25:35 +03:00
|
|
|
|
|
|
|
|
|
|
def forward(self, x):
|
2026-09-12 03:25:38 +03:00
|
|
|
|
msk, *_ = self.msk(x)
|
|
|
|
|
|
bm = self.bm((msk > 0.5).float() * x, iters=12, test_mode=True)
|
|
|
|
|
|
return (2 * (bm / 286.8) - 1) * 0.99
|
2026-09-12 03:25:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
2026-09-12 03:25:38 +03:00
|
|
|
|
def load_model(model, path, strip_prefix=False):
|
|
|
|
|
|
state_dict = model.state_dict()
|
|
|
|
|
|
pretrained = torch.load(path, map_location='cuda:0')
|
|
|
|
|
|
if strip_prefix:
|
|
|
|
|
|
pretrained = {k[6:]: v for k, v in pretrained.items() if k[6:] in state_dict}
|
2026-09-12 03:25:35 +03:00
|
|
|
|
else:
|
2026-09-12 03:25:38 +03:00
|
|
|
|
pretrained = {k: v for k, v in pretrained.items() if k in state_dict}
|
|
|
|
|
|
state_dict.update(pretrained)
|
|
|
|
|
|
model.load_state_dict(state_dict)
|
|
|
|
|
|
return model
|
2026-09-12 03:25:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
2026-09-12 03:25:38 +03:00
|
|
|
|
def resolve_user_path(path):
|
|
|
|
|
|
"""Если путь относительный — считаем его относительно CWD пользователя, не скрипта"""
|
|
|
|
|
|
if os.path.isabs(path):
|
|
|
|
|
|
return path
|
|
|
|
|
|
return os.path.join(USER_CWD, path)
|
2026-09-12 03:25:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
2026-09-12 03:25:38 +03:00
|
|
|
|
def main():
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
|
parser.add_argument('-i', '--input', required=True)
|
|
|
|
|
|
parser.add_argument('-o', '--output', required=True)
|
|
|
|
|
|
opt = parser.parse_args()
|
2026-09-12 03:25:35 +03:00
|
|
|
|
|
2026-09-12 03:25:38 +03:00
|
|
|
|
input_path = resolve_user_path(opt.input)
|
|
|
|
|
|
output_path = resolve_user_path(opt.output)
|
2026-09-12 03:25:35 +03:00
|
|
|
|
|
2026-09-12 03:25:38 +03:00
|
|
|
|
net = Net().cuda().eval()
|
|
|
|
|
|
load_model(net.msk, f'{SCRIPT_DIR}/model_pretrained/seg.pth', strip_prefix=True)
|
|
|
|
|
|
load_model(net.bm, f'{SCRIPT_DIR}/model_pretrained/DocScanner-L.pth')
|
2026-09-12 03:25:35 +03:00
|
|
|
|
|
2026-09-12 03:25:38 +03:00
|
|
|
|
image = np.array(Image.open(input_path))[:, :, :3] / 255.0
|
|
|
|
|
|
height, width = image.shape[:2]
|
|
|
|
|
|
tensor = torch.from_numpy(cv2.resize(image, (288, 288)).transpose(2, 0, 1)).float().unsqueeze(0)
|
2026-09-12 03:25:35 +03:00
|
|
|
|
|
2026-09-12 03:25:38 +03:00
|
|
|
|
with torch.no_grad():
|
|
|
|
|
|
bm = net(tensor.cuda()).cpu()
|
2026-09-12 03:25:35 +03:00
|
|
|
|
|
2026-09-12 03:25:38 +03:00
|
|
|
|
flow = torch.from_numpy(np.stack([
|
|
|
|
|
|
cv2.blur(cv2.resize(bm[0, 0].numpy(), (width, height)), (3, 3)),
|
|
|
|
|
|
cv2.blur(cv2.resize(bm[0, 1].numpy(), (width, height)), (3, 3))
|
|
|
|
|
|
], axis=2)).unsqueeze(0)
|
2026-09-12 03:25:35 +03:00
|
|
|
|
|
2026-09-12 03:25:38 +03:00
|
|
|
|
out = F.grid_sample(
|
|
|
|
|
|
torch.from_numpy(image).permute(2, 0, 1).unsqueeze(0).float(),
|
|
|
|
|
|
flow, align_corners=True
|
|
|
|
|
|
)
|
|
|
|
|
|
result = ((out[0] * 255).permute(1, 2, 0).numpy()[:, :, ::-1]).astype(np.uint8)
|
2026-09-12 03:25:35 +03:00
|
|
|
|
|
2026-09-12 03:25:38 +03:00
|
|
|
|
out_dir = os.path.dirname(output_path)
|
|
|
|
|
|
if out_dir:
|
|
|
|
|
|
os.makedirs(out_dir, exist_ok=True)
|
|
|
|
|
|
cv2.imwrite(output_path, result)
|
|
|
|
|
|
print(f"[OK] {output_path}")
|
2026-09-12 03:25:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
main()
|