MCPcopy Create free account
hub / github.com/OpenGVLab/DragGAN / on_drag

Function on_drag

gradio_app.py:70–99  ·  view source on GitHub ↗
(model, points, max_iters, state, size, mask, lr_box)

Source from the content-addressed store, hash-verified

68
69
70def on_drag(model, points, max_iters, state, size, mask, lr_box):
71 if len(points['handle']) == 0:
72 raise gr.Error('You must select at least one handle point and target point.')
73 if len(points['handle']) != len(points['target']):
74 raise gr.Error('You have uncompleted handle points, try to selct a target point or undo the handle point.')
75 max_iters = int(max_iters)
76 W = state['W']
77
78 handle_points = [torch.tensor(p, device=device).float() for p in points['handle']]
79 target_points = [torch.tensor(p, device=device).float() for p in points['target']]
80
81 if mask.get('mask') is not None:
82 mask = Image.fromarray(mask['mask']).convert('L')
83 mask = np.array(mask) == 255
84
85 mask = torch.from_numpy(mask).float().to(device)
86 mask = mask.unsqueeze(0).unsqueeze(0)
87 else:
88 mask = None
89
90 step = 0
91 for image, W, handle_points in drag_gan(W, model['G'],
92 handle_points, target_points, mask,
93 max_iters=max_iters, lr=lr_box):
94 points['handle'] = [p.cpu().numpy().astype('int') for p in handle_points]
95 image = add_points_to_image(image, points, size=SIZE_TO_CLICK_SIZE[size])
96
97 state['history'].append(image)
98 step += 1
99 yield image, state, step
100
101
102def on_reset(points, image, state):

Callers

nothing calls this directly

Calls 2

drag_ganFunction · 0.90
add_points_to_imageFunction · 0.70

Tested by

no test coverage detected