【问题标题】:how to save a detected face cropped from (object detection) to its particular created folder?如何将从(对象检测)裁剪的检测到的面部保存到其特定创建的文件夹中?
【发布时间】:2019-09-22 07:11:07
【问题描述】:

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

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

【问题讨论】:

    标签: python python-3.x directory save object-detection


    【解决方案1】:
    FaceFileName = '/path_to_faces_folder/faces/'+ str(y) + ".jpg"
    # save image
    face_write = cv2.imwrite(FaceFileName,f1)
    

    可能存在路径问题。如果是,提供faces 文件夹的完整路径可能会解决问题。

    编辑:

    我正在根据您的评论编辑我的答案。我假设您可以获得每张图像的预测标签。

    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)
    

    【讨论】:

    • 它工作正常,但我想将检测到的裁剪图像保存到特定分配的文件夹名称。例如创建文件夹猫,狗,如果检测到的图像是狗,则将该图像保存到狗文件夹,否则将其保存在猫文件夹中。
    猜你喜欢
    • 2017-04-24
    • 2017-02-21
    • 2018-09-26
    • 2014-05-30
    • 2014-07-28
    • 2019-05-29
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多