图像数据无法转换为浮点数错误?

Posted

技术标签:

【中文标题】图像数据无法转换为浮点数错误?【英文标题】:Image data cannot be converted to float error? 【发布时间】:2021-05-23 11:24:46 【问题描述】:

我正在尝试检测人脸并在人脸对齐后返回一个 img 对应的地标。这是我的代码:

def getIthFrame(self, i):
        img = self.data.get_data(i)
        raw_img = Image.fromarray(img)
        w, h = raw_img.size
        if self.pos == "center":
            img, landmarks = self.detectLandmarks(raw_img)
        elif self.pos == "left": 
            img, landmarks = self.detectLandmarks(raw_img.crop(box=(0,0, w//2, h)))
        elif self.pos == "right":
            img, landmarks = self.detectLandmarks(raw_img.crop(box=(w//2,0, w, h)))
        elif self.pos == "90": # face oriented to right
            img, landmarks = self.detectLandmarks(raw_img.rotate(90)) # rotate anticlockwise 90
        elif self.pos == "180": # face oriented to down
            img, landmarks = self.detectLandmarks(raw_img.rotate(180))
        elif self.pos == "270": # face oriented to left
            img, landmarks = self.detectLandmarks(raw_img.rotate(270))
        if landmarks is None: return None, None #  landmark detection fail
        img, landmarks = normalize(img, landmarks)
        return img, landmarks
    def showImgLandmarks(self, img, landmarks=None, ax=None):
        """
            visualize i-th frame with landmarks
        """
        if ax is None:
            fig = plt.figure()
            ax = fig.add_subplot(1,1,1)
        ax.imshow(img)
        if landmarks is not None:
            ax.scatter(landmarks[:,0], landmarks[...,1])
            for i in range(landmarks.shape[0]):
                ax.text(landmarks[i,0], landmarks[i,1], i)
        plt.show()
face_detector = dlib.get_frontal_face_detector()
landmark_detector = dlib.shape_predictor('./shape_predictor_68_face_landmarks.dat')
extractor = FrameExtract(face_detector, landmark_detector)
extractor.showImgLandmarks(*extractor.getIthFrame(0))

但是,当我尝试查看数据集中的图像时,我遇到了 TypeError:dtype 对象的图像数据无法转换为浮点数。 我收到以下错误消息:

extractor.showImgLandmarks(*extractor.getIthFrame(0))

    ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-3429da19673c> in <module>
----> 1 extractor.showImgLandmarks(*extractor.getIthFrame(0))

<ipython-input-3-6cc47ac83d27> in showImgLandmarks(self, img, landmarks, ax)
     62             fig = plt.figure()
     63             ax = fig.add_subplot(1,1,1)
---> 64         ax.imshow(img)
     65         if landmarks is not None:
     66             ax.scatter(landmarks[:,0], landmarks[...,1])

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
   1563     def inner(ax, *args, data=None, **kwargs):
   1564         if data is None:
-> 1565             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1566 
   1567         bound = new_sig.bind(ax, *args, **kwargs)

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\cbook\deprecation.py in wrapper(*args, **kwargs)
    356                 f"%(removal)s.  If any parameter follows name!r, they "
    357                 f"should be pass as keyword, not positionally.")
--> 358         return func(*args, **kwargs)
    359 
    360     return wrapper

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\cbook\deprecation.py in wrapper(*args, **kwargs)
    356                 f"%(removal)s.  If any parameter follows name!r, they "
    357                 f"should be pass as keyword, not positionally.")
--> 358         return func(*args, **kwargs)
    359 
    360     return wrapper

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
   5624                               resample=resample, **kwargs)
   5625 
-> 5626         im.set_data(X)
   5627         im.set_alpha(alpha)
   5628         if im.get_clip_path() is None:

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\image.py in set_data(self, A)
    691         if (self._A.dtype != np.uint8 and
    692                 not np.can_cast(self._A.dtype, float, "same_kind")):
--> 693             raise TypeError("Image data of dtype  cannot be converted to "
    694                             "float".format(self._A.dtype))
    695 

TypeError: Image data of dtype object cannot be converted to float

【问题讨论】:

谁知道你的数据是什么,但显然你在img 中传递一个对象数组而不是一个浮点数组。您可以通过检查适当的变量来检查这一点。 我正在准备从视频中提取顶点帧和中性帧。 (我的输入数据是视频)。 video_path = './videos/mmi-facial-expression-database_download_2019-05 13_03_06_41/Sessions/2/S001-002.avi' video_ID = 1 save_path = './onset_apex_pairs/0002' 【参考方案1】:

我正在准备从视频中提取顶点帧和中性帧。 (我的输入数据是视频)。

video_path = './videos/mmi-facial-expression-database_download_2019-05 13_03_06_41/Sessions/2/S001-002.avi'
video_ID = 1
save_path = './onset_apex_pairs/0002'

【讨论】:

以上是关于图像数据无法转换为浮点数错误?的主要内容,如果未能解决你的问题,请参考以下文章

Python 2.7:Canny Edge detection TypeError(“图像数据无法转换为浮点数”)中的错误

ValueError:无法将字符串转换为浮点数:'GIAC'

无法将 varchar 转换为浮点数

值错误:无法将字符串转换为浮点数

Numpy 错误“无法将字符串转换为浮点数:'Illinois'”

使用 sklearn 时出现错误:ValueError:无法将字符串转换为浮点数: