- Saved searches
- Use saved searches to filter your results more quickly
- ValueError: assignment destination is read-only #6242
- ValueError: assignment destination is read-only #6242
- Comments
- ` The issue
- Valueerror assignment destination is read-only
- What does the ValueError: assignment destination is read-only error mean?
- How the Error Occurs?
- Solutions for ValueError: assignment destination is read-only
- Solution 1: Use Mutable Data Structures
- Solution 2: Reassign Variables
- Solution 3: Check Documentation and Restrictions
- Solution 4: Use a Copy or Clone
- Solution 5: Identify Context-Specific Solutions
- Solution 6: Seek Help from the Community
- Frequently Asked Questions
- Conlcusion
- Additional Resources
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ValueError: assignment destination is read-only #6242
ValueError: assignment destination is read-only #6242
models:research models that come under research directory stat:awaiting response Waiting on input from the contributor
Comments
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=) # 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:
/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
/Users/tianchuangxin1/anaconda/lib/python3.5/site-packages/moviepy/decorators.py in wrapper(f, *a, **kw)
87 new_kw = 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
The text was updated successfully, but these errors were encountered:
Valueerror assignment destination is read-only
One of the errors that developers often come across is the ValueError: assignment destination is read-only.
This error typically occurs when you try to modify a read-only object or variable.
What does the ValueError: assignment destination is read-only error mean?
This error occurs when you try to modify a read-only object or variable in your code. The error message indicates that the assignment destination is restricted to being read-only.
How the Error Occurs?
These are the following examples of how the error occurs.
Example 1: Modifying an Immutable Tuple
my_tuple = (1, 2, 3) my_tuple[0] = 10
- The code creates a tuple called “my_tuple” with the values 1, 2, and 3.
- Tuples are immutable, meaning their elements cannot be changed once they are created.
- The line “my_tuple[0] = 10” attempts to modify the value at index 0 of the tuple to 10, which is not allowed and will result in a valueerror.
Example 2: Attempting to Modify a String
my_string = "Hello, World!" my_string[0] = 'J'
The code attempts to change the first character of a string but produces a valueerror because strings cannot be directly modified.
Example 3: Assigning to a Constant
import math math.pi = 3.14
The code example attempts to assign a new value (3.14) to the constant math.pi in Python.
However, in Python, constants are typically immutable and cannot be modified once defined.
Therefore, this code will likely raise a valueerror .
Example 4: Altering an Immutable Data Structure
my_dict = my_dict['key'] = 'new_value'
This code example demonstrates how to modify a value in an immutable data structure in Python.
It starts with a dictionary called “my_dict” that has a key-value pair of ‘key‘ and ‘value‘.
The code then changes the value associated with the ‘key’ to ‘new_value‘ by assigning it directly using indexing.
Solutions for ValueError: assignment destination is read-only
Here are some solutions to solve the ValueError: assignment destination is read-only:
Solution 1: Use Mutable Data Structures
Instead of using immutable data structures like tuples or strings, switch to mutable ones such as lists or dictionaries.
Mutable objects allow modifications, eliminating the read-only error.
my_list = [1, 2, 3] my_list[0] = 10
Solution 2: Reassign Variables
If you encounter the valueerror while attempting to modify a variable, try reassigning it with a new value.
This method can help you resolve the read-only restriction.
my_variable = 10 my_variable = 20
Solution 3: Check Documentation and Restrictions
In some cases, certain objects or variables are intentionally designed to be read-only.
It’s important to consult the documentation or source code to understand any restrictions required.
Make sure that the object you’re attempting to modify is meant to be changed.
Solution 4: Use a Copy or Clone
If the object you’re working with is meant to be read-only, consider creating a copy or clone of it.
By doing this, you can modify the duplicate without affecting the original read-only object.
my_tuple = (1, 2, 3) my_modified_tuple = list(my_tuple) my_modified_tuple[0] = 10
Solution 5: Identify Context-Specific Solutions
The ValueError: assignment destination is read-only error that can be context-specific.
Analyze the specific context where the error occurs and seek solutions to that scenario.
Understanding the possible cause will aid in finding a proper resolution.
Solution 6: Seek Help from the Community
If all the solutions are not working, don’t hesitate to reach out to the programming community for assistance.
Online forums, developer communities, and platforms like Stack Overflow can provide valuable insights and guidance from experienced programmers.
Frequently Asked Questions
To resolve this valueerror, you can apply different solutions such as using mutable data structures, reassigning variables, checking documentation and restrictions, using copies or clones, identifying context-specific solutions, or seeking help from the programming community.
Why am I getting the ValueError assignment destination is read-only error when trying to modify a variable?
The error arises because the variable you are attempting to modify is marked as read-only.
It could be intentionally designed this way or due to a restriction imposed by the programming language or framework you are using.
In some cases, you can convert a read-only object into a writable one by using techniques like creating a copy or clone of the object.
However, this depends on the specific context and the restrictions imposed on the program.
Yes, there are similar errors that you may encounter in different programming languages.
For example, in JavaScript, you might encounter the error TypeError: Assignment to a constant variable when trying to modify a constant.
Conlcusion
The ValueError: assignment destination is read-only error can be encountered when attempting to modify read-only objects or variables in your code.
By following the solutions provided in this article, such as using mutable data structures, reassigning variables, checking restrictions, making copies or clones, considering context-specific solutions, and seeking community help, you can effectively resolve this error.