Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.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/1/angularjs/22.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 1个文件夹的数据被添加到另一个文件夹_Python - Fatal编程技术网

Python 1个文件夹的数据被添加到另一个文件夹

Python 1个文件夹的数据被添加到另一个文件夹,python,Python,我有一个小脚本,可以检查我的对象是否出现在图像中。如果存在,则将图像写入文件夹。其中有多个子文件夹。写入时,第一个文件夹的数据工作正常,但当数据写入第二个子文件夹时,第一个文件夹的数据也会与第二个文件夹的数据一起追加 代码工作得非常好,除了将数据写入磁盘时,第一个子文件夹的数据也附加到第二个子文件夹的数据。下面是我的代码 def target_non_target(input_frames_folder,model_file,output): if not os.path.exists

我有一个小脚本,可以检查我的对象是否出现在图像中。如果存在,则将图像写入文件夹。其中有多个子文件夹。写入时,第一个文件夹的数据工作正常,但当数据写入第二个子文件夹时,第一个文件夹的数据也会与第二个文件夹的数据一起追加

代码工作得非常好,除了将数据写入磁盘时,第一个子文件夹的数据也附加到第二个子文件夹的数据。下面是我的代码

def target_non_target(input_frames_folder,model_file,output):

    if not os.path.exists(output):
        os.makedirs(output,exist_ok=True)

    count=0
    folders = glob(input_frames_folder)

    img_list = []


    for folder in folders:
        folder_name=os.path.basename(folder)
        #print(folder_name)
        out_path = output +"\\" + folder_name
        print(out_path)
        os.makedirs(out_path,exist_ok=True)



        for f in glob(folder+"/*.jpg"):
            img_list.append(f)

        for i in range(len(img_list)):
            v1=os.path.basename(img_list[i])
            img_name = os.path.splitext(v1)[0]
            image = cv2.imread(img_list[i])
            orig = image.copy()

            image = cv2.resize(image, (28, 28))
            image = image.astype("float") / 255.0
            image = img_to_array(image)
            image = np.expand_dims(image, axis=0)


            print("[INFO] loading network...")
            model = load_model(model_file)


            (non_target, target) = model.predict(image)[0]

            if target > non_target:
                label = "Target"

            else:
                label = "Non Target"

            probab = target if target > non_target else non_target
            label = "{}: {:.2f}%".format(label, probab * 100)


            op = imutils.resize(orig, width=400)
            cv2.putText(op, label, (10, 25),  cv2.FONT_HERSHEY_SIMPLEX,0.7, (0, 255, 0), 2)

            if target > non_target:
                cv2.imwrite(out_path+"/"+"\\{}.jpg".format(img_name),orig)
            cv2.waitKey(0)

    #return target_op


frames_folder = ("C:\\Python36\\videos\\videos_new\\*")

model = ("C:\\Python35\\target_non_target\\target_non_target.model")

output_folder = ("C:\\Python35\\target_non_target\\Target_images_new\\")

target_check = target_non_target(frames_folder,model,output_folder)

假设主文件夹X中有2个子文件夹A和B。将有更多子文件夹。在将输出写入磁盘时,A的数据被完美写入,但在为B写入数据时,文件夹A和文件夹B的数据都被追加到B文件夹中。我希望数据位于各自的文件夹中。您知道我的脚本中可以做哪些更改以获得所需的输出吗?您正在使用
img_list=[]
启动,但您需要在每个文件夹循环结束时重复此操作以将其重置为空。现在,您保留所有结果,然后当您移动到下一个文件夹时,您将保留以前的结果并继续添加

文件夹中文件夹的
循环的最末端需要有
img\u列表=[]

更新完整代码:

def target_non_target(input_frames_folder,model_file,output):

    if not os.path.exists(output):
        os.makedirs(output,exist_ok=True)

    count=0
    folders = glob(input_frames_folder)

    img_list = []

    for folder in folders:
        folder_name=os.path.basename(folder)
        #print(folder_name)
        out_path = output +"\\" + folder_name
        print(out_path)
        os.makedirs(out_path,exist_ok=True)

        for f in glob(folder+"/*.jpg"):
            img_list.append(f)

        for i in range(len(img_list)):
            v1=os.path.basename(img_list[i])
            img_name = os.path.splitext(v1)[0]
            image = cv2.imread(img_list[i])
            orig = image.copy()

            image = cv2.resize(image, (28, 28))
            image = image.astype("float") / 255.0
            image = img_to_array(image)
            image = np.expand_dims(image, axis=0)

            print("[INFO] loading network...")
            model = load_model(model_file)

            (non_target, target) = model.predict(image)[0]

            if target > non_target:
                label = "Target"
            else:
                label = "Non Target"

            probab = target if target > non_target else non_target
            label = "{}: {:.2f}%".format(label, probab * 100)

            op = imutils.resize(orig, width=400)
            cv2.putText(op, label, (10, 25),  cv2.FONT_HERSHEY_SIMPLEX,0.7, (0, 255, 0), 2)

            if target > non_target:
                cv2.imwrite(out_path+"/"+"\\{}.jpg".format(img_name),orig)
            cv2.waitKey(0)

        img_list = []   # this is the end of for folder in folders, reset list
    #return target_op


frames_folder = ("C:\\Python36\\videos\\videos_new\\*")
model = ("C:\\Python35\\target_non_target\\target_non_target.model")
output_folder = ("C:\\Python35\\target_non_target\\Target_images_new\\")
target_check = target_non_target(frames_folder,model,output_folder)

我有点明白你想暗示什么。但是我对python还是很陌生的。你能帮我在脚本中所需的位置进行建议的更改吗。我还有一些类似的功能。如果你能在这里给我看一下变化,我可能可以解决其他函数中的问题。基本上,我只是不知道是在os.makedirs(out_path,exist_ok=True)之后还是在这个cv2.putText(op,label,(10,25),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,255,0),2)之后添加它需要的地方。只需根据缩进注意每个循环的开始和停止位置,以确定每个循环的结束位置
for folders in folders
是基本级别缩进,因此我们需要在该循环中插入一个缩进,并且我们需要在其他内部循环结束后将其更新为该缩进。非常感谢兄弟,这起到了作用。在你暗示我的缩进是错误的之后,我也做了同样的事情。看到你编辑的代码就更正了它,它工作了。再次非常感谢:)没问题,安基特:)