'Python can't find a directory that exists [closed]
I have a folder structure that looks like this
.
├── 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
and calibrate.py looks like this
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
)
For some reason that I can't understand the line images = glob(f"{dir_path}/*.{image_format}") finds no files despite the path being correct, I even tried executing in both the root and the scripts/ folder. The script is supposed to glob all the files in the directory and use them to find the camera distortion matrix
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
