2 years ago

#68223

test-img

srv_77

Interactive matplotlib : generate and display a new image each time slider bar is moved

So, I created an ML model that takes 4 variables as inputs, that range from -1 to 1 (e.g.variables= [0.5, 0.4, -0.3, 0.9 ]. Each combination of these variables generates a different image, which I can then plot.

gen_imgs = gen_mdl.predict([variables])
# Rescale images 0 - 1
gen_imgs = 0.5 * gen_imgs + 0.5

image=255-(gen_imgs[0,:,:,0]*255)
plt.figure(figsize=(3,4))
plt.imshow(image1, cmap='gray')
plt.axis('off')

plt.show()

I've tried editing the following code to get the results I'm looking for but keep hitting errors.

%matplotlib notebook
from ipywidgets import *
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
line, = ax.plot(x, np.sin(x))

def update(w = 1.0):
    line.set_ydata(np.sin(w * x))
    fig.canvas.draw()

interact(update)

UPDATE: I figured it out. Only thing I can't seem to figure out is how to limit the latent variable slider between ranges -1 to 1. This is my code:

%matplotlib notebook
from ipywidgets import *
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(4,2))
ax = fig.add_subplot(1, 1, 1)
line = ax.imshow(image1, cmap='gray')
plt.axis('off')

def update(lv_1 = 1.0):
    noise[0][0]=lv_1
    
    gen_imgs = gen_mdl.predict([noise,vfs])
    gen_imgs = 0.5 * gen_imgs + 0.5
    updated_image=255-(gen_imgs[0,:,:,0]*255)
    
    line.set_data(updated_image)
    fig.canvas.draw()

interact(update)       

matplotlib

jupyter-notebook

interactive

0 Answers

Your Answer

Accepted video resources