Skip to content

Commit ae82947

Browse files
authored
Persist polygons between runs
1 parent 869dc21 commit ae82947

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

external_masking.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
lastx,lasty=None,None
1212

13-
def display_mask_ui(image,mask,max_size):
13+
def display_mask_ui(image,mask,max_size,initPolys):
1414

15-
polys = [[]]
15+
polys = initPolys
1616

1717
def on_mouse(event, x, y, buttons, param):
1818
global lastx,lasty
@@ -87,7 +87,7 @@ def on_mouse(event, x, y, buttons, param):
8787
segs = [(int(a/factor),int(b/factor)) for a,b in polyline]
8888
cv2.fillPoly(newmask, np.array([segs]), (255,255,255), 0)
8989
cv2.destroyWindow('MaskingWindow')
90-
return Image.fromarray( cv2.cvtColor( newmask, cv2.COLOR_BGR2GRAY) )
90+
return Image.fromarray( cv2.cvtColor( newmask, cv2.COLOR_BGR2GRAY) ),polys
9191
break
9292
if key == ord('c'):
9393
polys = [[]]
@@ -97,7 +97,7 @@ def on_mouse(event, x, y, buttons, param):
9797
break
9898

9999
cv2.destroyWindow('MaskingWindow')
100-
return mask
100+
return mask,polys
101101

102102
import modules.scripts as scripts
103103
import gradio as gr
@@ -130,8 +130,11 @@ def run(self, p, max_size, ask_on_each_run):
130130
if not hasattr(self,'lastMask'):
131131
self.lastMask = None
132132

133+
if not hasattr(self,'lastPolys'):
134+
self.lastPolys = [[]]
135+
133136
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)
135138
self.lastImg = p.init_images[0]
136139
if p.image_mask is not None:
137140
self.lastMask = p.image_mask.copy()

0 commit comments

Comments
 (0)