LoginSignup
2
0

More than 3 years have passed since last update.

cv2.putText 周りのエラー

Last updated at Posted at 2020-12-05

出会ったエラー

  • なんとなく cv2.putText していたら、plt.imshow(img) で怒られた
TypeError: Image data of dtype object cannot be converted to float
  • 適当に対処しようとして出たエラー集
AttributeError: 'cv2.UMat' object has no attribute 'dtype'
TypeError: 'cv2.UMat' object is not subscriptable

対処法

img = img.get()

.get() で直接データにアクセスできる

出会ったエラー2

  • pytorchの出力を np.uint8 した後に cv2.puttext していたら怒られた
x_t = model.step(x_0=vid[-1])
T = 10
for t in range(T):
    x_t = model.step(viw=viw[t], mot=mot[t])
    # x_tは np.uint8, 0~255, (256,256,3)
    # ↓エラー
    x_t = cv2.putText(x_t, "{:04} / {}".format(t+1, T), 
                        (5, 15), cv2.FONT_HERSHEY_PLAIN, 
                        1, (0, 0, 0), 1, cv2.LINE_AA)
    frames.append(x_t)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<timed exec> in <module>

TypeError: Expected Ptr<cv::UMat> for argument 'img'

対処法

  • どうやらpytorchの出力はnumpyに変換しても余計なものがついている気がする、ので
x_t = model.step(viw=viw[t], mot=mot[t]).copy()
  • と、 .copy() をつけると動いた

対処法2

cv2.UMat(numpy_img) で強制的に cv2.UMat 形式にする
cv2.UMat 形式からnumpyに変換するには .get() をつける

    x_t = model.step(viw=viw[t], mot=mot[t])
    x_t = cv2.putText(cv2.UMat(x_t), "{:04} / {}".format(t+1, T), 
                        (5, 15), cv2.FONT_HERSHEY_PLAIN, 
                        1, (0, 0, 0), 1, cv2.LINE_AA).get()

以上!

2
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0