-
Notifications
You must be signed in to change notification settings - Fork 0
/
deNoise.py
97 lines (80 loc) · 2.78 KB
/
deNoise.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import cv2
import os
import argparse
import tensorflow as tf
from tensorflow import keras
import numpy as np
import PIL
IMG_HEIGHT = 128
IMG_WIDTH = 128
INPUT_CHANNELS = 1
def path2img(path):
img = PIL.Image.open(path)
img = img.resize((IMG_WIDTH, IMG_HEIGHT))
return img
def path2imgarray(path, binary=True):
if INPUT_CHANNELS == 1:
binary = True
img = PIL.Image.open(path)
img = img.resize((IMG_WIDTH, IMG_HEIGHT))
img = np.array(img)
def binaryzation(img):
cv_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
return cv_img
img = binaryzation(img) if binary else img
img = img / 255.0
img = img.reshape(IMG_HEIGHT, IMG_WIDTH, (1 if binary else 3))
return img
class AutoEncoder(keras.Model):
def __init__(self):
layers = keras.layers
super().__init__()
self.encoder = tf.keras.Sequential([
layers.Input(shape=(IMG_HEIGHT, IMG_WIDTH, INPUT_CHANNELS)),
layers.Conv2D(16, (3, 3), activation='relu',
padding='same', strides=2),
layers.Conv2D(8, (3, 3), activation='relu',
padding='same', strides=2),
])
self.decoder = tf.keras.Sequential([
layers.Conv2DTranspose(
8, kernel_size=3, strides=2, activation='relu', padding='same'),
layers.Conv2DTranspose(
16, kernel_size=3, strides=2, activation='relu', padding='same'),
layers.Conv2D(1, kernel_size=(3, 3),
activation='sigmoid', padding='same')
])
def call(self, x):
return self.decoder(self.encoder(x))
def encode(self, x):
return self.encoder(x)
def decode(self, x):
return self.decoder(x)
parser = argparse.ArgumentParser(description='captcha')
parser.add_argument('-o', '--outdir', type=str,
default='./dataset', help='output dir')
parser.add_argument('-i', '--inputdir', type=str, default=100,
help='inputdir')
parser.add_argument('-m', '--model', type=str, required=True,)
args = parser.parse_args()
outdir = args.outdir
inputdir = args.inputdir
modelSaved = args.model
model = AutoEncoder()
model.build(input_shape=(None, IMG_HEIGHT, IMG_WIDTH, INPUT_CHANNELS))
# print(model.summary())
# exit()
model.load_weights(modelSaved)
if not os.path.exists(inputdir):
raise Exception('inputdir not exists')
if not os.path.exists(outdir):
os.makedirs(outdir)
jpglist = os.listdir(inputdir)
for jpg in jpglist:
img = path2imgarray(os.path.join(inputdir, jpg))
img = img.reshape(1, IMG_HEIGHT, IMG_WIDTH, INPUT_CHANNELS)
img = model(img)
img = (np.array(img)*255).astype(np.uint8)
cv2.imwrite(os.path.join(outdir, jpg), img[0])
# print(os.path.join(outdir, jpg))
# exit()