函数'cvtColor'中的OpenCV(4.1.2)错误!_src.empty()

时间:2020-06-08 15:20:28

标签: python google-colaboratory cv2 unity3d-unet

每个人。我正在尝试通过Google Colab运行Unet脚本进行培训。但是我的cv2函数有错误。这是代码中发生错误的部分:

def prepare_data(img_shape, ids, reader):
    img_count = len(ids)
    x_data = np.empty((img_count,) + img_shape,
                       dtype='uint8')
    y_data = np.empty((img_count, img_shape[0], img_shape[1], 1),
                       dtype='uint8')

   for i, idx in enumerate(ids):
       path = reader.get_image_path_by_id(idx)
       img = cv2.imread(path, cv2.IMREAD_COLOR)
       img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
       img = cv2.resize(img, dsize=(img_shape[1], img_shape[0]))

       mask = []

错误消息如下:

Traceback (most recent call last):
File "/content/gdrive/My Drive/Unet/unet/train_unet.py", line 247, in <module> train(train_ids=fold4_train, val_ids=fold4_test, fold_num=fold_num, restart=args.restart)
File "/content/gdrive/My Drive/Unet/unet/train_unet.py", line 162, in train train_data, train_masks = prepare_data(img_shape, train_ids, dataset_reader)
File "/content/gdrive/My Drive/Unet/unet/train_unet.py", line 110, in prepare_data
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

我尝试使用from skimage import io img = io.imread(file_path)代替cv2,但是没有用。

1 个答案:

答案 0 :(得分:0)

尝试打开并读取图像后,检查其是否有效。

#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <ctime>
#include <conio.h>
using std::cout;
using std::string;
using std::cin;
int main()

{
    string username;
    string password;
    string secondchoice;
    string passwd = "", user;
    string firstchoice;
    cout << "www.mytools.com\n\nWelcome to MyTools!";
    getchar();
    cout << "\nlogin [1]";
    cout << "\nabout us [2]";
    cout << "\ncontact [3]";
    cout << "\nMake your choice: ";
    cin >> firstchoice;
    system("CLS");

    firstchoice = " ";
    while (firstchoice != "q")
    {
        cin >> firstchoice;
        if (firstchoice == "1")
        {
            system("cls");
            cout << "username: ";
            cin >> username;
            cout << "password: ";
            cin >> password;
            if (username == "myusername" && password == "mypassword") {
                system("cls");
                cout << "You are succesfully logged in as "; cout << username; cout << "!";
            }
            else {
                cout << "username or password incorrect! press enter to try again. ";
                string enter;
                system("cls");
            }
            cout << "username: ";
            cin >> username;
            cout << "password: ";
            cin >> password;
            if (username == "myusername" && password == "mypassword")
            {
                system("cls");
                cout << "You are succesfylly loggen in as "; cout << username; cout << "!";
            }
            else {
                cout << "You have been blocked from the server for to many tries\n";
                return 0;
            }
        }


        if (firstchoice == "2") {
            system("cls");
            cout << "some stuff";
            system("cls");

            cout << "\nlogin [1]";
            cout << "\nabout us [2]";
            cout << "\ncontact [3]";
            cout << "\nMake your choice: ";
        }

        if (firstchoice == "3") {
            system("cls");
            cout << "some stuff";
            system("cls");
            cout << "welcome to MyTools!";
            cout << "\nlogin [1]";
            cout << "\nabout us [2]";
            cout << "\ncontact [3]";
            cout << "\nMake your choice: ";

        }

    }
    return 0;
}