|
|
- # import pickle
-
- # # Load the pickle file
- # with open("face_store.pkl", "rb") as f:
- # data = pickle.load(f)
-
- # # Print the data for inspection
- # print(data)
-
-
- # import json
- # import numpy as np
-
- # def convert_ndarray(obj):
- # """Recursively convert numpy.ndarray to list in a data structure."""
- # if isinstance(obj, np.ndarray):
- # return obj.tolist()
- # elif isinstance(obj, list):
- # return [convert_ndarray(item) for item in obj]
- # elif isinstance(obj, dict):
- # return {key: convert_ndarray(value) for key, value in obj.items()}
- # else:
- # return obj
-
- # # Assuming `data` contains numpy arrays
- # # data = {
- # # "name": "example",
- # # "embedding": np.array([1.0, 2.0, 3.0]),
- # # "nested": {
- # # "another_array": np.array([4.0, 5.0])
- # # }
- # # }
-
- # # Convert `data` to JSON-serializable format
- # data_serializable = convert_ndarray(data)
-
- # # Save as JSON
- # with open("data.json", "w") as json_file:
- # json.dump(data_serializable, json_file, indent=4)
-
- # print("JSON file saved successfully!")
-
-
- import pickle
- import json
- import numpy as np
-
- # Function to convert non-serializable objects (like numpy arrays) to a serializable format
- def convert_to_serializable(data):
- if isinstance(data, np.ndarray):
- return data.tolist() # Convert ndarray to list
- elif isinstance(data, dict): # If data is a dictionary, recursively convert values
- return {key: convert_to_serializable(value) for key, value in data.items()}
- elif isinstance(data, list): # If data is a list, recursively convert items
- return [convert_to_serializable(item) for item in data]
- else:
- return data # For other types, return as is
-
- # Load the pickle file
- with open("face_store.pkl", "rb") as f:
- data = pickle.load(f)
-
- # Convert the data to a JSON-serializable format
- data_serializable = convert_to_serializable(data)
-
- # Save to a JSON file
- with open("face_store.json", "w") as json_file:
- json.dump(data_serializable, json_file, indent=4)
-
- print("Data has been saved to face_store.json")
|