1
+ from cgi import test
1
2
import cv2
2
3
from flask import url_for
3
4
import numpy as np
4
5
import tensorflow as tf
5
6
import hashlib
6
7
from flask_queue_sse import ServerSentEvents
7
8
9
+ from empatches import EMPatches
10
+
11
+ emp = EMPatches ()
12
+
8
13
9
14
def format_sse (data : str , event = None ) -> str :
10
15
"""Formats a string and an event name in order to follow the event stream convention.
@@ -36,42 +41,6 @@ def announce_progress(announcer, msg):
36
41
announcer .send (msg )
37
42
38
43
39
- def get_patches (file_name , patch_size , crop_sizes ):
40
- """This functions creates and return patches of given image with a specified patch_size"""
41
- image = cv2 .imread (file_name )
42
- image = cv2 .cvtColor (image , cv2 .COLOR_BGR2RGB )
43
- height , width , channels = image .shape
44
- patches = []
45
- for crop_size in crop_sizes : # We will crop the image to different sizes
46
- crop_h , crop_w = int (height * crop_size ), int (width * crop_size )
47
- image_scaled = cv2 .resize (
48
- image , (crop_w , crop_h ), interpolation = cv2 .INTER_CUBIC
49
- )
50
- for i in range (0 , crop_h - patch_size + 1 , patch_size ):
51
- for j in range (0 , crop_w - patch_size + 1 , patch_size ):
52
- x = image_scaled [
53
- i : i + patch_size , j : j + patch_size
54
- ] # This gets the patch from the original image with size patch_size x patch_size
55
- patches .append (x )
56
- return patches
57
-
58
-
59
- def create_image_from_patches (patches , image_shape ):
60
- """This function takes the patches of images and reconstructs the image"""
61
- image = np .zeros (
62
- image_shape
63
- ) # Create a image with all zeros with desired image shape
64
- patch_size = patches .shape [1 ]
65
- p = 0
66
- for i in range (0 , image .shape [0 ] - patch_size + 1 , patch_size ):
67
- for j in range (0 , image .shape [1 ] - patch_size + 1 , patch_size ):
68
- image [i : i + patch_size , j : j + patch_size ] = patches [
69
- p
70
- ] # Assigning values of pixels from patches to image
71
- p += 1
72
- return np .array (image )
73
-
74
-
75
44
def ssim_loss (y_true , y_pred ):
76
45
return 1 - tf .reduce_mean (tf .image .ssim (y_true , y_pred , 1.0 ))
77
46
@@ -90,16 +59,17 @@ def PSNR(y_true, y_pred):
90
59
async def predict (
91
60
id : str , image_path : str , save_path : str , announcer : ServerSentEvents
92
61
):
93
- patches = get_patches (image_path , 40 , [1 ])
94
62
test_image = cv2 .imread (image_path )
63
+ test_image = cv2 .cvtColor (test_image , cv2 .COLOR_BGR2RGB )
64
+ patches , indices = emp .extract_patches (test_image , patchsize = 40 , overlap = 0.2 )
95
65
96
66
patches = np .array (patches )
97
67
patches = patches .astype ("float32" ) / 255.0
98
68
patches_noisy = patches
99
69
patches_noisy = tf .clip_by_value (
100
70
patches_noisy , clip_value_min = 0.0 , clip_value_max = 1.0
101
71
)
102
- noisy_image = create_image_from_patches ( patches , test_image . shape ) / 255.0
72
+ noisy_image = test_image / 255.0
103
73
denoised_patches = model .predict (
104
74
patches_noisy , callbacks = [ServerSentEventsCallback (announcer , len (patches ))]
105
75
)
@@ -109,7 +79,7 @@ async def predict(
109
79
)
110
80
111
81
# Creating entire denoised image from denoised patches
112
- denoised_image = create_image_from_patches (denoised_patches , test_image . shape )
82
+ denoised_image = emp . merge_patches (denoised_patches , indices , mode = "avg" )
113
83
cv2 .imwrite (
114
84
save_path ,
115
85
cv2 .cvtColor ((255 * denoised_image ).astype ("uint8" ), cv2 .COLOR_RGB2BGR ),
0 commit comments