Skip to content

Commit

Permalink
fix bugs and edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
NevermindNilas committed Jun 21, 2024
1 parent c47673f commit aa015d4
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 6 deletions.
162 changes: 162 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
6 changes: 3 additions & 3 deletions depth_anything_v2/dpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,10 @@ def __init__(

self.device = 'cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu'
self.mean_tensor = (
torch.tensor([0.485, 0.456, 0.406]).view(3, 1, 1).to(self.DEVICE)
torch.tensor([0.485, 0.456, 0.406]).view(3, 1, 1).to(self.device)
)
self.std_tensor = (
torch.tensor([0.229, 0.224, 0.225]).view(3, 1, 1).to(self.DEVICE)
torch.tensor([0.229, 0.224, 0.225]).view(3, 1, 1).to(self.device)
)

def forward(self, x):
Expand Down Expand Up @@ -229,7 +229,7 @@ def image2tensor(self, frame, input_size=518, precision: str = 'fp32', newHeight
"""

h, w = frame.shape[:2]
frame = frame.to(self.device).mul(1.0 / 255.0).permute(2, 0, 1).unsqueeze(0)
frame = torch.from_numpy(frame).to(self.device).mul(1.0 / 255.0).permute(2, 0, 1).unsqueeze(0)
frame = F.interpolate(
frame.float(),
(newHeight, newWidth),
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ gradio_imageslider
gradio==4.29.0
matplotlib
opencv-python
tqdm
torch==2.2.2
torchvision=0.17.2
14 changes: 11 additions & 3 deletions run_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import torch

from tqdm import tqdm
from depth_anything_v2.dpt import DepthAnythingV2


Expand All @@ -20,7 +21,7 @@

parser.add_argument('--pred-only', dest='pred_only', action='store_true', help='only display the prediction')
parser.add_argument('--grayscale', dest='grayscale', action='store_true', help='do not apply colorful palette')
parser.set_defaults("--precision", type=str, default='fp32', choices= ['fp32', 'fp16'])
parser.add_argument("--precision", type=str, default='fp32', choices= ['fp32', 'fp16'])

args = parser.parse_args()

Expand All @@ -37,8 +38,13 @@
depth_anything.load_state_dict(torch.load(f'checkpoints/depth_anything_v2_{args.encoder}.pth', map_location='cpu'))
depth_anything = depth_anything.to(DEVICE).eval()

if args.precision == 'fp16':
if args.precision == 'fp16' and DEVICE == 'cuda':
depth_anything = depth_anything.half()
else:
print('FP16 precision is only available on CUDA devices. Using FP32 instead.')
args.precision = 'fp32'
depth_anything = depth_anything.float()


if os.path.isfile(args.video_path):
if args.video_path.endswith('txt'):
Expand Down Expand Up @@ -77,7 +83,9 @@
output_path = os.path.join(args.outdir, os.path.splitext(os.path.basename(filename))[0] + '.mp4')
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"mp4v"), frame_rate, (output_width, frame_height))

while raw_video.isOpened():
totalFrameCount = int(raw_video.get(cv2.CAP_PROP_FRAME_COUNT))

for _ in tqdm(range(totalFrameCount)):
ret, raw_frame = raw_video.read()
if not ret:
break
Expand Down

0 comments on commit aa015d4

Please sign in to comment.