Image data cannot be converted to float error?

225 Views Asked by At

I am trying to detect face and return an img corresponding landmarks after face alignment. this is my code:

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))

However, when I try to view the images in my dataset, I get hit with the TypeError: Image data of dtype object cannot be converted to float. I get the following error message on the line:

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

1

There are 1 best solutions below

0
On

I am traying to extract the apex frame and the neutral frame from a video. (My input data is a video).

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'