Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法使用“rfind”函数从文件路径提取动物名称_Python_String_Dictionary - Fatal编程技术网

Python 无法使用“rfind”函数从文件路径提取动物名称

Python 无法使用“rfind”函数从文件路径提取动物名称,python,string,dictionary,Python,String,Dictionary,无法使用rfind函数从文件路径提取动物名称 动物名称将用作字典索引={}中的键 我的png文件路径是:c:\users\intel\desktop\folder\elephant.png 运行此代码之后 import ZernikeMoments import numpy as np import argparse import cPickle import glob import cv2 ap = argparse.ArgumentParser() ap.add_argument("-a

无法使用rfind函数从文件路径提取动物名称

动物名称将用作字典索引={}中的键

我的png文件路径是:c:\users\intel\desktop\folder\elephant.png

运行此代码之后

import ZernikeMoments
import numpy as np
import argparse
import cPickle
import glob
import cv2


ap = argparse.ArgumentParser()
ap.add_argument("-a", "--animals", required = True,
    help = "Path where the animals will be stored")
ap.add_argument("-i", "--index", required = True,
    help = "Path to where the index file will be sotred")
args = vars(ap.parse_args())


desc = ZernikeMoments(21)
index = {}

for animalPath in glob.glob(args["animals"] + "/*.png"):
    # parse out the name, load image and convert to grayscale
    animal = animalPath[animalPath.rfind("/") + 1: ].replace(".png", "")
    image = cv2.imread(animalPath, 0)

    image = cv2.copyMakeBorder(image, 15, 15, 15, 15,
        cv2.BORDER_CONSTANT, value = 255)
    thresh = cv2.bitwise_not(image)
    thresh[thresh > 0] = 255

    outline = np.zeros(image.shape, dtype = "uint8")
    (cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, 
        cv2.CHAIN_APPROX_SIMPLE)
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
    cv2.drawContours(outline, [cnts], -1, 255, -1)

    moments = desc.describe(outline)
    index[animal] = moments
我得到c:\users\intel\desktop\folder\elephant作为字典键。 其目的是将字典键作为大象来获取

您可以使用ntpath来实现这一点

或者简单地使用ntpath和操作系统

使用os.path.basename函数


别忘了导入操作系统模块

awesomeontv!谢谢你的宝贵意见。又短又简单..而且works@patrick.c除非您特别希望在linux或mac上使用windows路径,否则您确实不应该使用ntpath模块。path保证是适用于运行os python的路径模块。我明白了,我不知道这一点。Alrite@Alik我会注意到这一点,感谢您发表如此有益的评论!
import ntpath
a_path = "c:\users\intel\desktop\folder\elephant.png"
>>>ntpath.basename(a_path)
'elephant.png'
>>>ntpath.basename(a_path).split(".")[0]
'elephant'
>>>os.path.splitext(ntpath.basename(a_path))[0]
'elephant'
animal =  os.path.splitext(os.path.basename(animalPath))[0]