เปิด cv ไม่สามารถอ่านแฟ้มทุกแฟ้มรูปแบบเป็นเส้นทาง

0

คำถาม

ฉันทำงานกับวันที่ของ 72 ภาพและ 72 หน้ากาก ฉัน appended ภาพให้เป็น numpy ndarrayฉันต้องการ cv2 เพื่อทำการอ่านแฟ้มออกจากเส้นทางที่ตรงกับแฟ้มที่ numpy ndarray.

นี่คือเส้นทางไปยังภาพและหน้ากาก:

images_dir = '/content/drive/MyDrive/dataset/images'
masks_dir = '/content/drive/MyDrive/dataset/masks'

#การเพิ่มภาพที่ต้อง numpy ndarray

file_names = np.sort(os.listdir(images_dir)) 
file_names = np.char.split(file_names, '.')
filenames = np.array([])
for i in range(len(file_names)):
    filenames = np.append(filenames, file_names[i][0])

นี่คือฟังก์ชันฉันต้องการเปิด cv ต้องอ่านทุกๆภาพและต่อหน้ากากมาจากที่สัมพันธ์กันแหน่งจัดเก็บเอกสาร:

def augment_dataset(count):
    '''Function for data augmentation
        Input:
            count - total no. of images after augmentation = initial no. of images * count
        Output:
            writes augmented images (input images & segmentation masks) to the working directory
    '''
    transform_1 = augment(512, 512)
    transform_2 = augment(480, 480)
    transform_3 = augment(512, 512)
    transform_4 = augment(800, 800)
    transform_5 = augment(1024, 1024)
    transform_6 = augment(800, 800)
    transform_7 = augment(1600, 1600)
    transform_8 = augment(1920, 1280)
    
    i = 0
    for i in range(count):
        for file in filenames:
            tile = file.split('_')[1]
            img = cv2.imread(images_dir+file+'.jpg')
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            mask = cv2.imread(masks_dir+file+'.png')
            mask = cv2.cvtColor(mask, cv2.COLOR_BGR2RGB)

ตอนที่ฉันวิ่งรหัส:

augment_dataset(8)

มีข้อผิดพลาดนี้มา:

---------------------------------------------------------------------------

error                                     Traceback (most recent call last)

<ipython-input-112-fae4beb79e15> in <module>()
----> 1 augment_dataset(8)

<ipython-input-111-121d55acd3fc> in augment_dataset(count)
     20             tile = file.split('_')[1]
     21             img = cv2.imread(images_dir+file+'.jpg')
---> 22             img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
     23             mask = cv2.imread(masks_dir+file+'.png')
     24             mask = cv2.cvtColor(mask, cv2.COLOR_BGR2RGB)

error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

ฉันรู้ว่านี่เป็นเพราะ OpenCV ยังไม่ได้อ่านแฟ้ม ดังนั้นผมต้องทำยังไงให้ openCV เพื่ออ่านเอกสาร

computer-vision imread opencv path
2021-11-20 09:26:23
1

คำตอบที่ดีที่สุด

0

อยู่ที่คดีเหล่านี้,มันจะดีกว่า print(path/to/directory) เพื่อจะได้เห็นถ้าไดเรกทอรีที่ถูกต้องหรือไม่ ในกรณีนี้เราสามารถเห็นว่าฉันพลาด / อยู่ในตำแหน่งของไดเรกทอรี ดังนั้นเป็นภาษาไพธอนไม่สามารถวิเคราะห์ข้อมูลออกมา แล้วคุณสามารถใช้ ose.path.exists(path/to/directory) เพื่อดูว่าเส้นทางที่อยู่ก่อนหรือไม่ ถ้าคืนค่ากลับมา Falseคุณต้องตรวจสอบที่กำหนดเส้นทางสำหรับข้อผิดพลาด

2021-11-23 19:47:17

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่