首页 > 解决方案 > 如何使用张量流解码图像 roi?

问题描述

在使用 haarcasde 文件检测后从面部检测到图像后。但我收到了这个错误

/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_string_ops.py in substr(input, pos, len, unit, name)
   1888       _result = pywrap_tfe.TFE_Py_FastPathExecute(
   1889         _ctx._context_handle, tld.device_name, "Substr", name,
-> 1890         tld.op_callbacks, input, pos, len, "unit", unit)
   1891       return _result
   1892     except _core._FallbackException:

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int).

这是我的代码

def detect_and_noise_face(img):

    # Watch solutions video for line by line explanation!

    face_img = img.copy()
    roi = img.copy()

    face_rects = face_cascade.detectMultiScale(face_img,scaleFactor=1.3, minNeighbors=3) 
    for (x,y,w,h) in face_rects: 

        roi = roi[y:y+h,x:x+w]
        image = tf.image.decode_image(roi)

    return face_img

为什么我要解码图像,因为我试图在该 roi 上创建对抗性示例。 https://www.tensorflow.org/tutorials/generation/adversarial_fgsm 谢谢。

标签: pythonnumpyopencvtensorflow

解决方案


tf.image.decode_image用于将原始输入字节解码为张量,这里不是这种情况。

我想您想要的是将 numpy 数组 ( roi) 转换为tf.Tensor. 为此,您可以使用tf.convert_to_tensor https://www.tensorflow.org/api_docs/python/tf/convert_to_tensor


推荐阅读