ihabooe commited on
Commit
b82a86a
·
verified ·
1 Parent(s): 38b6344

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -1
app.py CHANGED
@@ -32,4 +32,42 @@ def process(image, progress=gr.Progress()):
32
  im_tensor = normalize(im_tensor, [0.5, 0.5, 0.5], [1.0, 1.0, 1.0])
33
 
34
  if torch.cuda.is_available():
35
- im_tensor
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  im_tensor = normalize(im_tensor, [0.5, 0.5, 0.5], [1.0, 1.0, 1.0])
33
 
34
  if torch.cuda.is_available():
35
+ im_tensor = im_tensor.cuda()
36
+
37
+ # Inference
38
+ result = net(im_tensor)
39
+ progress(0.7) # 70% progress during inference
40
+
41
+ # Post-process
42
+ result = torch.squeeze(F.interpolate(result[0][0], size=(h, w), mode='bilinear'), 0)
43
+ result = (result - result.min()) / (result.max() - result.min())
44
+
45
+ # Convert to PIL image with alpha mask
46
+ result_array = (result * 255).cpu().data.numpy().astype(np.uint8)
47
+ pil_mask = Image.fromarray(result_array)
48
+ new_im = orig_image.copy()
49
+ new_im.putalpha(pil_mask)
50
+
51
+ # Save the image for download
52
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
53
+ new_im.save(temp_file.name, format='PNG')
54
+
55
+ progress(1.0) # 100% complete
56
+ return new_im, temp_file.name
57
+
58
+ # Gradio interface
59
+ with gr.Blocks() as demo:
60
+ gr.Markdown("## BRIA RMBG 1.4 - Background Remover")
61
+ gr.HTML("<p>Upload your image to remove the background.</p>")
62
+
63
+ with gr.Row():
64
+ input_image = gr.Image(type="numpy", label="Upload Image")
65
+ output_image = gr.Image(type="pil", label="Processed Image")
66
+
67
+ download_button = gr.File(label="Download Processed Image")
68
+
69
+ input_image.change(fn=process, inputs=input_image, outputs=[output_image, download_button])
70
+
71
+ # Run the app on port 7860
72
+ demo.launch(server_name="0.0.0.0", server_port=7860)
73
+