Johannes commited on
Commit
f41e267
1 Parent(s): bf28b90
Files changed (3) hide show
  1. README.md +2 -2
  2. app.py +44 -0
  3. requirements.txt +2 -0
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: Salient Object Detection
3
- emoji: 📚
4
  colorFrom: pink
5
- colorTo: gray
6
  sdk: gradio
7
  sdk_version: 4.25.0
8
  app_file: app.py
 
1
  ---
2
  title: Salient Object Detection
3
+ emoji: 👥
4
  colorFrom: pink
5
+ colorTo: purple
6
  sdk: gradio
7
  sdk_version: 4.25.0
8
  app_file: app.py
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image, ImageDraw
3
+ from transparent_background import Remover
4
+ import numpy as np
5
+
6
+
7
+ def infer(img: Image):
8
+ remover = Remover(mode="fast")
9
+ masked_image = remover.process(img, type="map")
10
+
11
+ gray_image = masked_image.convert("L")
12
+ binary_image = gray_image.point(lambda x: 0 if x < 128 else 255, "1")
13
+
14
+ image_array = np.array(binary_image)
15
+ white_pixels = np.where(image_array == True)
16
+
17
+ x_coords = white_pixels[1]
18
+ y_coords = white_pixels[0]
19
+
20
+ min_x = np.min(x_coords)
21
+ max_x = np.max(x_coords)
22
+ min_y = np.min(y_coords)
23
+ max_y = np.max(y_coords)
24
+
25
+ draw = ImageDraw.Draw(img)
26
+ draw.rectangle([(min_x, min_y), (max_x, max_y)], outline="red", width=2)
27
+
28
+ return img, masked_image
29
+
30
+
31
+ gr.Interface(
32
+ fn=infer,
33
+ description="""
34
+ This space performs salient object detection on an image uploaded by the user. I.e. it will detect the object(s) in the image foreground and draw a red rectangle around it.
35
+
36
+ It uses the [transparente-background](https://github.com/plemeri/transparent-background) library, which is build on [InSPyReNet](https://github.com/plemeri/inspyrenet).
37
+ """,
38
+ inputs=gr.components.Image(type="pil", label="Input Image"),
39
+ outputs=[
40
+ gr.components.Image(type="pil", label="Output Image"),
41
+ gr.components.Image(type="pil", label="Image Mask"),
42
+ ],
43
+ title="Salient Object Detection",
44
+ ).launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ transparent-background = "*"
2
+ numpy = "<2"