Heisenberg08 commited on
Commit
035d31c
1 Parent(s): 2756a7e

added blurring effect

Browse files
Files changed (1) hide show
  1. app.py +29 -13
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
3
 
@@ -11,27 +12,42 @@ import torchvision.transforms.functional as TF
11
  from torchvision import transforms
12
 
13
  from model import DoubleConv,UNET
14
-
15
  convert_tensor = transforms.ToTensor()
16
  device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
17
  model = UNET(in_channels=3, out_channels=1).to(device)
18
- # model=torch.load("Unet_acc_94.pth",map_location=torch.device('cpu'))
19
 
20
- model=torch.load("Unet_acc_94.pth",map_location=device)
21
 
22
  def predict(img):
23
  img=cv2.resize(img,(240,160))
24
  test_img=convert_tensor(img).unsqueeze(0)
25
- print(test_img.shape)
26
  preds=model(test_img.float())
27
  preds=torch.sigmoid(preds)
28
  preds=(preds > 0.5).float()
29
- print(preds.shape)
30
  im=preds.squeeze(0).permute(1,2,0).detach()
31
- print(im.shape)
32
  im=im.numpy()
33
  return im
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  import streamlit as st
36
  st.title("Image Colorizer")
37
 
@@ -42,11 +58,11 @@ if file is None:
42
  else:
43
  file_bytes = np.asarray(bytearray(file.read()), dtype=np.uint8)
44
  opencv_image = cv2.imdecode(file_bytes, 1)
45
- im=predict(opencv_image)
 
46
  st.text("Original")
47
- st.image(file)
48
- st.text("Colorized!!")
49
- st.image(im)
50
-
51
-
52
-
 
1
+ import imp
2
  import os
3
  os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
4
 
 
12
  from torchvision import transforms
13
 
14
  from model import DoubleConv,UNET
15
+
16
  convert_tensor = transforms.ToTensor()
17
  device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
18
  model = UNET(in_channels=3, out_channels=1).to(device)
19
+ model=torch.load("Unet_acc_94.pth",map_location=torch.device('cpu'))
20
 
21
+ # model=torch.load("src//Unet_acc_94.pth",map_location=device)
22
 
23
  def predict(img):
24
  img=cv2.resize(img,(240,160))
25
  test_img=convert_tensor(img).unsqueeze(0)
26
+ # print(test_img.shape)
27
  preds=model(test_img.float())
28
  preds=torch.sigmoid(preds)
29
  preds=(preds > 0.5).float()
30
+ # print(preds.shape)
31
  im=preds.squeeze(0).permute(1,2,0).detach()
32
+ # print(im.shape)
33
  im=im.numpy()
34
  return im
35
 
36
+ def blurr_image(input_image,preds):
37
+ mask=preds
38
+ inp=input_image
39
+ mask=np.resize(mask,(160,240))
40
+ mask=(mask>0.1)*255
41
+ mask=np.full((160,240),[mask],np.uint8)
42
+ mapping = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB)
43
+ image=cv2.resize(inp,(240,160))
44
+ blurred_original_image = cv2.GaussianBlur(image,(25,25),0)
45
+ blurred_img = np.where(mapping != (0,0,0),image,blurred_original_image)
46
+
47
+ blurred_img=cv2.cvtColor(blurred_img,cv2.COLOR_BGR2RGB)
48
+ inp=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
49
+ return inp,blurred_img
50
+
51
  import streamlit as st
52
  st.title("Image Colorizer")
53
 
 
58
  else:
59
  file_bytes = np.asarray(bytearray(file.read()), dtype=np.uint8)
60
  opencv_image = cv2.imdecode(file_bytes, 1)
61
+ pred=predict(opencv_image)
62
+ inp_img,blurred=blurr_image(opencv_image,pred)
63
  st.text("Original")
64
+ st.image(inp_img)
65
+ st.text("Mask!!")
66
+ st.image(pred)
67
+ st.text("Blurred")
68
+ st.image(blurred)