Repcak00 commited on
Commit
e333748
·
verified ·
1 Parent(s): a188886

Polish version

Browse files
Files changed (1) hide show
  1. src/app.py +153 -147
src/app.py CHANGED
@@ -1,147 +1,153 @@
1
- import io
2
- import zipfile
3
- from datetime import datetime
4
-
5
- import streamlit as st
6
- from rembg import new_session
7
-
8
- from utils import load_and_process_image
9
-
10
- st.set_page_config(page_title="Background Remover", layout="wide")
11
-
12
- st.title("🎨 Background Removal & Color Replace")
13
- st.write("Upload images, remove their backgrounds, and replace with your chosen color!")
14
-
15
- # Options
16
- col_a, col_b, col_c = st.columns(3)
17
-
18
- with col_a:
19
- bg_color = st.color_picker("Pick a background color", "#FFFFFF")
20
-
21
- with col_b:
22
- model_choice = st.selectbox(
23
- "AI Model:",
24
- [
25
- "birefnet-general-lite",
26
- "birefnet-general",
27
- "birefnet-dis", # Best for objects with holes
28
- "isnet-general-use",
29
- "u2net",
30
- "birefnet-massive",
31
- "bria-rmbg",
32
- ],
33
- index=0,
34
- )
35
-
36
- with col_c:
37
- transparency_mode = st.radio("Transparency:", ["Keep transparent", "Fill with color"], index=1)
38
-
39
- with st.sidebar:
40
- st.subheader("Watermark Options")
41
- add_watermark = st.checkbox("Add watermark", value=True)
42
- watermark_file = st.file_uploader("Upload watermark (PNG)", type=["png"], key="wm")
43
- wm_opacity = st.slider("Watermark opacity", 0.0, 1.0, 0.3)
44
- wm_scale = st.slider("Watermark size (fraction of width)", 0.1, 1.0, 0.33)
45
- wm_position = st.selectbox(
46
- "Position", ["bottom-right", "bottom-left", "top-right", "top-left"]
47
- )
48
-
49
-
50
- # Convert hex to RGB
51
- hex_color = bg_color.lstrip("#")
52
- rgb_color = tuple(int(hex_color[i : i + 2], 16) for i in (0, 2, 4))
53
-
54
- # Multiple file uploader
55
- uploaded_files = st.file_uploader(
56
- "Choose images", type=["png", "jpg", "jpeg"], accept_multiple_files=True
57
- )
58
-
59
- if uploaded_files:
60
- st.write(f"📁 {len(uploaded_files)} image(s) uploaded")
61
-
62
- # Process button
63
- if st.button("🚀 Process All Images", type="primary"):
64
- # Create containers for progress tracking
65
- progress_bar = st.progress(0)
66
- status_text = st.empty()
67
-
68
- # Create session with selected model (reuse for all images)
69
- status_text.text(f"Loading model: {model_choice}...")
70
- session = new_session(model_choice)
71
-
72
- processed_images = []
73
-
74
- # Process each image
75
- for idx, uploaded_file in enumerate(uploaded_files):
76
- # Update progress
77
- progress = (idx + 1) / len(uploaded_files)
78
- progress_bar.progress(progress)
79
- status_text.text(f"Processing {idx + 1}/{len(uploaded_files)}: {uploaded_file.name}")
80
-
81
- input_img, processed_img = load_and_process_image(
82
- uploaded_file,
83
- session=session,
84
- transparency_mode=transparency_mode,
85
- rgb_color=rgb_color,
86
- add_watermark=add_watermark,
87
- watermark_file=watermark_file if watermark_file else None,
88
- wm_opacity=wm_opacity,
89
- wm_scale=wm_scale,
90
- wm_position=wm_position,
91
- )
92
-
93
- # Store processed image
94
- processed_images.append(
95
- {"name": uploaded_file.name, "original": input_img, "processed": processed_img}
96
- )
97
-
98
- status_text.text("✅ All images processed!")
99
-
100
- # Display results in a grid
101
- st.markdown("---")
102
- st.subheader("Results")
103
-
104
- # Show images in pairs
105
- for img_data in processed_images:
106
- col1, col2 = st.columns(2)
107
-
108
- with col1:
109
- st.text(f"Original: {img_data['name']}")
110
- st.image(img_data["original"], width="stretch")
111
-
112
- with col2:
113
- st.text(f"Processed: {img_data['name']}")
114
- st.image(img_data["processed"], width="stretch")
115
-
116
- st.markdown("---")
117
-
118
- # Create ZIP file with all processed images
119
- zip_buffer = io.BytesIO()
120
- with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:
121
- for img_data in processed_images:
122
- # Save each image to bytes
123
- img_buffer = io.BytesIO()
124
-
125
- # Save as PNG to preserve transparency
126
- img_data["processed"].save(img_buffer, format="PNG")
127
- img_bytes = img_buffer.getvalue()
128
-
129
- # Add to ZIP
130
- filename = f"processed_{img_data['name'].rsplit('.', 1)[0]}.png"
131
- zip_file.writestr(filename, img_bytes)
132
-
133
- zip_buffer.seek(0)
134
-
135
- # Download button for ZIP
136
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
137
- st.download_button(
138
- label=f"📥 Download All ({len(processed_images)} images)",
139
- data=zip_buffer.getvalue(),
140
- file_name=f"background_removed_{timestamp}.zip",
141
- mime="application/zip",
142
- )
143
-
144
- else:
145
- st.info("👆 Upload one or more images to get started")
146
-
147
- st.markdown("---")
 
 
 
 
 
 
 
1
+ import io
2
+ import zipfile
3
+ from datetime import datetime
4
+
5
+ import streamlit as st
6
+ from rembg import new_session
7
+
8
+ from utils import load_and_process_image
9
+
10
+ st.set_page_config(page_title="Usuwanie tła", layout="wide")
11
+
12
+ st.title("🎨 Usuwanie tła i zmiana koloru")
13
+ st.write("Prześlij obrazy, usuń tło i zastąp je wybranym kolorem!")
14
+
15
+ # Opcje
16
+ col_a, col_b, col_c = st.columns(3)
17
+
18
+ with col_a:
19
+ bg_color = st.color_picker("Wybierz kolor tła", "#FFFFFF")
20
+
21
+ with col_b:
22
+ model_choice = st.selectbox(
23
+ "Model AI:",
24
+ [
25
+ "birefnet-general-lite",
26
+ "birefnet-general",
27
+ "birefnet-dis", # Best for objects with holes
28
+ "isnet-general-use",
29
+ "u2net",
30
+ "birefnet-massive",
31
+ "bria-rmbg",
32
+ ],
33
+ index=0,
34
+ )
35
+
36
+ with col_c:
37
+ transparency_mode = st.radio(
38
+ "Przezroczystość:",
39
+ ["Pozostaw przezroczyste", "Wypełnij kolorem"],
40
+ index=1,
41
+ )
42
+
43
+ with st.sidebar:
44
+ st.subheader("Opcje znaku wodnego")
45
+ add_watermark = st.checkbox("Dodaj znak wodny", value=True)
46
+ watermark_file = st.file_uploader("Prześlij znak wodny (PNG)", type=["png"], key="wm")
47
+ wm_opacity = st.slider("Przezroczystość znaku wodnego", 0.0, 1.0, 0.3)
48
+ wm_scale = st.slider("Rozmiar znaku wodnego (część szerokości)", 0.1, 1.0, 0.33)
49
+ wm_position = st.selectbox(
50
+ "Pozycja",
51
+ ["prawy-dolny róg", "lewy-dolny róg", "prawy-górny róg", "lewy-górny róg"],
52
+ )
53
+
54
+ # Konwersja HEX na RGB
55
+ hex_color = bg_color.lstrip("#")
56
+ rgb_color = tuple(int(hex_color[i : i + 2], 16) for i in (0, 2, 4))
57
+
58
+ # Wgrywanie wielu plików
59
+ uploaded_files = st.file_uploader(
60
+ "Wybierz obrazy",
61
+ type=["png", "jpg", "jpeg"],
62
+ accept_multiple_files=True,
63
+ )
64
+
65
+ if uploaded_files:
66
+ st.write(f"📁 Przesłano {len(uploaded_files)} obraz(ów)")
67
+
68
+ # Przycisk przetwarzania
69
+ if st.button("🚀 Przetwórz wszystkie obrazy", type="primary"):
70
+ # Kontenery postępu
71
+ progress_bar = st.progress(0)
72
+ status_text = st.empty()
73
+
74
+ # Utworzenie sesji z wybranym modelem (wspólna dla wszystkich obrazów)
75
+ status_text.text(f"Ładowanie modelu: {model_choice}...")
76
+ session = new_session(model_choice)
77
+
78
+ processed_images = []
79
+
80
+ # Przetwarzanie każdego obrazu
81
+ for idx, uploaded_file in enumerate(uploaded_files):
82
+ # Aktualizacja postępu
83
+ progress = (idx + 1) / len(uploaded_files)
84
+ progress_bar.progress(progress)
85
+ status_text.text(
86
+ f"Przetwarzanie {idx + 1}/{len(uploaded_files)}: {uploaded_file.name}"
87
+ )
88
+
89
+ input_img, processed_img = load_and_process_image(
90
+ uploaded_file,
91
+ session=session,
92
+ transparency_mode=transparency_mode,
93
+ rgb_color=rgb_color,
94
+ add_watermark=add_watermark,
95
+ watermark_file=watermark_file if watermark_file else None,
96
+ wm_opacity=wm_opacity,
97
+ wm_scale=wm_scale,
98
+ wm_position=wm_position,
99
+ )
100
+
101
+ # Zapis przetworzonego obrazu
102
+ processed_images.append(
103
+ {"name": uploaded_file.name, "original": input_img, "processed": processed_img}
104
+ )
105
+
106
+ status_text.text("✅ Wszystkie obrazy zostały przetworzone!")
107
+
108
+ # Wyświetlanie wyników w siatce
109
+ st.markdown("---")
110
+ st.subheader("Wyniki")
111
+
112
+ # Pokazuj obrazy parami
113
+ for img_data in processed_images:
114
+ col1, col2 = st.columns(2)
115
+
116
+ with col1:
117
+ st.text(f"Oryginał: {img_data['name']}")
118
+ st.image(img_data["original"], width="stretch")
119
+
120
+ with col2:
121
+ st.text(f"Po przetworzeniu: {img_data['name']}")
122
+ st.image(img_data["processed"], width="stretch")
123
+
124
+ st.markdown("---")
125
+
126
+ # Utworzenie pliku ZIP ze wszystkimi przetworzonymi obrazami
127
+ zip_buffer = io.BytesIO()
128
+ with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:
129
+ for img_data in processed_images:
130
+ img_buffer = io.BytesIO()
131
+
132
+ # Zapis jako PNG, aby zachować przezroczystość
133
+ img_data["processed"].save(img_buffer, format="PNG")
134
+ img_bytes = img_buffer.getvalue()
135
+
136
+ filename = f"przetworzony_{img_data['name'].rsplit('.', 1)[0]}.png"
137
+ zip_file.writestr(filename, img_bytes)
138
+
139
+ zip_buffer.seek(0)
140
+
141
+ # Przycisk pobierania ZIP
142
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
143
+ st.download_button(
144
+ label=f"📥 Pobierz wszystkie ({len(processed_images)} obrazów)",
145
+ data=zip_buffer.getvalue(),
146
+ file_name=f"usuniete_tlo_{timestamp}.zip",
147
+ mime="application/zip",
148
+ )
149
+
150
+ else:
151
+ st.info("👆 Prześlij jeden lub więcej obrazów, aby rozpocząć")
152
+
153
+ st.markdown("---")