itacaiunas commited on
Commit
f2c74a2
1 Parent(s): 1c4f0a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -73
app.py CHANGED
@@ -46,82 +46,81 @@ def set_image(img):
46
 
47
  st.title("AI Remover Objetos")
48
 
49
- with st.beta_container():
50
- st.image(open("assets/demo.png", "rb").read())
51
- st.markdown(
52
- """
53
- So you want to remove an object in your photo? You don't need to learn photo editing skills.
54
- **Just draw over the parts of the image you want to remove, then our AI will remove them.**
55
- """
56
- )
57
-
58
- uploaded_file = st.file_uploader("Choose image", accept_multiple_files=False, type=["png", "jpg", "jpeg"])
59
-
60
- if uploaded_file is not None:
61
-
62
- if st.session_state.reuse_image is not None:
63
- img_input = Image.fromarray(st.session_state.reuse_image)
64
- else:
65
- bytes_data = uploaded_file.getvalue()
66
- img_input = Image.open(BytesIO(bytes_data)).convert("RGBA")
67
-
68
- stroke_width = st.slider("Brush size", 1, 100, 50)
69
 
70
- st.write("**Now draw (brush) the part of image that you want to remove.**")
 
 
 
 
 
 
71
 
72
- # Canvas size logic
73
- canvas_bg = deepcopy(img_input)
74
- aspect_ratio = canvas_bg.width / canvas_bg.height
75
- streamlit_width = 720
 
 
 
76
 
77
- # Max width is 720. Resize the height to maintain its aspectratio.
78
- if canvas_bg.width > streamlit_width:
79
- canvas_bg = canvas_bg.resize((streamlit_width, int(streamlit_width / aspect_ratio)))
80
 
81
- canvas_result = st_canvas(
82
- stroke_color="rgba(255, 0, 255, 1)",
83
- stroke_width=stroke_width,
84
- background_image=canvas_bg,
85
- width=canvas_bg.width,
86
- height=canvas_bg.height,
87
- drawing_mode="freedraw",
88
- key="compute_arc_length",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  )
90
-
91
- if canvas_result.image_data is not None:
92
- im = np.array(Image.fromarray(canvas_result.image_data.astype(np.uint8)).resize(img_input.size))
93
- background = np.where(
94
- (im[:, :, 0] == 0) &
95
- (im[:, :, 1] == 0) &
96
- (im[:, :, 2] == 0)
97
- )
98
- drawing = np.where(
99
- (im[:, :, 0] == 255) &
100
- (im[:, :, 1] == 0) &
101
- (im[:, :, 2] == 255)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  )
103
- im[background]=[0,0,0,255]
104
- im[drawing]=[0,0,0,0] # RGBA
105
-
106
- reuse = False
107
-
108
- if st.button('Submit'):
109
-
110
- with st.spinner("AI is doing the magic!"):
111
- output = process_inpaint(np.array(img_input), np.array(im)) #TODO Put button here
112
- img_output = Image.fromarray(output).convert("RGB")
113
-
114
- st.write("AI has finished the job!")
115
- st.image(img_output)
116
- # reuse = st.button('Edit again (Re-use this image)', on_click=set_image, args=(inpainted_img, ))
117
-
118
- uploaded_name = os.path.splitext(uploaded_file.name)[0]
119
- image_download_button(
120
- pil_image=img_output,
121
- filename=uploaded_name,
122
- fmt="jpg",
123
- label="Download Image"
124
- )
125
-
126
- st.info("**TIP**: If the result is not perfect, you can download it then "
127
- "upload then remove the artifacts.")
 
46
 
47
  st.title("AI Remover Objetos")
48
 
49
+ st.image(open("assets/demo.png", "rb").read())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
+ st.markdown(
52
+ """
53
+ So you want to remove an object in your photo? You don't need to learn photo editing skills.
54
+ **Just draw over the parts of the image you want to remove, then our AI will remove them.**
55
+ """
56
+ )
57
+ uploaded_file = st.file_uploader("Choose image", accept_multiple_files=False, type=["png", "jpg", "jpeg"])
58
 
59
+ if uploaded_file is not None:
60
+
61
+ if st.session_state.reuse_image is not None:
62
+ img_input = Image.fromarray(st.session_state.reuse_image)
63
+ else:
64
+ bytes_data = uploaded_file.getvalue()
65
+ img_input = Image.open(BytesIO(bytes_data)).convert("RGBA")
66
 
67
+ stroke_width = st.slider("Brush size", 1, 100, 50)
 
 
68
 
69
+ st.write("**Now draw (brush) the part of image that you want to remove.**")
70
+
71
+ # Canvas size logic
72
+ canvas_bg = deepcopy(img_input)
73
+ aspect_ratio = canvas_bg.width / canvas_bg.height
74
+ streamlit_width = 720
75
+
76
+ # Max width is 720. Resize the height to maintain its aspectratio.
77
+ if canvas_bg.width > streamlit_width:
78
+ canvas_bg = canvas_bg.resize((streamlit_width, int(streamlit_width / aspect_ratio)))
79
+
80
+ canvas_result = st_canvas(
81
+ stroke_color="rgba(255, 0, 255, 1)",
82
+ stroke_width=stroke_width,
83
+ background_image=canvas_bg,
84
+ width=canvas_bg.width,
85
+ height=canvas_bg.height,
86
+ drawing_mode="freedraw",
87
+ key="compute_arc_length",
88
+ )
89
+
90
+ if canvas_result.image_data is not None:
91
+ im = np.array(Image.fromarray(canvas_result.image_data.astype(np.uint8)).resize(img_input.size))
92
+ background = np.where(
93
+ (im[:, :, 0] == 0) &
94
+ (im[:, :, 1] == 0) &
95
+ (im[:, :, 2] == 0)
96
  )
97
+ drawing = np.where(
98
+ (im[:, :, 0] == 255) &
99
+ (im[:, :, 1] == 0) &
100
+ (im[:, :, 2] == 255)
101
+ )
102
+ im[background]=[0,0,0,255]
103
+ im[drawing]=[0,0,0,0] # RGBA
104
+
105
+ reuse = False
106
+
107
+ if st.button('Submit'):
108
+
109
+ with st.spinner("AI is doing the magic!"):
110
+ output = process_inpaint(np.array(img_input), np.array(im)) #TODO Put button here
111
+ img_output = Image.fromarray(output).convert("RGB")
112
+
113
+ st.write("AI has finished the job!")
114
+ st.image(img_output)
115
+ # reuse = st.button('Edit again (Re-use this image)', on_click=set_image, args=(inpainted_img, ))
116
+
117
+ uploaded_name = os.path.splitext(uploaded_file.name)[0]
118
+ image_download_button(
119
+ pil_image=img_output,
120
+ filename=uploaded_name,
121
+ fmt="jpg",
122
+ label="Download Image"
123
  )
124
+
125
+ st.info("**TIP**: If the result is not perfect, you can download it then "
126
+ "upload then remove the artifacts.")