How to modify the code to output the depth maps by themselves instead of alongside the orginal?
As anyone who's used it already knows, currently the resulting depth map is displayed next to the original image with captions above each, combined in a single image. To then make the depth map useful, you have to bring each image into photo editing software and manually cut each out and resave it, etc.
How can we change the code to output the depth maps by themselves? I'll try by myself, but if anyone has an answer before I figure it out, please let us know.
I'm no coder, but I can see in run.py that at the end, it writes the depth map result.. It wants to write what is defined as "final_result"... "final_result" seem to by a combination of 'caption_space' and 'combined_results'. For depth map only.. we don't want the captions... and we only want half of 'combined_results'.. 'combined_results' seems to be a mix of "raw_image, split_region, depth_color".. raw_image should be the original, which we don't want in the results.. split_region sounds like the white divider in between.. and here's what we want.. depth_color. We want to write just depth_color.
So if I take "depth_color" and replace "final_result" at the end... hopefully that does it?.. but then there's also this extra code and unnecessary processing being done to combine the photos. What exactly of that can be deleted?
Mostly Solution: So yes, simple fix to output only the depth map. Open run.py in Notepad++ or whatever code editing program you use.. and replace the ending "final_result" with "depth_color" and save it. That's it.
Now I'd like to delete as much code at the end there as possible, to run more efficiently.. because it's still working to make combined images, even though they're now not being written to the output. I'd probably also like to see if I can output them in black and white.. which I'm assuming may be just putting "depth" at the end instead of "depth_color", as "depth_color" is depth + cv2.COLORMAP_INFERNO. So yes, that did it. Black and white depth maps alone.
This also gave a significant performance boost, without even removing the unnecessary code yet. I have other things running, so this isn;t exact science, but seems pretty clear:
4.08 it/s 1m47s original speed with combined original image and colored depth map 5.52 it/s 1m19s writing only the colored depth map 5.89 it/s 1m14s writing only black and white depth maps
Ok, so very easy.. I like easy.. In run.py I removed the code from lines 79 through 97.
delete everything including these and between them: line 79 - depth_color = cv2.applyColorMap(depth, cv2.COLORMAP_INFERNO) line 97 - final_result = cv2.vconcat([caption_space, combined_results])
Keep line 79 - depth_color = cv2.applyColorMap(depth, cv2.COLORMAP_INFERNO) if you still want colored depth maps. I made copies of run.py with the original, color, and black white. You do whatever you want. More performance improvements were had.. again, unscientific as I'm running other things, but clearly running faster in this single test. Final results:
4.08 it/s 1m47s original speed with combined original image and colored depth map 5.52 it/s 1m19s writing only the colored depth map 5.89 it/s 1m14s writing only black and white depth maps 6.12 it/s 1m11s writing only black and white depth maps with unnecessary coding in lines 79 through 97 removed
**REMINDER: for those that missed it, or forgot already, or skipped ahead.. be sure you don't just remove lines 79 to 97.. make sure you also replace "final_result" with "depth" at the very end of the code.
50% performance boost and more immediately usable map image. That's a win.
Hi, thank you for your exploration. We have updated the run.py. It now supports two additional arguments: --pred-only and --grayscale.
You can specify the model to only save the predicted depth map by adding --pred-only in the command line. See here for more details.
I had a similar request and started coding away: https://github.com/velaia/Depth-Anything
My forked version contains:
- free choice of OpenCV colormap (e.g. inferno, magma, rainbow, ...)
- --pred-only (in my case called --only-depth for both run.py and run_video.py
- option to save video with original audio if you have ffmpeg installed (no checks, will throw error)
Feel free to add whatever you like to the main repo 🤗 I didn't expect hat Monocular Depth Estimation can be that much fun 😃
Thanks for all the great work, @LiheYoung and team (and greetings to beautiful Hangzhou where I was lucky to have spent some good time at Zhejiang University)
PS: My code also checks for Apple's MPS and uses it if present. It's useful to have the environment variable PYTORCH_ENABLE_MPS_FALLBACK=1 if you're running it on macOS with MPS support (or you'll get an error / warning).
@velaia, thank you for your kind efforts! It is also very honored that our project can make you love MDE :)
@LiheYoung How do we go about using the --grayscale argument with run_video.py? Even when manually adding those lines into the file as they are in run.py, it doesn't seem to work.
How do we go about using the --grayscale argument with run_video.py?
You'd first need to add support for the script argument, similar to the run.py script, near the top of the run_video.py script (around line 17/18):
parser.add_argument('--grayscale', dest='grayscale', action='store_true', help='do not apply colorful palette')
This only makes it so the script will 'see' that you're asking for grayscale, you also need to add the code to switch to a grayscale image when this flag is given, which you should be able to do by modifying line 86 to something like:
depth_color = cv2.cvtColor(depth, cv2.COLOR_GRAY2BGR) if args.grayscale else cv2.applyColorMap(depth, cv2.COLORMAP_INFERNO)
@heyoeyo Figured it out eventually that just adding the argument flag wouldn't magically make it work lol. I used what was in run.py as its grayscale code to change the run_video.py colorization:
file_path = '/content/Depth-Anything/run_video.py'
with open(file_path, 'r') as file:
content = file.read()
modified_content = content.replace('depth_color = cv2.applyColorMap(depth, cv2.COLORMAP_INFERNO)', 'depth_color = np.repeat(depth[..., np.newaxis], 3, axis=-1)')
with open(file_path, 'w') as file:
file.write(modified_content)
I put that into my Colab and it worked perfectly. I'm not sure if yours works too, but I guess it's best to keep it consistent between run.py and run_video.py. Thanks though! Slowly learning how all this stuff works.