HimankJ commited on
Commit
9a792e8
Β·
verified Β·
1 Parent(s): 40a58d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -9
app.py CHANGED
@@ -22,7 +22,6 @@ def segment_everything(
22
  conf_threshold=0.25,
23
  better_quality=False,
24
  use_retina=True,
25
- text="",
26
  wider=False,
27
  mask_random_color=True,
28
  ):
@@ -40,13 +39,8 @@ def segment_everything(
40
  conf=conf_threshold,
41
  imgsz=input_size,)
42
 
43
- if len(text) > 0:
44
- results = format_results(results[0], 0)
45
- annotations, _ = text_prompt(results, text, input, device=device, wider=wider)
46
- annotations = np.array([annotations])
47
- else:
48
- annotations = results[0].masks.data
49
 
 
50
  fig = fast_process(annotations=annotations,
51
  image=input,
52
  device=device,
@@ -56,7 +50,31 @@ def segment_everything(
56
  bbox=None,
57
  use_retina=use_retina,
58
  withContours=withContours,)
59
- return fig
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  title = "<center><strong><font size='8'>πŸƒ Fast Segment Anything πŸ€—</font></strong></center>"
62
  description = """ # 🎯 Instructions for points mode """
@@ -79,7 +97,8 @@ demo = gr.Interface(
79
  gr.Checkbox(value=True, label='withContours', info='draw the edges of the masks')
80
  ],
81
  outputs = [
82
- gr.Image(label="Segmented Image", interactive=False, type='pil')
 
83
  ],
84
  title = title,
85
  description = description,
 
22
  conf_threshold=0.25,
23
  better_quality=False,
24
  use_retina=True,
 
25
  wider=False,
26
  mask_random_color=True,
27
  ):
 
39
  conf=conf_threshold,
40
  imgsz=input_size,)
41
 
 
 
 
 
 
 
42
 
43
+ annotations = results[0].masks.data
44
  fig = fast_process(annotations=annotations,
45
  image=input,
46
  device=device,
 
50
  bbox=None,
51
  use_retina=use_retina,
52
  withContours=withContours,)
53
+
54
+ bboxes = results[0].boxes.data
55
+ areas = (bboxes[:, 2] - bboxes[:, 0]) * (bboxes[:, 3] - bboxes[:, 1])
56
+ _, largest_indices = torch.topk(areas, 2)
57
+ largest_boxes = bboxes[largest_indices]
58
+ for i, box in enumerate(largest_boxes):
59
+ print(f"Largest Box {i+1}: {box.tolist()}")
60
+
61
+ fig, ax = plt.subplots(1)
62
+ ax.imshow(image)
63
+
64
+ for box in largest_boxes:
65
+ x1, y1, x2, y2 = box[:4]
66
+ rect = patches.Rectangle((x1, y1), x2-x1, y2-y1, linewidth=2, edgecolor='r', facecolor='none')
67
+ ax.add_patch(rect)
68
+
69
+ ax.axis('off')
70
+ buf = io.BytesIO()
71
+ plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0)
72
+ plt.close(fig)
73
+ buf.seek(0)
74
+
75
+ cropped_image = Image.open(buf)
76
+
77
+ return fig,cropped_image
78
 
79
  title = "<center><strong><font size='8'>πŸƒ Fast Segment Anything πŸ€—</font></strong></center>"
80
  description = """ # 🎯 Instructions for points mode """
 
97
  gr.Checkbox(value=True, label='withContours', info='draw the edges of the masks')
98
  ],
99
  outputs = [
100
+ gr.Image(label="Segmented Image", interactive=False, type='pil'),
101
+ gr.Image(label="Cropped Image", interactive=False, type='pil')
102
  ],
103
  title = title,
104
  description = description,