gamecontroll commited on
Commit
e6f638c
·
1 Parent(s): caa93ea

inserir novos reccursos em remover fundos

Browse files
Files changed (1) hide show
  1. app.py → main.py +60 -3
app.py → main.py RENAMED
@@ -572,14 +572,71 @@ elif tipo_edicao == "🖼️ Imagens":
572
 
573
  st.markdown("---")
574
  st.subheader("✨ Remover Fundo")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
575
  if st.button("🎨 Remover Fundo da Imagem", type="primary", key="bg_remover_button"):
576
  with st.spinner("Processando e removendo o fundo..."):
577
- # Resetar o ponteiro do arquivo para ler novamente
578
  image_file.seek(0)
579
  input_bytes = image_file.getvalue()
580
-
581
  try:
582
- output_bytes = rembg.remove(input_bytes)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
583
 
584
  st.success("✅ Fundo removido com sucesso!")
585
 
 
572
 
573
  st.markdown("---")
574
  st.subheader("✨ Remover Fundo")
575
+
576
+ with st.expander("🔧 Opções Avançadas de Remoção", expanded=False):
577
+ modelo_rembg = st.selectbox(
578
+ "Modelo de IA",
579
+ ["u2net", "u2netp", "u2net_human_seg", "isnet-general-use", "silueta"],
580
+ index=3,
581
+ help="Cada modelo tem especialidades diferentes. 'isnet-general-use' é bom para uso geral."
582
+ )
583
+
584
+ col_alpha1, col_alpha2 = st.columns(2)
585
+ with col_alpha1:
586
+ alpha_matting = st.checkbox("Refinamento de bordas (Alpha Matting)", value=False)
587
+ with col_alpha2:
588
+ post_process_mask = st.checkbox("Pós-processar máscara", value=True, help="Limpa ruídos na máscara final.")
589
+
590
+ if alpha_matting:
591
+ col_t1, col_t2, col_t3 = st.columns(3)
592
+ with col_t1:
593
+ fg_threshold = st.slider("Limiar primeiro plano", 0, 255, 240,
594
+ help="Pixels acima deste valor são considerados primeiro plano.")
595
+ with col_t2:
596
+ bg_threshold = st.slider("Limial fundo", 0, 255, 10,
597
+ help="Pixels abaixo deste valor são considerados fundo.")
598
+ with col_t3:
599
+ erode_size = st.slider("Erosão da borda", 0, 30, 10,
600
+ help="Suaviza as bordas da separação.")
601
+
602
+ bgcolor_opcao = st.selectbox("Cor de fundo (substituição)", [
603
+ "Transparente (sem fundo)", "Branco", "Preto", "Personalizado"
604
+ ], index=0)
605
+ bgcolor_val = None
606
+ if bgcolor_opcao == "Branco":
607
+ bgcolor_val = (255, 255, 255, 255)
608
+ elif bgcolor_opcao == "Preto":
609
+ bgcolor_val = (0, 0, 0, 255)
610
+ elif bgcolor_opcao == "Personalizado":
611
+ bg_r = st.number_input("R", 0, 255, 255, key="bg_r")
612
+ bg_g = st.number_input("G", 0, 255, 255, key="bg_g")
613
+ bg_b = st.number_input("B", 0, 255, 255, key="bg_b")
614
+ bgcolor_val = (bg_r, bg_g, bg_b, 255)
615
+
616
  if st.button("🎨 Remover Fundo da Imagem", type="primary", key="bg_remover_button"):
617
  with st.spinner("Processando e removendo o fundo..."):
 
618
  image_file.seek(0)
619
  input_bytes = image_file.getvalue()
620
+
621
  try:
622
+ session = rembg.new_session(modelo_rembg)
623
+
624
+ remove_kwargs = {
625
+ "data": input_bytes,
626
+ "session": session,
627
+ "post_process_mask": post_process_mask,
628
+ }
629
+
630
+ if alpha_matting:
631
+ remove_kwargs["alpha_matting"] = True
632
+ remove_kwargs["alpha_matting_foreground_threshold"] = fg_threshold
633
+ remove_kwargs["alpha_matting_background_threshold"] = bg_threshold
634
+ remove_kwargs["alpha_matting_erode_size"] = erode_size
635
+
636
+ if bgcolor_val is not None:
637
+ remove_kwargs["bgcolor"] = bgcolor_val
638
+
639
+ output_bytes = rembg.remove(**remove_kwargs)
640
 
641
  st.success("✅ Fundo removido com sucesso!")
642