Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32 while using tf.image.crop_to_bounding_box

I am trying to crop a image depending on the bounding box.

bbox = [{'width': '500', 'ymin': '125', 'depth': '3', 'xmax': '387', 'xmin': '29', 'height': '375', 'ymax': '245'}]

ymin = float(bbox['ymin'])/float(bbox['height'])
ymax = float(bbox['ymax'])/float(bbox['height'])
xmin = float(bbox['xmin'])/float(bbox['width'])
xmax = float(bbox['xmax'])/float(bbox['width'])

total_height = tf.convert_to_tensor(ymax_int -  ymin_int)
total_width = tf.convert_to_tensor(xmax -  xmin) 
ymin =  tf.convert_to_tensor(ymin)
xmin  = tf.convert_to_tensor(xmin)

img=mpimg.imread(filename)
img = tf.convert_to_tensor(img)
image = tf.image.crop_to_bounding_box(img,  ymin,  xmin,  total_height, total_width)

I am getting the following error:

 ValueError: Tensor conversion requested dtype int32 for Tensor with dtype 
   float32: 'Tensor("Const_7:0", shape=(), dtype=float32)'

   Const_7:0 is ymin

Any help on how to fix this is appreciated

like image 645
user2851669 Avatar asked Nov 01 '25 23:11

user2851669


1 Answers

TensorFlow uses tf.slice to crop the image, which requires the data type of tensors total_height, total_width, ymin and xmin to be int32:

total_height = tf.convert_to_tensor(ymax_int -  ymin_int, dtype=tf.int32)
total_width = tf.convert_to_tensor(xmax -  xmin, dtype=tf.int32) 
ymin =  tf.convert_to_tensor(ymin, dtype=tf.int32)
xmin  = tf.convert_to_tensor(xmin, dtype=tf.int32)
like image 198
rvinas Avatar answered Nov 03 '25 14:11

rvinas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!