9
9
import numpy as np
10
10
11
11
lastx ,lasty = None ,None
12
+ zoomOrigin = 0 ,0
13
+ zoomFactor = 1
14
+
15
+ midDragStart = None
12
16
13
17
def display_mask_ui (image ,mask ,max_size ,initPolys ):
14
18
15
19
polys = initPolys
16
20
17
21
def on_mouse (event , x , y , buttons , param ):
18
- global lastx ,lasty
22
+ global lastx ,lasty , zoomFactor , midDragStart , zoomOrigin
19
23
20
- lastx ,lasty = x , y
24
+ lastx ,lasty = x / zoomFactor , y / zoomFactor
21
25
22
26
if event == cv2 .EVENT_LBUTTONDOWN :
23
- polys [- 1 ].append ((x , y ))
27
+ polys [- 1 ].append ((x / zoomFactor , y / zoomFactor ))
24
28
elif event == cv2 .EVENT_RBUTTONDOWN :
25
29
polys .append ([])
30
+ elif event == cv2 .EVENT_RBUTTONDOWN :
31
+ midDragStart = x ,y
32
+ elif event == cv2 .EVENT_RBUTTONUP :
33
+ zoomOrigin = 0 ,0
34
+ if midDragStart is not None :
35
+ zoomOrigin = x - midDragStart [0 ],yx - midDragStart [1 ],
36
+ midDragStart = None
37
+ elif event == cv2 .EVENT_MOUSEMOVE :
38
+ if midDragStart is not None :
39
+ zoomOrigin = x - midDragStart [0 ],yx - midDragStart [1 ],
40
+ elif event == cv2 .EVENT_MOUSEWHEEL :
41
+ if buttons > 0 :
42
+ zoomFactor += 0.1
43
+ else :
44
+ zoomFactor -= 0.1
45
+ zoomFactor = max (1 ,zoomFactor )
46
+
26
47
27
48
opencvImage = cv2 .cvtColor (np .array (image ), cv2 .COLOR_RGB2BGR )
28
49
@@ -49,7 +70,8 @@ def on_mouse(event, x, y, buttons, param):
49
70
50
71
while 1 :
51
72
52
- foreground = np .zeros_like (srcImage )
73
+ zoomedSrc = cv2 .resize (srcImage ,(None ,None ),fx = zoomFactor ,fy = zoomFactor )
74
+ foreground = np .zeros_like (zoomedSrc )
53
75
54
76
for i ,polyline in enumerate (polys ):
55
77
if len (polyline )> 0 :
@@ -63,13 +85,13 @@ def on_mouse(event, x, y, buttons, param):
63
85
active = True
64
86
65
87
if active :
66
- cv2 .fillPoly (foreground , np .array ([segs ]), ( 190 , 107 , 253 ), 0 )
88
+ cv2 .fillPoly (foreground , ( np .array ([segs ])* zoomFactor ). astype ( int ) , ( 190 , 107 , 253 ), 0 )
67
89
else :
68
- cv2 .fillPoly (foreground , np .array ([segs ]), (255 , 255 , 255 ), 0 )
90
+ cv2 .fillPoly (foreground , ( np .array ([segs ])* zoomFactor ). astype ( int ) , (255 , 255 , 255 ), 0 )
69
91
70
92
71
- foreground [foreground < 1 ] = srcImage [foreground < 1 ]
72
- combinedImage = cv2 .addWeighted (srcImage , 0.5 , foreground , 0.5 , 0 )
93
+ foreground [foreground < 1 ] = zoomedSrc [foreground < 1 ]
94
+ combinedImage = cv2 .addWeighted (zoomedSrc , 0.5 , foreground , 0.5 , 0 )
73
95
74
96
helpText = 'Q=Save, C=Reset, LeftClick=Add new point to polygon, Rightclick=Close polygon'
75
97
combinedImage = cv2 .putText (combinedImage , helpText , (0 ,11 ), font , 0.4 , (0 ,0 ,0 ), 2 , cv2 .LINE_AA )
@@ -99,6 +121,13 @@ def on_mouse(event, x, y, buttons, param):
99
121
cv2 .destroyWindow ('MaskingWindow' )
100
122
return mask ,polys
101
123
124
+ if __name__ == '__main__' :
125
+ img = Image .open ('K:\\ test2.png' )
126
+ oldmask = Image .new ('L' ,img .size ,(0 ,))
127
+ newmask ,newPolys = display_mask_ui (img ,oldmask ,1024 ,[[]])
128
+ newmask .show ()
129
+ exit ()
130
+
102
131
import modules .scripts as scripts
103
132
import gradio as gr
104
133
0 commit comments