-
Notifications
You must be signed in to change notification settings - Fork 2
Optional non-blocking display of progress with cv2.imshow #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
global: show_with_system_viewer = False
The end of visualize_progress:
else:
if show_with_system_viewer: img.show()
else:
display_image = numpy.array(img)
display_image = cv2.cvtColor(display_image,cv2.COLOR_RGB2BGR)
cv2.imshow("PROGRESS", display_image)
|
I tried this on the Mac (cut pasting your changes), can't seem to get it working - I'll keep trying. 'cv2' didn't install though, needed a differnt import, there might be another way to get the same convenience, I'll keep looking. opencv could be useful for me to bring in for other reasons, let me think about that. I saw some things suggesting how to do this with matplotlib which I already have imported, but those didn't work either. its unfortunate that PIL's standard image show doesn't return some kind of handle already. I saw people suggest solutions like firing up an image viewer in a separate process , and kill that process, lmao. it would be nice to do this without a web server. |
|
I haven't developed on Mac, maybe you have to build opencv? I have also had
to build it for PC, Linux/Windows. There are many optional modules,
optimizations etc., e.g. it is good to have opengl integration for the GUI
windows, there are different backends, as well as for the video libraries -
ffmpeg, on windows directshow and more modern apis, gstreamer.
Re matplotlib - yes, I recently did it with Matplotlib, non-blocking, when
training the pix2pix model for colorization of the dfk faces, I'll show you the code when on the PC.
Something .ion() had to be called, and then (blocking=False) or something
on plt.show()
Re the spawned process - yes :), os.system("viewer path" + image_path) etc. or a
better spawning func., python has various.
Or just for display - maybe another GUI library - e.g. pygame or directly
sdl2. It uses RGB as far as I remember so a color conversion won't be
needed.
Also tkinter, showing a bitmap.
На ср, 5.10.2022 г., 2:39 ч. dobkeratops ***@***.***> написа:
… I tried this on the Mac (cut pasting your changes), can't seem to get it
working - I'll keep trying. 'cv2' didn't install though, needed a differnt
import, there might be another way to get the same convenience, I'll keep
looking. opencv could be useful for me to bring in for other reasons, let
me think about that.
I saw some things suggesting how to do this with matplotlib which I
already have imported, but those didn't work either. its unfortunate that
PIL's standard image show doesn't return some kind of handle already. I saw
people suggest solutions like firing up an image viewer in a separate
process , and kill that process, lmao. it would be nice to do this
*without* a web server.
—
Reply to this email directly, view it on GitHub
<#1 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AFSI7WFOVVATMB7C6SDHYY3WBS5ZDANCNFSM6AAAAAAQ3DHFVY>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
|
right I also had pygame in the back of my mind, funnily enough (I have actually used that briefly), and SDL2 bindings from python could work . indeed someone else has also suggested tkinter. I see it is possible to make some kind of background UI thread . |
|
If you apply a GUI library, they spawn their threads and callbacks are defined on starting windows in order to deal with the interface. You can update them etc., they are running. For opencv just a delay has to be added after updating in order to allow the OS to redraw the windows, maybe some memory locking/unlocking, e.g. 1 ms, possibly less; if it is constantly updated in a loop with a very high speed it may stay gray. |
|
This is how I used matplotlib with non-blocking display: def generate_images(model, test_input, tar, show=True, name=None): #name - file, extension etc.
prediction = model(test_input, training=True)
plt.figure(figsize=(9, 4))
plt.ion() #? for interactive
display_list = [test_input[0], tar[0], prediction[0]]
title = ['Input Image', 'Ground Truth', 'Predicted Image']
for i in range(3):
plt.subplot(1, 3, i+1)
plt.title(title[i])
# Getting the pixel values in the [0, 1] range to plot.
if show:
plt.imshow(display_list[i] * 0.5 + 0.5)
plt.axis('off')
if name!=None: plt.savefig(name)
plt.pause(0.001)
plt.show(block = False) |
|
The ion-function: "Interactive ON": https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.ion.html Another example: import matplotlib.pyplot as plt
import numpy as np
t = 0.66 #pause, sec.
#the function to turn on interactive mode
plt.ion()
#creating randomly generate collections/data
random_array = np.arange(-4, 5)
collection_1 = random_array ** 2
collection_2 = 10 / (random_array ** 2 + 1)
figure, axes = plt.subplots()
axes.plot(random_array, collection_1,
'rx', random_array,
collection_2, 'b+',
linestyle='solid')
plt.pause(t)
axes.fill_between(random_array,
collection_1,
collection_2,
where=collection_2>collection_1,
interpolate=True,
color='green', alpha=0.3)
plt.pause(t)
lgnd = axes.legend(['collection-1',
'collection-2'],
loc='upper center',
shadow=True)
plt.pause(t)
lgnd.get_frame().set_facecolor('#ffb19a')
plt.pause(t) |
|
maybe overkill but I found there something to launch a web browser from python. that lets me show it the same way from both places (testing development on the Mac, and running it seriously on the desktop PC) I might leave it at that for the timebeing. its better than it was, i.e. it can show an updating image in one window now. I actually quite like the idea of sdl2 in the long run for rendering annotation data.. although that might be better as a seperate tool aswell. keep this just working on an image directory, and everything else is "make an image directory.." regarding tensor board.. I'll keep that in the back of my mind. I do like being able to write debug visualiser code directly myself. I must say it looks like a big library to navigate. thanks for the additional suggestions on matplotlib too.. I might try that again at some point. firing up a web browser to show an image is overkill, but 99% of people have a browser open anyway |
|
as there's so many options on this eventually I think I need to make some kind of visualiser interface so it can be swapped out easily (use tensor board, whatever). for the timebeing I will stick with my adhoc 'render custom debug image' - I like being able to save that alongside the model . eventually. I'll make it show a grid of a few generated examples aswell |
global: show_with_system_viewer = False
The end of visualize_progress:
else:
if show_with_system_viewer: img.show()
else:
display_image = numpy.array(img)
display_image = cv2.cvtColor(display_image,cv2.COLOR_RGB2BGR)
cv2.imshow("PROGRESS", display_image)