Python 找不到存在的目录
Python can't find a directory that exists
我的文件夹结构如下所示
.
├── requirements.txt
├── resources
│ ├── img
│ │ └── chessboard
│ │ ├── 2022-02-01-153053.jpg
│ │ ├── 2022-02-01-153058.jpg
│ │ ├── 2022-02-01-153101.jpg
│ │ ├── 2022-02-01-153103.jpg
│ │ ├── 2022-02-01-153106.jpg
│ │ ├── 2022-02-01-153108.jpg
│ │ ├── 2022-02-01-153112.jpg
│ │ ├── 2022-02-01-153116.jpg
│ │ ├── 2022-02-01-153118.jpg
│ │ ├── 2022-02-01-153123.jpg
│ │ ├── 2022-02-01-153126.jpg
│ │ ├── 2022-02-01-153129.jpg
│ │ ├── 2022-02-01-153131.jpg
│ │ ├── 2022-02-01-153134.jpg
│ │ ├── 2022-02-01-153136.jpg
│ │ └── 2022-02-01-153137.jpg
└──scripts
└── calibrate.py
和calibrate.py
看起来像这样
import os
import cv2
import numpy as np
from glob import glob
# Parameters
IMAGES_DIR = os.path.join("..", "resources", "img", "chessboard")
IMAGES_FORMAT = ".jpg"
SQUARE_SIZE = 2.9 # cm
# Count lines not boxes
WIDTH = 6
HEIGHT = 7
def calibrate_chessboard(dir_path, image_format, square_size, width, height):
"""Calibrate a camera using chessboard images."""
images = glob(f"{dir_path}/*.{image_format}")
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objp = np.zeros((height * width, 3), np.float32)
objp[:, :2] = np.mgrid[0:width, 0:height].T.reshape(-1, 2)
objp = objp * square_size
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
# Iterate through all images
for file in images:
img = cv2.imread(str(file))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Find the chess board corners
ret, corners = cv2.findChessboardCorners(gray, (width, height), None)
# If found, add object points, image points (after refining them)
if ret:
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
imgpoints.append(corners2)
# Calibrate camera
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(
objpoints, imgpoints, gray.shape[::-1], None, None
)
return [ret, mtx, dist, rvecs, tvecs]
# Calibrate
ret, mtx, dist, rvecs, tvecs = calibrate_chessboard(
IMAGES_DIR, IMAGES_FORMAT, SQUARE_SIZE, WIDTH, HEIGHT
)
出于某种原因,尽管路径正确,但我无法理解 images = glob(f"{dir_path}/*.{image_format}")
行找不到任何文件,我什至尝试在根目录和 scripts/
文件夹中执行。该脚本应该 glob 目录中的所有文件并使用它们来查找相机失真矩阵
我发现您的路径/文件类型存在一个潜在的小问题。
images = glob(f"{dir_path}/*.{image_format}")
和
IMAGES_FORMAT = ".jpg"
它们都包含 .
,因此您找不到任何图片,因为它们只有一个点。尝试从 IMAGES_FORMAT = ".jpg"
或您的另一行中删除点。
我的文件夹结构如下所示
.
├── requirements.txt
├── resources
│ ├── img
│ │ └── chessboard
│ │ ├── 2022-02-01-153053.jpg
│ │ ├── 2022-02-01-153058.jpg
│ │ ├── 2022-02-01-153101.jpg
│ │ ├── 2022-02-01-153103.jpg
│ │ ├── 2022-02-01-153106.jpg
│ │ ├── 2022-02-01-153108.jpg
│ │ ├── 2022-02-01-153112.jpg
│ │ ├── 2022-02-01-153116.jpg
│ │ ├── 2022-02-01-153118.jpg
│ │ ├── 2022-02-01-153123.jpg
│ │ ├── 2022-02-01-153126.jpg
│ │ ├── 2022-02-01-153129.jpg
│ │ ├── 2022-02-01-153131.jpg
│ │ ├── 2022-02-01-153134.jpg
│ │ ├── 2022-02-01-153136.jpg
│ │ └── 2022-02-01-153137.jpg
└──scripts
└── calibrate.py
和calibrate.py
看起来像这样
import os
import cv2
import numpy as np
from glob import glob
# Parameters
IMAGES_DIR = os.path.join("..", "resources", "img", "chessboard")
IMAGES_FORMAT = ".jpg"
SQUARE_SIZE = 2.9 # cm
# Count lines not boxes
WIDTH = 6
HEIGHT = 7
def calibrate_chessboard(dir_path, image_format, square_size, width, height):
"""Calibrate a camera using chessboard images."""
images = glob(f"{dir_path}/*.{image_format}")
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objp = np.zeros((height * width, 3), np.float32)
objp[:, :2] = np.mgrid[0:width, 0:height].T.reshape(-1, 2)
objp = objp * square_size
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
# Iterate through all images
for file in images:
img = cv2.imread(str(file))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Find the chess board corners
ret, corners = cv2.findChessboardCorners(gray, (width, height), None)
# If found, add object points, image points (after refining them)
if ret:
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
imgpoints.append(corners2)
# Calibrate camera
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(
objpoints, imgpoints, gray.shape[::-1], None, None
)
return [ret, mtx, dist, rvecs, tvecs]
# Calibrate
ret, mtx, dist, rvecs, tvecs = calibrate_chessboard(
IMAGES_DIR, IMAGES_FORMAT, SQUARE_SIZE, WIDTH, HEIGHT
)
出于某种原因,尽管路径正确,但我无法理解 images = glob(f"{dir_path}/*.{image_format}")
行找不到任何文件,我什至尝试在根目录和 scripts/
文件夹中执行。该脚本应该 glob 目录中的所有文件并使用它们来查找相机失真矩阵
我发现您的路径/文件类型存在一个潜在的小问题。
images = glob(f"{dir_path}/*.{image_format}")
和
IMAGES_FORMAT = ".jpg"
它们都包含 .
,因此您找不到任何图片,因为它们只有一个点。尝试从 IMAGES_FORMAT = ".jpg"
或您的另一行中删除点。