Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.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 如何将从(对象检测)裁剪的检测到的面保存到其特定创建的文件夹中?_Python_Python 3.x_Directory_Save_Object Detection - Fatal编程技术网

Python 如何将从(对象检测)裁剪的检测到的面保存到其特定创建的文件夹中?

Python 如何将从(对象检测)裁剪的检测到的面保存到其特定创建的文件夹中?,python,python-3.x,directory,save,object-detection,Python,Python 3.x,Directory,Save,Object Detection,我正在使用对象检测(裁剪面)创建一个数据收集器程序,但未能将其保存在特定创建的文件夹中 for i in range(len(boxes)): if i in indexes: x, y, w, h = boxes[i] label = str(classes[class_ids[i]]) sub_face = fl[y:y+h, x:x+w] FaceFileName = 'faces/'+ str(y) + ".jpg

我正在使用对象检测(裁剪面)创建一个数据收集器程序,但未能将其保存在特定创建的文件夹中

for i in range(len(boxes)):
    if i in indexes:
        x, y, w, h = boxes[i]
        label = str(classes[class_ids[i]])
        sub_face = fl[y:y+h, x:x+w]

        FaceFileName = 'faces/'+ str(y) + ".jpg"
        cv2.imwrite(FaceFileName, sub_face)#problem

        color = colors[i]
        cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
        cv2.putText(frame,label,(x, y + 30),cv2.FONT_HERSHEY_SIMPLEX, 1, color, 1
可能存在路径问题。如果是,给出
文件夹的完整路径可能会解决问题

编辑:

我正在编辑关于你评论的答案。我假设您可以得到每个图像的预测标签

import os
predicted_class_label = "dog" # somehow you get it somewhere in your code
if os.path.isdir(predicted_class_label):
    FaceFileName = predicted_class_label +"/"+ str(y) + ".jpg"
    face_write = cv2.imwrite(FaceFileName,sub_face)
else:
    os.mkdir(predicted_class_label)
    FaceFileName = predicted_class_label +"/" + str(y) + ".jpg"
    face_write = cv2.imwrite(FaceFileName,sub_face)

它工作正常,但我想将检测到的裁剪图像保存到指定的特定文件夹名。e、 g创建文件夹猫、狗,如果检测到的图像是狗,则将该图像保存到狗文件夹中,否则将其保存在猫文件夹中。
import os
predicted_class_label = "dog" # somehow you get it somewhere in your code
if os.path.isdir(predicted_class_label):
    FaceFileName = predicted_class_label +"/"+ str(y) + ".jpg"
    face_write = cv2.imwrite(FaceFileName,sub_face)
else:
    os.mkdir(predicted_class_label)
    FaceFileName = predicted_class_label +"/" + str(y) + ".jpg"
    face_write = cv2.imwrite(FaceFileName,sub_face)