williamcfrancis commited on
Commit
6bc05e6
·
1 Parent(s): e22b55b

Update deblur_img.py

Browse files
Files changed (1) hide show
  1. deblur_img.py +42 -29
deblur_img.py CHANGED
@@ -53,33 +53,46 @@ def create_fft(img):
53
 
54
  return mag_spec
55
 
56
- # Change this variable with the name of the trained models.
57
- angle_model_name= args['angle_model']
58
- length_model_name= args['length_model']
59
- model1= load_model(angle_model_name)
60
- model2= load_model(length_model_name)
61
-
62
- # read blurred image
63
- ip_image = cv2.imread(args['image'])
64
- ip_image= cv2.cvtColor(ip_image, cv2.COLOR_BGR2GRAY)
65
- ip_image= cv2.resize(ip_image, (640, 480))
66
- # FFT visualization of the blurred image
67
- fft_img= create_fft(ip_image)
68
-
69
- # Predicting the psf parameters of length and angle.
70
- img= cv2.resize(create_fft(ip_image), (224,224))
71
- img= np.expand_dims(img_to_array(img), axis=0)/ 255.0
72
- preds= model1.predict(img)
73
- # angle_value= np.sum(np.multiply(np.arange(0, 180), preds[0]))
74
- angle_value = np.mean(np.argsort(preds[0])[-3:])
75
-
76
- print("Predicted Blur Angle: ", angle_value)
77
- length_value= model2.predict(img)[0][0]
78
- print("Predicted Blur Length: ",length_value)
79
-
80
- op_image = process(ip_image, length_value, angle_value)
81
- op_image = (op_image*255).astype(np.uint8)
82
- op_image = (255/(np.max(op_image)-np.min(op_image))) * (op_image-np.min(op_image))
83
-
84
- cv2.imwrite("result.jpg", op_image)
 
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()