models icon indicating copy to clipboard operation
models copied to clipboard

ValueError: assignment destination is read-only

Open T-chuangxin opened this issue 7 years ago • 5 comments

mac os 14

I have no idea about this !!!

Here is the code , i have... `def detect_objects(image_np, sess, detection_graph): # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(image_np, axis=0) image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

# Each box represents a part of the image where a particular object was detected.
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

# Each score represent how level of confidence for each of the objects.
# Score is shown on the result image, together with the class label.
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')

# Actual detection.
(boxes, scores, classes, num_detections) = sess.run(
    [boxes, scores, classes, num_detections],
    feed_dict={image_tensor: image_np_expanded})

# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
    image_np,
    np.squeeze(boxes),
    np.squeeze(classes).astype(np.int32),
    np.squeeze(scores),
    category_index,
    use_normalized_coordinates=True,
    line_thickness=8)
return image_np

def process_image(image): # NOTE: The output you return should be a color image (3 channel) for processing video below # you should return the final output (image with lines are drawn on lanes) with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: image_process = detect_objects(image, sess, detection_graph) return image_process

white_output = '/Users/tianchuangxin1/models/research/object_detection/video3_3_out.mp4' clip1 = VideoFileClip("/Users/tianchuangxin1/models/research/object_detection/video1.mp4").subclip(0,1) white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!s %time white_clip.write_videofile(white_output, audio=False)

` The issue

`ValueError Traceback (most recent call last) in () 1 white_output = '/Users/tianchuangxin1/models/research/object_detection/video3_3_out.mp4' 2 clip1 = VideoFileClip("/Users/tianchuangxin1/models/research/object_detection/video1.mp4").subclip(0,1) ----> 3 white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!s 4 get_ipython().magic('time white_clip.write_videofile(white_output, audio=False)')

/Users/tianchuangxin1/anaconda/lib/python3.5/site-packages/moviepy/video/VideoClip.py in fl_image(self, image_func, apply_to) 509 if apply_to is None: 510 apply_to = [] --> 511 return self.fl(lambda gf, t: image_func(gf(t)), apply_to) 512 513 # --------------------------------------------------------------

/Users/tianchuangxin1/anaconda/lib/python3.5/site-packages/moviepy/Clip.py in fl(self, fun, apply_to, keep_duration) 136 137 #mf = copy(self.make_frame) --> 138 newclip = self.set_make_frame(lambda t: fun(self.get_frame, t)) 139 140 if not keep_duration:

in set_make_frame(self, mf)

/Users/tianchuangxin1/anaconda/lib/python3.5/site-packages/moviepy/decorators.py in outplace(f, clip, *a, **k) 12 """ Applies f(clip.copy(), *a, **k) and returns clip.copy()""" 13 newclip = clip.copy() ---> 14 f(newclip, *a, **k) 15 return newclip 16

/Users/tianchuangxin1/anaconda/lib/python3.5/site-packages/moviepy/video/VideoClip.py in set_make_frame(self, mf) 664 """ 665 self.make_frame = mf --> 666 self.size = self.get_frame(0).shape[:2][::-1] 667 668 @outplace

in get_frame(self, t)

/Users/tianchuangxin1/anaconda/lib/python3.5/site-packages/moviepy/decorators.py in wrapper(f, *a, **kw) 87 new_kw = {k: fun(v) if k in varnames else v 88 for (k,v) in kw.items()} ---> 89 return f(*new_a, **new_kw) 90 return decorator.decorator(wrapper) 91

/Users/tianchuangxin1/anaconda/lib/python3.5/site-packages/moviepy/Clip.py in get_frame(self, t) 93 return frame 94 else: ---> 95 return self.make_frame(t) 96 97 def fl(self, fun, apply_to=None, keep_duration=True):

/Users/tianchuangxin1/anaconda/lib/python3.5/site-packages/moviepy/Clip.py in (t) 136 137 #mf = copy(self.make_frame) --> 138 newclip = self.set_make_frame(lambda t: fun(self.get_frame, t)) 139 140 if not keep_duration:

/Users/tianchuangxin1/anaconda/lib/python3.5/site-packages/moviepy/video/VideoClip.py in (gf, t) 509 if apply_to is None: 510 apply_to = [] --> 511 return self.fl(lambda gf, t: image_func(gf(t)), apply_to) 512 513 # --------------------------------------------------------------

in process_image(image) 4 with detection_graph.as_default(): 5 with tf.Session(graph=detection_graph) as sess: ----> 6 image_process = detect_objects(image, sess, detection_graph) 7 return image_process

in detect_objects(image_np, sess, detection_graph) 26 category_index, 27 use_normalized_coordinates=True, ---> 28 line_thickness=8) 29 return image_np

/Users/tianchuangxin1/models/research/object_detection/utils/visualization_utils.py in visualize_boxes_and_labels_on_image_array(image, boxes, classes, scores, category_index, instance_masks, instance_boundaries, keypoints, use_normalized_coordinates, max_boxes_to_draw, min_score_thresh, agnostic_mode, line_thickness, groundtruth_box_visualization_color, skip_scores, skip_labels) 664 thickness=line_thickness, 665 display_str_list=box_to_display_str_map[box], --> 666 use_normalized_coordinates=use_normalized_coordinates) 667 if keypoints is not None: 668 draw_keypoints_on_image_array(

/Users/tianchuangxin1/models/research/object_detection/utils/visualization_utils.py in draw_bounding_box_on_image_array(image, ymin, xmin, ymax, xmax, color, thickness, display_str_list, use_normalized_coordinates) 128 thickness, display_str_list, 129 use_normalized_coordinates) --> 130 np.copyto(image, np.array(image_pil)) 131 132

ValueError: assignment destination is read-only

`

T-chuangxin avatar Feb 21 '19 06:02 T-chuangxin

this is due to numpy version <1.16, you can fix it by adding before line 130: image.setflags(write=1) or updating numpy !

bensdm avatar Feb 22 '19 00:02 bensdm

Thank you for your post. We noticed you have not filled out the following field in the issue template. Could you update them if they are relevant in your case, or leave them as N/A? Thanks. What is the top-level directory of the model you are using Have I written custom code OS Platform and Distribution TensorFlow installed from TensorFlow version Bazel version CUDA/cuDNN version GPU model and memory Exact command to reproduce

tensorflowbutler avatar Feb 22 '19 00:02 tensorflowbutler

It happens the same to me. I had version 1.15 of numpy. I have installed version 1.16.2 and the error does not disappear. Also try adding the line of code but it does not work.

this is due to numpy version <1.16, you can fix it by adding before line 130: image.setflags(write=1) or updating numpy !

rn-lpz avatar Mar 04 '19 18:03 rn-lpz

i had a similar issue, solved it in a hacky way by simply creating a copy of the image array, e.g.

image_copy = np.copy(image_np)
vis_util.visualize_boxes_and_labels_on_image_array(
    image_copy,
    np.squeeze(boxes),
    np.squeeze(classes).astype(np.int32),
    np.squeeze(scores),
    category_index,
    use_normalized_coordinates=True,
    line_thickness=8)
return image_copy

amm385 avatar Mar 11 '19 17:03 amm385

One can use np.array(image) to avoid this error

vis_util.visualize_boxes_and_labels_on_image_array( np.array(image), np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates=True, line_thickness=8)

msaqib17 avatar Nov 19 '19 04:11 msaqib17