You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.1 KiB

  1. # import pickle
  2. # # Load the pickle file
  3. # with open("face_store.pkl", "rb") as f:
  4. # data = pickle.load(f)
  5. # # Print the data for inspection
  6. # print(data)
  7. # import json
  8. # import numpy as np
  9. # def convert_ndarray(obj):
  10. # """Recursively convert numpy.ndarray to list in a data structure."""
  11. # if isinstance(obj, np.ndarray):
  12. # return obj.tolist()
  13. # elif isinstance(obj, list):
  14. # return [convert_ndarray(item) for item in obj]
  15. # elif isinstance(obj, dict):
  16. # return {key: convert_ndarray(value) for key, value in obj.items()}
  17. # else:
  18. # return obj
  19. # # Assuming `data` contains numpy arrays
  20. # # data = {
  21. # # "name": "example",
  22. # # "embedding": np.array([1.0, 2.0, 3.0]),
  23. # # "nested": {
  24. # # "another_array": np.array([4.0, 5.0])
  25. # # }
  26. # # }
  27. # # Convert `data` to JSON-serializable format
  28. # data_serializable = convert_ndarray(data)
  29. # # Save as JSON
  30. # with open("data.json", "w") as json_file:
  31. # json.dump(data_serializable, json_file, indent=4)
  32. # print("JSON file saved successfully!")
  33. import pickle
  34. import json
  35. import numpy as np
  36. # Function to convert non-serializable objects (like numpy arrays) to a serializable format
  37. def convert_to_serializable(data):
  38. if isinstance(data, np.ndarray):
  39. return data.tolist() # Convert ndarray to list
  40. elif isinstance(data, dict): # If data is a dictionary, recursively convert values
  41. return {key: convert_to_serializable(value) for key, value in data.items()}
  42. elif isinstance(data, list): # If data is a list, recursively convert items
  43. return [convert_to_serializable(item) for item in data]
  44. else:
  45. return data # For other types, return as is
  46. # Load the pickle file
  47. with open("face_store.pkl", "rb") as f:
  48. data = pickle.load(f)
  49. # Convert the data to a JSON-serializable format
  50. data_serializable = convert_to_serializable(data)
  51. # Save to a JSON file
  52. with open("face_store.json", "w") as json_file:
  53. json.dump(data_serializable, json_file, indent=4)
  54. print("Data has been saved to face_store.json")