Skip to content

Commit b5d17f9

Browse files
committed
fix: aptching
1 parent 8f1f88d commit b5d17f9

File tree

4 files changed

+11
-39
lines changed

4 files changed

+11
-39
lines changed

__pycache__/utils.cpython-310.pyc

-860 Bytes
Binary file not shown.

app.py

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def denoise():
7474
os.unlink(filepath)
7575
filepath = os.path.join(app.config["UPLOAD_FOLDER"], hash + ".jpg")
7676
if hash in denoised_images:
77+
print("id alread in dict")
7778
return {"id": hash}, 200
7879
announcer = ServerSentEvents()
7980
denoised_images[hash] = announcer

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ six==1.16.0
1414
Werkzeug==3.0.2
1515
zope.event==5.0
1616
zope.interface==6.3
17+
empatches==0.2.3

utils.py

+9-39
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
from cgi import test
12
import cv2
23
from flask import url_for
34
import numpy as np
45
import tensorflow as tf
56
import hashlib
67
from flask_queue_sse import ServerSentEvents
78

9+
from empatches import EMPatches
10+
11+
emp = EMPatches()
12+
813

914
def format_sse(data: str, event=None) -> str:
1015
"""Formats a string and an event name in order to follow the event stream convention.
@@ -36,42 +41,6 @@ def announce_progress(announcer, msg):
3641
announcer.send(msg)
3742

3843

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-
7544
def ssim_loss(y_true, y_pred):
7645
return 1 - tf.reduce_mean(tf.image.ssim(y_true, y_pred, 1.0))
7746

@@ -90,16 +59,17 @@ def PSNR(y_true, y_pred):
9059
async def predict(
9160
id: str, image_path: str, save_path: str, announcer: ServerSentEvents
9261
):
93-
patches = get_patches(image_path, 40, [1])
9462
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)
9565

9666
patches = np.array(patches)
9767
patches = patches.astype("float32") / 255.0
9868
patches_noisy = patches
9969
patches_noisy = tf.clip_by_value(
10070
patches_noisy, clip_value_min=0.0, clip_value_max=1.0
10171
)
102-
noisy_image = create_image_from_patches(patches, test_image.shape) / 255.0
72+
noisy_image = test_image / 255.0
10373
denoised_patches = model.predict(
10474
patches_noisy, callbacks=[ServerSentEventsCallback(announcer, len(patches))]
10575
)
@@ -109,7 +79,7 @@ async def predict(
10979
)
11080

11181
# 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")
11383
cv2.imwrite(
11484
save_path,
11585
cv2.cvtColor((255 * denoised_image).astype("uint8"), cv2.COLOR_RGB2BGR),

0 commit comments

Comments
 (0)