인공지능을 좋아하는 곧미남

opencv에는 무조건 np array type을 input data로 넣자! 본문

code_study/opencv

opencv에는 무조건 np array type을 input data로 넣자!

곧미남 2022. 5. 16. 10:23

아래 코드에서 cv2.imwrite()에 image data로 input을 넣었는데, 이때 위에서 torch.tensor type으로 선언되어 있습니다.

이때, numpy array로 변환하지 않으면 아래와 같은 TypeError가 발생합니다.

TypeError: Expected Ptr for argument '%s'

with torch.no_grad():
    for idx, (input, target) in enumerate(zip(input_test, target_test)):
        input, target = input.float().to(device), target.to(device)
        output = endtoendmodel(input)

        # Decoder
        _, prediction = torch.max(output.data, 1)

        for i in range(len(target)):
            log = [(input.shape[0] * idx) + i, prediction[i], target[i]]

            with open(os.path.join(log_dir, 'test_log.csv'), 'a') as f:
                log_1 = list(map(str, log))
                f.write(','.join(log_1) + '\n')

            if not os.path.exists('./test_result_image'):
                os.makedirs('./test_result_image')

            cv2.imwrite('./test_result_image/'+'%4d'%(input.shape[0] * idx+i)+'.png' , np.array(input[i].permute(1, 2, 0).cpu(), dtype=np.float32))

 

그래서 아래와 같이 np array로 변환한 뒤 입력 변수에 넣어줍니다.

np.array(input[i].permute(1, 2, 0).cpu(), dtype=np.float32)

Comments