File size: 1,112 Bytes
62dd301
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4704d12
62dd301
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e71566
62dd301
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import gradio as gr
from meta_segment_anything import SegmentAnything
from color import Color
from color_wheel import ColorWheel

def process_image(image):
	if image is None:
		yield None
		return

	color = Color.fromRgb(0xff, 0x0, 0x0)
	cwt = ColorWheel(color)
	colors = []
	for t in cwt.tone15:
		cw = ColorWheel(Color.fromRgb(t.r, t.g, t.b))
		for h in cw.hue:
			colors.append((h.r, h.g, h.b, 0x80))

	generator = SegmentAnything()
	masks = generator.generate(image)
	masks = sorted(masks, key=lambda x: x['area'], reverse=True)
	index = 0
	for mask in masks:
		if index >= len(colors):
			break
		maskimage = SegmentAnything.makeMaskImage(mask['segmentation'].T, colors[index])
		image.paste(maskimage, (0, 0), maskimage)
		index += 1
		yield image

app = gr.Interface(
	title='Segment Anything Demo',
	description='generate masks for an entire image',
	fn=process_image,
	inputs=gr.Image(type='pil'),
	outputs=gr.Image(label='segmentation', type='pil'),
	allow_flagging='never',
	examples=[['examples/example1.jpg'], ['examples/example2.jpg']],
	concurrency_limit=20,
	#cache_examples=False
)
app.launch()