Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,69 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: apache-2.0
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
datasets:
|
| 4 |
+
- open-thoughts/OpenThoughts-114k
|
| 5 |
+
language:
|
| 6 |
+
- aa
|
| 7 |
+
metrics:
|
| 8 |
+
- accuracy
|
| 9 |
+
base_model:
|
| 10 |
+
- deepseek-ai/DeepSeek-R1
|
| 11 |
+
new_version: deepseek-ai/DeepSeek-R1
|
| 12 |
+
pipeline_tag: image-to-image
|
| 13 |
+
library_name: adapter-transformers
|
| 14 |
+
---
|
| 15 |
+
import cv2
|
| 16 |
+
import numpy as np
|
| 17 |
+
import torch
|
| 18 |
+
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 19 |
+
from realesrgan import RealESRGANer
|
| 20 |
+
from gfpgan import GFPGANer
|
| 21 |
+
|
| 22 |
+
class ImageRestorer:
|
| 23 |
+
def __init__(self, device='cuda'):
|
| 24 |
+
# 初始化设备
|
| 25 |
+
self.device = torch.device(device)
|
| 26 |
+
|
| 27 |
+
# 加载超分辨率模型
|
| 28 |
+
self.upsampler = RealESRGANer(
|
| 29 |
+
scale=4,
|
| 30 |
+
model_path='weights/RealESRGAN_x4plus.pth',
|
| 31 |
+
model=RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32),
|
| 32 |
+
tile=400,
|
| 33 |
+
tile_pad=10,
|
| 34 |
+
pre_pad=0,
|
| 35 |
+
half=True
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# 加载面部增强模型
|
| 39 |
+
self.face_enhancer = GFPGANer(
|
| 40 |
+
model_path='weights/GFPGANv1.4.pth',
|
| 41 |
+
upscale=4,
|
| 42 |
+
arch='clean',
|
| 43 |
+
channel_multiplier=2,
|
| 44 |
+
bg_upsampler=self.upsampler
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
def restore_image(self, input_path, output_path, face_enhance=True):
|
| 48 |
+
# 读取图像
|
| 49 |
+
img = cv2.imread(input_path, cv2.IMREAD_UNCHANGED)
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
if face_enhance:
|
| 53 |
+
# 执行面部增强
|
| 54 |
+
_, _, output = self.face_enhancer.enhance(
|
| 55 |
+
img,
|
| 56 |
+
has_aligned=False,
|
| 57 |
+
only_center_face=False,
|
| 58 |
+
paste_back=True
|
| 59 |
+
)
|
| 60 |
+
else:
|
| 61 |
+
# 普通超分辨率处理
|
| 62 |
+
output, _ = self.upsampler.enhance(img, outscale=4)
|
| 63 |
+
|
| 64 |
+
# 保存结果
|
| 65 |
+
cv2.imwrite(output_path, output)
|
| 66 |
+
print(f"图像已保存至 {output_path}")
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"处理出错: {e}")
|