Rezuwan commited on
Commit
b22cbe3
·
verified ·
1 Parent(s): 8899aca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -30
app.py CHANGED
@@ -1,33 +1,36 @@
1
  import gradio as gr
 
2
  from PIL import Image
3
- import random
4
  import os
5
 
6
- # Dummy classifier function (replace with real model logic)
7
- def classify_parrot(image):
8
- # Dummy label list
9
- labels = ["Hyacinth Macaw", "Eclectus Parrot", "Cockatiel", "Kakapo", "Budgerigar", "Palm Cockatoo", "Sulphur-crested Cockatoo"]
10
- prediction = random.choice(labels)
11
- output_img_path = "output_result.png"
12
- image.save(output_img_path)
13
- return output_img_path, f"Predicted Species: {prediction}"
14
 
15
- # Example images (replace with your actual trial images)
 
 
 
 
 
 
 
 
16
  example_images = [
17
- "examples/hyacinth.jpg",
18
- "examples/eclectus.jpg",
19
- "examples/cockatiel.jpg",
20
- "examples/kakapo.jpg",
21
- "examples/macaw.jpg",
22
- "examples/sulphur.jpg",
23
- "examples/green_parrot.jpg",
24
- "examples/palm_cockatoo.jpg",
25
- "examples/yellow_parrot.jpg",
26
  ]
27
 
28
- with gr.Blocks(title="Parrot60 Classifier") as demo:
29
- gr.Markdown("## 🦜 Parrot60 Classifier")
30
- gr.Markdown("This classifier can classify 60 parrot species with 93% accuracy.")
31
 
32
  with gr.Row():
33
  with gr.Column():
@@ -36,11 +39,11 @@ with gr.Blocks(title="Parrot60 Classifier") as demo:
36
  clear_btn = gr.Button("Clear")
37
  with gr.Column():
38
  output_image = gr.Image(label="Output", type="filepath")
39
- output_text = gr.Textbox(label="Prediction", lines=1)
40
  download_output = gr.File(label="Download Output")
41
 
42
  with gr.Row():
43
- gr.Markdown("### 🧪 Examples")
44
  with gr.Row():
45
  gr.Examples(
46
  examples=example_images,
@@ -48,12 +51,8 @@ with gr.Blocks(title="Parrot60 Classifier") as demo:
48
  label="Click to try an example",
49
  )
50
 
51
- def run_pipeline(img):
52
- result_path, label = classify_parrot(img)
53
- return result_path, label, result_path
54
-
55
- submit_btn.click(fn=run_pipeline, inputs=image_input, outputs=[output_image, output_text, download_output])
56
  clear_btn.click(fn=lambda: (None, "", None), outputs=[image_input, output_text, download_output])
57
 
58
- # Launch app
59
  demo.launch()
 
1
  import gradio as gr
2
+ from rembg import remove
3
  from PIL import Image
4
+ import io
5
  import os
6
 
7
+ # Background removal function
8
+ def remove_background(image):
9
+ input_bytes = io.BytesIO()
10
+ image.save(input_bytes, format='PNG')
11
+ input_bytes = input_bytes.getvalue()
 
 
 
12
 
13
+ output_bytes = remove(input_bytes)
14
+ output_img = Image.open(io.BytesIO(output_bytes))
15
+
16
+ output_path = "output.png"
17
+ output_img.save(output_path, format="PNG")
18
+
19
+ return output_path, "Background removed successfully!", output_path
20
+
21
+ # Example images (replace with actual sample image paths)
22
  example_images = [
23
+ "examples/person1.jpg",
24
+ "examples/product1.jpg",
25
+ "examples/cat.jpg",
26
+ "examples/dog.jpg",
27
+ "examples/portrait.jpg",
28
+ "examples/bird.jpg"
 
 
 
29
  ]
30
 
31
+ with gr.Blocks(title="Background Remover") as demo:
32
+ gr.Markdown("# 🖼️ Background Remover")
33
+ gr.Markdown("Upload an image or use an example to remove its background.")
34
 
35
  with gr.Row():
36
  with gr.Column():
 
39
  clear_btn = gr.Button("Clear")
40
  with gr.Column():
41
  output_image = gr.Image(label="Output", type="filepath")
42
+ output_text = gr.Textbox(label="Status", lines=1)
43
  download_output = gr.File(label="Download Output")
44
 
45
  with gr.Row():
46
+ gr.Markdown("### 🧪 Example Images")
47
  with gr.Row():
48
  gr.Examples(
49
  examples=example_images,
 
51
  label="Click to try an example",
52
  )
53
 
54
+ # Hook logic
55
+ submit_btn.click(fn=remove_background, inputs=image_input, outputs=[output_image, output_text, download_output])
 
 
 
56
  clear_btn.click(fn=lambda: (None, "", None), outputs=[image_input, output_text, download_output])
57
 
 
58
  demo.launch()