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

file_utils.py 3.1 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """
  2. Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. """
  13. from PIL import Image
  14. from io import BytesIO
  15. import os
  16. import time
  17. import requests
  18. import shutil
  19. import base64
  20. import json
  21. from pathlib import Path
  22. from tempfile import NamedTemporaryFile
  23. from logger import Logger
  24. log = Logger().logger
  25. MAX_TIME_LENGTH = 1000000
  26. def download_image(images_path):
  27. """
  28. 根据网络图片下载到本地
  29. """
  30. save_image_dir = "/usr/local/images/"
  31. if not os.path.exists(save_image_dir):
  32. os.mkdir(save_image_dir)
  33. for image_path in images_path:
  34. response = requests.get(image_path)
  35. response = response.content
  36. BytesIOObj = BytesIO()
  37. BytesIOObj.write(response)
  38. im = Image.open(BytesIOObj)
  39. im.save(
  40. save_image_dir + str(int(round(time.time() * MAX_TIME_LENGTH))) + "." + image_path.split("/")[-1].split(".")[-1])
  41. def upload_data(files):
  42. """
  43. 前端上传图片保存到本地
  44. """
  45. save_data_dir = "/usr/local/data/"
  46. if not os.path.exists(save_data_dir):
  47. os.mkdir(save_data_dir)
  48. data_list = list()
  49. for file in files:
  50. try:
  51. suffix = Path(file.filename).suffix
  52. with NamedTemporaryFile(delete=False, suffix=suffix, dir=save_data_dir) as tmp:
  53. shutil.copyfileobj(file.file, tmp)
  54. tmp_file_name = Path(tmp.name).name
  55. data = {"data_name": file.filename, "data_path": save_data_dir + tmp_file_name}
  56. data_list.append(data)
  57. finally:
  58. file.file.close
  59. return data_list
  60. def upload_image_by_base64(data_list):
  61. """
  62. base64图片信息保存到本地
  63. """
  64. save_data_dir = "/usr/local/data/"
  65. if not os.path.exists(save_data_dir):
  66. os.mkdir(save_data_dir)
  67. data_list_b64 = list()
  68. for data in data_list:
  69. file_path = save_data_dir + str(int(round(time.time() * MAX_TIME_LENGTH))) + "." + data.data_name.split(".")[-1]
  70. file_b64 = base64.b64decode(data.data_file)
  71. file = open(file_path, 'wb')
  72. file.write(file_b64)
  73. file.close()
  74. data_b64 = {"data_name": data.data_name, "data_path": file_path}
  75. data_list_b64.append(data_b64)
  76. return data_list_b64
  77. def writer_json_file(json_path, data_name, data):
  78. """
  79. 保存为json文件
  80. """
  81. if not os.path.exists(json_path):
  82. os.mkdir(json_path)
  83. filename = json_path + data_name + '.json'
  84. with open(filename, 'w', encoding='utf-8') as file_obj:
  85. file_obj.write(json.dumps(data, ensure_ascii=False))

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能