Spaces:
Runtime error
Runtime error
williamcfrancis
commited on
Commit
·
6bc05e6
1
Parent(s):
e22b55b
Update deblur_img.py
Browse files- deblur_img.py +42 -29
deblur_img.py
CHANGED
@@ -53,33 +53,46 @@ def create_fft(img):
|
|
53 |
|
54 |
return mag_spec
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
ip_image=
|
65 |
-
ip_image=
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
img=
|
72 |
-
|
73 |
-
|
74 |
-
angle_value
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
op_image = (
|
82 |
-
op_image = (255
|
83 |
-
|
84 |
-
|
|
|
85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
return mag_spec
|
55 |
|
56 |
+
def deblur_img(ip_image)
|
57 |
+
# Change this variable with the name of the trained models.
|
58 |
+
angle_model_name= 'pretrained_models/angle_model.hdf5'
|
59 |
+
length_model_name= 'pretrained_models/length_model.hdf5'
|
60 |
+
model1= load_model(angle_model_name)
|
61 |
+
model2= load_model(length_model_name)
|
62 |
+
|
63 |
+
# read blurred image
|
64 |
+
# ip_image = cv2.imread(args['image'])
|
65 |
+
# ip_image= cv2.cvtColor(ip_image, cv2.COLOR_BGR2GRAY)
|
66 |
+
ip_image= cv2.resize(ip_image, (640, 480))
|
67 |
+
# FFT visualization of the blurred image
|
68 |
+
fft_img= create_fft(ip_image)
|
69 |
+
|
70 |
+
# Predicting the psf parameters of length and angle.
|
71 |
+
img= cv2.resize(create_fft(ip_image), (224,224))
|
72 |
+
img= np.expand_dims(img_to_array(img), axis=0)/ 255.0
|
73 |
+
preds= model1.predict(img)
|
74 |
+
# angle_value= np.sum(np.multiply(np.arange(0, 180), preds[0]))
|
75 |
+
angle_value = np.mean(np.argsort(preds[0])[-3:])
|
76 |
+
|
77 |
+
# print("Predicted Blur Angle: ", angle_value)
|
78 |
+
length_value= model2.predict(img)[0][0]
|
79 |
+
# print("Predicted Blur Length: ",length_value)
|
80 |
+
|
81 |
+
op_image = process(ip_image, length_value, angle_value)
|
82 |
+
op_image = (op_image*255).astype(np.uint8)
|
83 |
+
op_image = (255/(np.max(op_image)-np.min(op_image))) * (op_image-np.min(op_image))
|
84 |
+
|
85 |
+
return op_image
|
86 |
|
87 |
+
css = ".output-image, .input-image, .image-preview {height: 480px !important} "
|
88 |
+
gr.Interface(
|
89 |
+
fn=deblur_img,
|
90 |
+
inputs=[
|
91 |
+
gr.inputs.Image(type="pil", label="Input Image"),
|
92 |
+
],
|
93 |
+
outputs="image",
|
94 |
+
title="Image Upscaling 🦆",
|
95 |
+
description="Model: [Anchor-based Plain Net for Mobile Image Super-Resolution](https://arxiv.org/abs/2105.09750). Repository: [SR Mobile PyTorch](https://github.com/w11wo/sr_mobile_pytorch)",
|
96 |
+
allow_flagging="never",
|
97 |
+
css=css,
|
98 |
+
).launch()
|