简体   繁体   English

类型错误:无法将图像数据转换为浮点数 无法将图像数据转换为浮点数

[英]TypeError: Image data cannot be converted to float Image data cannot be converted to float

As I trying to work on python with the open cv and the flask module to remove the background color from an image.当我尝试使用 open cvflask模块处理 python 以从图像中删除背景颜色时。 As running the code, I am getting this error:在运行代码时,我收到此错误:

File "new.py", line 82, in <module>
    plt.imshow(None)
  File "C:\Users\USER\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 2699, in imshow
    None else {}), **kwargs)
  File "C:\Users\USER\Anaconda3\lib\site-packages\matplotlib\__init__.py", line 1810, in inner
    return func(ax, *args, **kwargs)
  File "C:\Users\USER\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 5494, in imshow
    im.set_data(X)
  File "C:\Users\USER\Anaconda3\lib\site-packages\matplotlib\image.py", line 634, in set_data
    raise TypeError("Image data cannot be converted to float")
TypeError: Image data cannot be converted to float

I am not getting how to resolve this issue.And the code which im working :我不知道如何解决这个问题。我正在工作的代码:

import io, traceback

from flask import Flask, request, g
from flask import send_file
from flask_mako import MakoTemplates, render_template
from plim import preprocessor
import matplotlib.pyplot as plt
from PIL import Image, ExifTags
from scipy.misc import imresize
import numpy as np
import cv2
from keras.models import load_model
import tensorflow as tf

app = Flask(__name__, instance_relative_config=True)
# For Plim templates
mako = MakoTemplates(app)
app.config['MAKO_PREPROCESSOR'] = preprocessor
app.config.from_object('config')
image= cv2.imread("1.jpg")
graph = tf.get_default_graph()

def ml_predict(image):
    with graph.as_default():
       # Add a dimension for the batch
       prediction = img.predict(image[None, :, :, :])
       prediction = prediction.reshape((224,224, -1))
       return prediction

def rotate_by_exif(image):
    try:
       for orientation in ExifTags.TAGS.keys() :
           if ExifTags.TAGS[orientation]=='Orientation' : break
           exif=dict(image._getexif().items())
           if not orientation in exif:
               return image

           if exif[orientation] == 3 :
               image=image.rotate(180, expand=True)
           elif exif[orientation] == 6 :
               image=image.rotate(270, expand=True)
           elif exif[orientation] == 8 :
               image=image.rotate(90, expand=True)
           return image
    except:
        traceback.print_exc()
        return image

THRESHOLD = 0.5

def predict():
    # Load image
    #image = request.files['file']
    image = Image.open(image)
    image = rotate_by_exif(image)
    resized_image = imresize(image, (224, 224)) / 255.0
    # Model input shape = (224,224,3)
    # [0:3] - Take only the first 3 RGB channels and drop ALPHA 4th channel in case this is a PNG
    prediction = ml_predict(resized_image[:, :, 0:3])
    print('PREDICTION COUNT', (prediction[:, :, 1]>0.5).sum())

    # Resize back to original image size
    # [:, :, 1] = Take predicted class 1 - currently in our model = Person class. Class 0 = Background
    prediction = imresize(prediction[:, :, 1], (image.height, image.width))
    prediction[prediction>THRESHOLD*255] = 255
    prediction[prediction<THRESHOLD*255] = 0

    # Append transparency 4th channel to the 3 RGB image channels.
    transparent_image = np.append(np.array(image)[:, :, 0:3], prediction[: , :, None], axis=-1)
    transparent_image = Image.fromarray(transparent_image)



plt.imshow(None)
plt.show()

Any Help regarding this is really appreciated.任何有关这方面的帮助都非常感谢。 And in Advance Thank you so much for helping to resolve this issue.并提前非常感谢您帮助解决此问题。

You need to call plt.imshow(transparent_image) inside the predict function.您需要在predict函数中调用plt.imshow(transparent_image)

It complained that transparent_image is not defined because you tried to use that local variable of predict function outside of its scope.它抱怨没有定义transparent_image因为您试图在其范围之外使用predict函数的局部变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:图像数据无法转换为浮点数 - TypeError: Image data cannot be converted to float 图像数据无法转换为浮动 - Image data cannot be converted to float matplotlib.image.imsave TypeError:无法将图像数据转换为float - matplotlib.image.imsave TypeError: Image data cannot be converted to float TypeError:图像数据无法转换为少量图像浮动 - TypeError: Image data cannot be converted to float for few images 类型错误:dtype 对象的图像数据无法转换为浮点数 - TypeError: Image data of dtype object cannot be converted to float TypeError:图像数据无法在 wordcloud 项目上转换为浮点数 - TypeError: Image data cannot be converted to float on wordcloud project Python OpenCV 错误:“类型错误:图像数据无法转换为浮点数” - Python OpenCV Error: “TypeError: Image data cannot be converted to float” plt.imshow()给出TypeError(“图像数据无法转换为浮点数”) - plt.imshow() giving TypeError(“Image data cannot be converted to float”) TypeError:加载.npy文件时,图像数据无法转换为float - TypeError: Image data cannot be converted to float while loading .npy file TypeError: 图片数据不能转换为float // 导入.png图片时 - TypeError: Image data cannot be converted to float // When importing .png images
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM