2026-09-07 01:01:29 +03:00
|
|
|
from model import DocScanner
|
|
|
|
|
from seg import U2NETP
|
|
|
|
|
|
|
|
|
|
import torch
|
|
|
|
|
import torch.nn as nn
|
|
|
|
|
import torch.nn.functional as F
|
|
|
|
|
import numpy as np
|
|
|
|
|
import cv2
|
|
|
|
|
import os
|
|
|
|
|
from PIL import Image
|
|
|
|
|
import argparse
|
|
|
|
|
import warnings
|
2026-09-07 01:07:23 +03:00
|
|
|
|
2026-09-07 01:01:29 +03:00
|
|
|
warnings.filterwarnings('ignore')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Net(nn.Module):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super(Net, self).__init__()
|
|
|
|
|
self.msk = U2NETP(3, 1)
|
2026-09-07 01:07:23 +03:00
|
|
|
self.bm = DocScanner()
|
2026-09-07 01:01:29 +03:00
|
|
|
|
|
|
|
|
def forward(self, x):
|
2026-09-07 01:07:23 +03:00
|
|
|
msk, *_ = self.msk(x)
|
2026-09-07 01:01:29 +03:00
|
|
|
msk = (msk > 0.5).float()
|
|
|
|
|
x = msk * x
|
|
|
|
|
|
|
|
|
|
bm = self.bm(x, iters=12, test_mode=True)
|
|
|
|
|
bm = (2 * (bm / 286.8) - 1) * 0.99
|
|
|
|
|
return bm
|
|
|
|
|
|
|
|
|
|
|
2026-09-07 01:07:23 +03:00
|
|
|
def load_model(model, path, strip_prefix=False):
|
|
|
|
|
"""Универсальная загрузка весов"""
|
|
|
|
|
if not path or not os.path.exists(path):
|
|
|
|
|
raise FileNotFoundError(f"Model not found: {path}")
|
|
|
|
|
|
|
|
|
|
model_dict = model.state_dict()
|
|
|
|
|
pretrained_dict = torch.load(path, map_location='cuda:0')
|
|
|
|
|
|
|
|
|
|
if strip_prefix:
|
|
|
|
|
# Для seg модели ключи имеют префикс 'module.' (6 символов)
|
2026-09-07 01:01:29 +03:00
|
|
|
pretrained_dict = {k[6:]: v for k, v in pretrained_dict.items() if k[6:] in model_dict}
|
|
|
|
|
else:
|
|
|
|
|
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
|
2026-09-07 01:07:23 +03:00
|
|
|
|
|
|
|
|
model_dict.update(pretrained_dict)
|
|
|
|
|
model.load_state_dict(model_dict)
|
|
|
|
|
return model
|
2026-09-07 01:01:29 +03:00
|
|
|
|
|
|
|
|
|
2026-09-07 01:07:23 +03:00
|
|
|
def main():
|
|
|
|
|
parser = argparse.ArgumentParser(description='DocScanner Single File Inference')
|
|
|
|
|
parser.add_argument('-i', '--input', required=True, help='Input image path')
|
|
|
|
|
parser.add_argument('-o', '--output', required=True, help='Output image path')
|
|
|
|
|
parser.add_argument('--seg_model', default='./model_pretrained/seg.pth')
|
|
|
|
|
parser.add_argument('--rec_model', default='./model_pretrained/DocScanner-L.pth')
|
|
|
|
|
opt = parser.parse_args()
|
2026-09-07 01:01:29 +03:00
|
|
|
|
2026-09-07 01:07:23 +03:00
|
|
|
# 1. Init & Load Models
|
2026-09-07 01:01:29 +03:00
|
|
|
net = Net().cuda()
|
2026-09-07 01:07:23 +03:00
|
|
|
net.msk = load_model(net.msk, opt.seg_model, strip_prefix=True)
|
|
|
|
|
net.bm = load_model(net.bm, opt.rec_model, strip_prefix=False)
|
2026-09-07 01:01:29 +03:00
|
|
|
net.eval()
|
|
|
|
|
|
2026-09-07 01:07:23 +03:00
|
|
|
# 2. Preprocess
|
|
|
|
|
im_ori = np.array(Image.open(opt.input))[:, :, :3] / 255.0
|
|
|
|
|
h, w, _ = im_ori.shape
|
|
|
|
|
|
|
|
|
|
im = cv2.resize(im_ori, (288, 288))
|
|
|
|
|
im = torch.from_numpy(im.transpose(2, 0, 1)).float().unsqueeze(0)
|
|
|
|
|
|
|
|
|
|
# 3. Inference
|
|
|
|
|
with torch.no_grad():
|
|
|
|
|
bm = net(im.cuda()).cpu()
|
|
|
|
|
|
|
|
|
|
# 4. Postprocess & Warp
|
|
|
|
|
bm0 = cv2.blur(cv2.resize(bm[0, 0].numpy(), (w, h)), (3, 3))
|
|
|
|
|
bm1 = cv2.blur(cv2.resize(bm[0, 1].numpy(), (w, h)), (3, 3))
|
|
|
|
|
|
|
|
|
|
lbl = torch.from_numpy(np.stack([bm0, bm1], axis=2)).unsqueeze(0)
|
|
|
|
|
inp_tensor = torch.from_numpy(im_ori).permute(2, 0, 1).unsqueeze(0).float()
|
|
|
|
|
|
|
|
|
|
out = F.grid_sample(inp_tensor, lbl, align_corners=True)
|
|
|
|
|
|
|
|
|
|
# 5. Save
|
|
|
|
|
result = ((out[0] * 255).permute(1, 2, 0).numpy()[:, :, ::-1]).astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
out_dir = os.path.dirname(opt.output)
|
|
|
|
|
if out_dir and not os.path.exists(out_dir):
|
|
|
|
|
os.makedirs(out_dir)
|
|
|
|
|
|
|
|
|
|
cv2.imwrite(opt.output, result)
|
|
|
|
|
print(f"[OK] Saved: {opt.output}")
|
2026-09-07 01:01:29 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|