I am working on a Python script that uses the NetworkX library and OpenCV to generate a video from a graph, where each node of the graph represents a state and the colors of the nodes indicate the current state. The code I am using is based on creating a Cayley tree from a dictionary of states and then generates a video showing the evolution of these states over time.
The problem I am experiencing is that the script works correctly for small JSON files, but when I try to use a larger JSON file, the script crashes and does not generate the expected video.
I encounter the following error:
Application Name with Errors: python.exe, Version: 3.11.2150.1013, Timestamp: 0x63e27f1eModule Name with Errors: ucrtbase.dll, Version: 10.0.22621.3374, Timestamp: 0xdf382650Exception Code: 0xc0000409Error Offset: 0x000000000007dfc8Error Process ID: 0x0x3CD8Error Application Start Time: 0x0x1DA9CB34F949896Application Path with Errors: C:\Program Files\Python311\python.exeModule Path with Errors: C:\windows\System32\ucrtbase.dllReport ID: e0ea6e06-58be-4a63-babe-0d3fe1c2d11dFull Name of the Package with Errors:Relative Application Identifier of the Package with Errors:My script in Python:
def main(): json_file = "path/to/your/file.json" root_node = "3" try: with open(json_file, "r") as file: data = [json.loads(line) for line in file if line.strip()] if not data: print("No valid data found in the JSON file.") return frame_rate = 5 frame_size = (800, 800) video_name = 'cayley_tree_video.mp4' fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter(video_name, fourcc, frame_rate, frame_size) for i, dictionary in enumerate(data): if root_node not in dictionary: break G = create_cayley_tree(dictionary, root_node) pos = graphviz_layout(G, prog="twopi", root=root_node, args='-Gsplines=true -Gnodesep=0.6 -Goverlap=scalexy') node_colors = ["green" if G.nodes[node]["state"] == "ON" else "red" for node in G.nodes()] draw_cayley_tree(G, node_colors, pos) filename = f"frame_{i:04d}.png" plt.savefig(filename) plt.close() image = cv2.imread(filename) out.write(image) os.remove(filename) saved_figures = i + 1 progress_percent = (saved_figures / len(data)) * 100 print(f"Saving figure {saved_figures}/{len(data)} - Progress: {progress_percent:.2f}%", end='\r') out.release() except Exception as e: print("An error occurred:", str(e)) traceback.print_exc()if __name__ == "__main__": main()If the JSON file I intend to use is small in size, the code runs. I've tried using sfc / scannow but it hasn't had any effect.
Do you know of any possible solution?
Thank you and regards.