prithivMLmods commited on
Commit
3e79fce
·
verified ·
1 Parent(s): 421bdf5

Delete src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +0 -130
src/streamlit_app.py DELETED
@@ -1,130 +0,0 @@
1
- import numpy as np
2
- import pandas as pd
3
- import streamlit as st
4
- import os
5
- from datetime import datetime
6
- from PIL import Image
7
- from streamlit_drawable_canvas import st_canvas
8
- from io import BytesIO
9
- from copy import deepcopy
10
-
11
- from src.core import process_inpaint
12
-
13
-
14
- def image_download_button(pil_image, filename: str, fmt: str, label="Download"):
15
- if fmt not in ["jpg", "png"]:
16
- raise Exception(f"Unknown image format (Available: {fmt} - case sensitive)")
17
-
18
- pil_format = "JPEG" if fmt == "jpg" else "PNG"
19
- file_format = "jpg" if fmt == "jpg" else "png"
20
- mime = "image/jpeg" if fmt == "jpg" else "image/png"
21
-
22
- buf = BytesIO()
23
- pil_image.save(buf, format=pil_format)
24
-
25
- return st.download_button(
26
- label=label,
27
- data=buf.getvalue(),
28
- file_name=f'{filename}.{file_format}',
29
- mime=mime,
30
- )
31
-
32
- if "button_id" not in st.session_state:
33
- st.session_state["button_id"] = ""
34
- if "color_to_label" not in st.session_state:
35
- st.session_state["color_to_label"] = {}
36
-
37
- if 'reuse_image' not in st.session_state:
38
- st.session_state.reuse_image = None
39
- def set_image(img):
40
- st.session_state.reuse_image = img
41
-
42
- st.title("Magic Eraser")
43
-
44
- st.image(open("assets/magic-eraser.png", "rb").read())
45
-
46
- st.markdown(
47
- """
48
- You don't have to worry about mastering photo editing techniques to remove an object from your photo. **Simply mark over the areas you want to erase, and our AI will take care of the rest.**
49
- """
50
- )
51
- uploaded_file = st.file_uploader("Choose image", accept_multiple_files=False, type=["png", "jpg", "jpeg"])
52
-
53
- if uploaded_file is not None:
54
-
55
- if st.session_state.reuse_image is not None:
56
- img_input = Image.fromarray(st.session_state.reuse_image)
57
- else:
58
- bytes_data = uploaded_file.getvalue()
59
- img_input = Image.open(BytesIO(bytes_data)).convert("RGBA")
60
-
61
- #resize
62
- max_size = 2000
63
- img_width, img_height = img_input.size
64
- if img_width > max_size or img_height > max_size:
65
- if img_width > img_height:
66
- new_width = max_size
67
- new_height = int((max_size / img_width) * img_height)
68
- else:
69
- new_height = max_size
70
- new_width = int((max_size / img_height) * img_width)
71
- img_input = img_input.resize((new_width, new_height))
72
-
73
- stroke_width = st.slider("Brush size", 1, 100, 50)
74
-
75
- st.write("**Now draw (brush) the part of image that you want to remove.**")
76
-
77
- canvas_bg = deepcopy(img_input)
78
- aspect_ratio = canvas_bg.width / canvas_bg.height
79
- streamlit_width = 720
80
-
81
- if canvas_bg.width > streamlit_width:
82
- canvas_bg = canvas_bg.resize((streamlit_width, int(streamlit_width / aspect_ratio)))
83
-
84
- canvas_result = st_canvas(
85
- stroke_color="rgba(255, 0, 255, 1)",
86
- stroke_width=stroke_width,
87
- background_image=canvas_bg,
88
- width=canvas_bg.width,
89
- height=canvas_bg.height,
90
- drawing_mode="freedraw",
91
- key="compute_arc_length",
92
- )
93
-
94
- if canvas_result.image_data is not None:
95
- im = np.array(Image.fromarray(canvas_result.image_data.astype(np.uint8)).resize(img_input.size))
96
- background = np.where(
97
- (im[:, :, 0] == 0) &
98
- (im[:, :, 1] == 0) &
99
- (im[:, :, 2] == 0)
100
- )
101
- drawing = np.where(
102
- (im[:, :, 0] == 255) &
103
- (im[:, :, 1] == 0) &
104
- (im[:, :, 2] == 255)
105
- )
106
- im[background]=[0,0,0,255]
107
- im[drawing]=[0,0,0,0] # RGBA
108
-
109
- reuse = False
110
-
111
- if st.button('Submit'):
112
-
113
- with st.spinner("AI is doing the magic!"):
114
- output = process_inpaint(np.array(img_input), np.array(im)) #TODO Put button here
115
- img_output = Image.fromarray(output).convert("RGB")
116
-
117
- st.write("AI has finished the job!")
118
- st.image(img_output)
119
- # reuse = st.button('Edit again (Re-use this image)', on_click=set_image, args=(inpainted_img, ))
120
-
121
- uploaded_name = os.path.splitext(uploaded_file.name)[0]
122
- image_download_button(
123
- pil_image=img_output,
124
- filename=uploaded_name,
125
- fmt="jpg",
126
- label="Download Image"
127
- )
128
-
129
- st.info("**TIP**: If the result is not perfect, you can download it then "
130
- "upload then remove the artifacts.")