为什么我无法阅读这些文件?多个库无法找到它们。

huangapple go评论105阅读模式
英文:

Why can't I read these files? multiple libraries couldn't find them

问题

Unsuccessfully trying to solve a seemingly simple problem: get dimensions of the image located in a folder using cv2.imread.
The folders structure as follow:

主文件夹
    -- ...
    -- 输入
         -- 数据1
               -- 001
                    100.png
                    101.png
                    102.png   
      -- VP代码
            -- test.py

从“主文件夹”内运行:

python VP代码/test.py --input_video_url ./输入

从“test.py”中的以下代码:

## 检测原始图像尺寸

input_clip_url = os.path.join(opts.input_video_url, test_clip_par_folder, clip_name)


image_folder_path=input_clip_url        
image_subfolder_path=os.listdir(image_folder_path)
image_path=cv2.imread(image_subfolder_path[0])




print ("文件夹路径_"+str(image_folder_path))
print ("图像子文件夹路径_"+str(image_subfolder_path)) 
print ("图像路径_"+str(image_subfolder_path[0])) 
print ("图像读取_"+str(image_path))

    ## 结束检测原始图像尺寸 

结果显示一切都正常,但“imread”:

文件夹路径_./输入/数据_1/001
图像子文件夹路径_['100.png', '101.png', '102.png']
图像路径_100.png
图像读取_None

我尝试了image.open(PIL)- 没有运气。
因此,看起来存在“路径”问题,但我无法解决它。
对于任何建议,我非常感激。来自test.py

英文:

Unsuccessfully trying to solve a seemingly simple problem: get dimensions of the image located in a folder using cv2.imread.
The folders structure as follow:

Main Folder
    -- ...
    -- input
         -- Data1
               -- 001
                    100.png
                    101.png
                    102.png   
      -- VP_code
            -- test.py

Run from within "Main Foder" :

python VP_code/test.py --input_video_url ./input

from "test.py" following codes:

## Detect original image dimention

input_clip_url = os.path.join(opts.input_video_url, test_clip_par_folder, clip_name)


image_folder_path=input_clip_url        
image_subfolder_path=os.listdir(image_folder_path)
image_path=cv2.imread(image_subfolder_path[0])





print ("Folder path_"+str(image_folder_path))
print ("Image subfolder path_"+str(image_subfolder_path)) 
print ("Image path_"+str(image_subfolder_path[0])) 
print ("Image read_"+str(image_path))

    ## End of detect original image dimention 

As result it properly detects everything but "imread" :

Folder path_./input/data_1/001
Image subfolder path_['100.png','101.png','102.png']
Image path_100.png
Image read_None

I tried image.open (PIL) - no luck.
So,it looks like there is a problem with "path" but I couldn't manage to resolve it.
My great appreciation for any suggestions.from test,py

答案1

得分: 1

如slothrop所评论的,cv2.imread需要完整路径[或正如Christoph所评论的,只需要“正确”的部分],而不仅仅是文件名:如果您从图像的本地目录调用脚本,或者如果您更改了当前工作目录,那么它应该使用"100.png"等文件正常工作。

是的,函数中的参数是"filename",但它意味着一个完整的路径[或"正确"的路径],其中"完整"包括来自您分割的文件夹组件:
https://docs.opencv.org/3.4/d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56

(您是否尝试过使用带有样本图像的完整路径的代码来打开图像,例如 print(cv2.imread("c:\\mainfolder\\input\\Data1\\001\\100.png").shape) 等等。

对于第一个文件的尺寸:

fullpath = os.path.join(image_folder_path, image_path)
image_path = cv2.imread(fullpath)
dim = cv2.imread(fullpath).shape

或者:

dim = cv2.imread(os.path.join(image_folder_path, image_path)).shape

您还可以使用 glob.glob 并将其与image_folder_path和通配符"*.png"一起调用,它将收集所选文件的完整路径列表。

import glob
import os   
files = glob.glob(os.path.join(image_folder_path,'*.png')) #或其他扩展名或通配符
dimensions = cv2.imread(files[0]).shape

示例glob.glob结果:

>>> files
['C:\\some\\pics\\3_n.jpg', 'C:\\some\\pics\\4_n.jpg', '... ]

https://docs.python.org/3/library/glob.html

英文:

As slothrop comments, cv2.imread needs the full path [or just "correct" part as Christoph commented], not only the file name: if you call the script from the local directory of the image or if you change the current working directory, then it should work with "100.png" etc.

Yes, the parameter in the function is "filename" but it means a full path [or "correct" path], with "full" I mean including the folder component from your segmentation:
https://docs.opencv.org/3.4/d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56

(Did you try it with code that opens the images with the full path for a sample image, e.g. print(cv2.imread("c:\\mainfolder\\input\\Data1\\001\\100.png").shape) etc.

For the dimensions of the first file:

fullpath = os.path.join(image_folder_path, image_path)
image_path=cv2.imread(fullpath)
dim = cv2.imread(fullpath).shape

or:

dim = cv2.imread(os.path.join(image_folder_path, image_path)).shape

Also you can use glob.glob and call it with the image_folder_path and a wildcard, "*.png", it will collect a list of full paths for the selected files.

import glob
import os   
files = glob.glob(os.path.join(image_folder_path,'*.png')) #or other extension or wildcard
dimensions = cv2.imread(files[0]).shape

Sample glob.glob result:
>>> files
['C:\\some\\pics\_n.jpg', 'C:\\some\\pics\_n.jpg', '... ]

https://docs.python.org/3/library/glob.html

答案2

得分: 0

已解决,通过添加以下内容:

full_path = os.path.join(image_folder_path, image_subfolder_path[0])

感谢大家提供的有用评论。

英文:

Solved by add the following:

full_path=os.path.join(image_folder_path, image_subfolder_path[0])

Thanks everybody for useful comments.

huangapple
  • 本文由 发表于 2023年6月30日 03:35:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定