10
10
11
11
lastx ,lasty = None ,None
12
12
13
- def display_mask_ui (image ,mask ,max_size ):
13
+ def display_mask_ui (image ,mask ,max_size , initPolys ):
14
14
15
- polys = [[]]
15
+ polys = initPolys
16
16
17
17
def on_mouse (event , x , y , buttons , param ):
18
18
global lastx ,lasty
@@ -87,7 +87,7 @@ def on_mouse(event, x, y, buttons, param):
87
87
segs = [(int (a / factor ),int (b / factor )) for a ,b in polyline ]
88
88
cv2 .fillPoly (newmask , np .array ([segs ]), (255 ,255 ,255 ), 0 )
89
89
cv2 .destroyWindow ('MaskingWindow' )
90
- return Image .fromarray ( cv2 .cvtColor ( newmask , cv2 .COLOR_BGR2GRAY ) )
90
+ return Image .fromarray ( cv2 .cvtColor ( newmask , cv2 .COLOR_BGR2GRAY ) ), polys
91
91
break
92
92
if key == ord ('c' ):
93
93
polys = [[]]
@@ -97,7 +97,7 @@ def on_mouse(event, x, y, buttons, param):
97
97
break
98
98
99
99
cv2 .destroyWindow ('MaskingWindow' )
100
- return mask
100
+ return mask , polys
101
101
102
102
import modules .scripts as scripts
103
103
import gradio as gr
@@ -130,8 +130,11 @@ def run(self, p, max_size, ask_on_each_run):
130
130
if not hasattr (self ,'lastMask' ):
131
131
self .lastMask = None
132
132
133
+ if not hasattr (self ,'lastPolys' ):
134
+ self .lastPolys = [[]]
135
+
133
136
if ask_on_each_run or self .lastImg is None or self .lastImg != p .init_images [0 ]:
134
- p .image_mask = display_mask_ui (p .init_images [0 ],p .image_mask ,max_size )
137
+ p .image_mask , self . lastPolys = display_mask_ui (p .init_images [0 ],p .image_mask ,max_size , self . lastPolys )
135
138
self .lastImg = p .init_images [0 ]
136
139
if p .image_mask is not None :
137
140
self .lastMask = p .image_mask .copy ()
0 commit comments