settings.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. from wsgiref import headers
  2. from dotenv import load_dotenv, find_dotenv
  3. from pathlib import Path # Python 3.6+ only
  4. import configparser, json, pytz
  5. import requests
  6. import pillow_avif
  7. from utils.common import message_queue
  8. import hashlib
  9. import os,stat
  10. TIME_ZONE = pytz.timezone("Asia/Shanghai")
  11. from numpy import true_divide
  12. from databases import (
  13. create_all_database,
  14. DeviceConfig,
  15. SysConfigs,
  16. CRUD,
  17. batch_insert_sys_configs,
  18. SqlQuery,
  19. batch_insert_device_configs,
  20. batch_insert_device_configsNew,
  21. )
  22. # 追加配置参数 machine_type 拍照机设备类型;0鞋;1服装
  23. MACHINE_TYPE = 0
  24. # 初始化数据表
  25. create_all_database()
  26. session = SqlQuery()
  27. device_config_crud = CRUD(DeviceConfig)
  28. all_devices = device_config_crud.read_all(session)
  29. if len(all_devices) == 0:
  30. # 如果配置表中一条数据都没有,就将初始化数据全部插入到数据表中
  31. action_tabs = json.load(open("action_tabs.json", encoding="utf-8"))
  32. actions = json.load(open("action.json", encoding="utf-8"))
  33. batch_insert_device_configs(session, action_tabs, actions)
  34. sys_config_crud = CRUD(SysConfigs)
  35. all_sys_configs = sys_config_crud.read_all(session)
  36. if len(all_sys_configs) == 0:
  37. # 如果配置表中一条数据都没有,就将初始化数据全部插入到数据表中
  38. sys_config_json = json.load(open("sys_configs.json", encoding="utf-8"))
  39. batch_insert_sys_configs(session, sys_config_json)
  40. else:
  41. # 查询数据库中缺失哪个数据,进行补充
  42. sys_config_json = json.load(open("sys_configs.json", encoding="utf-8"))
  43. for sys_config in sys_config_json:
  44. photos = CRUD(SysConfigs)
  45. item = photos.read(session, conditions={"key": sys_config.get("key")})
  46. if item == None:
  47. config = SysConfigs(**sys_config)
  48. session.add(config)
  49. session.commit() # 合并事务提交
  50. session.close()
  51. # 初始化数据表---结束
  52. def get_config_by_items(config_dict):
  53. __config_dict = {}
  54. for i, k in config_dict:
  55. __config_dict[i] = k
  56. return __config_dict
  57. keys = ["面料", "里料"]
  58. def getSysConfigs(key, item, default=None):
  59. session = SqlQuery()
  60. crud = CRUD(SysConfigs)
  61. one_item = crud.read(session, conditions={"key": key})
  62. config = json.loads(one_item.value)
  63. session.close()
  64. if item == "image_out_format":
  65. default_format = config.get(item, default)
  66. if default_format == "" or default_format == None:
  67. return "png"
  68. return config.get(item, default)
  69. def get_dict_value(_dict, key, default=None):
  70. if key in _dict:
  71. return _dict[key]
  72. else:
  73. return default
  74. # 鞋设备为1 服装设备为5
  75. MCU_CODE = 1
  76. MACHINE_LEVEL = (
  77. "二档"
  78. if getSysConfigs("other_configs", "device_speed", "二档") == ""
  79. else getSysConfigs("other_configs", "device_speed", "二档")
  80. )
  81. IS_TEST = False
  82. IS_MCU = True
  83. IS_LIN_SHI_TEST = False
  84. PhotographSeconds = float(
  85. 0.5
  86. if getSysConfigs("take_photo_configs", "camera_delay", "0.5") == ""
  87. else getSysConfigs("take_photo_configs", "camera_delay", "0.5")
  88. ) # 拍照停留时间
  89. MAX_PIXIAN_SIZE = 12000000
  90. def moveSpeed(level: str = None):
  91. config = {
  92. "一档": {
  93. "camera_high_motor": {
  94. "max_speed": 10000,
  95. "up_speed": 800,
  96. "down_speed": 700,
  97. },
  98. "turntable_steering": {
  99. "max_speed": 6000,
  100. "up_speed": 500,
  101. "down_speed": 400,
  102. },
  103. },
  104. "二档": {
  105. "camera_high_motor": {
  106. "max_speed": 7000,
  107. "up_speed": 600,
  108. "down_speed": 500,
  109. },
  110. "turntable_steering": {
  111. "max_speed": 4500,
  112. "up_speed": 350,
  113. "down_speed": 300,
  114. },
  115. },
  116. "三档": {
  117. "camera_high_motor": {
  118. "max_speed": 3500,
  119. "up_speed": 400,
  120. "down_speed": 300,
  121. },
  122. "turntable_steering": {
  123. "max_speed": 3000,
  124. "up_speed": 200,
  125. "down_speed": 200,
  126. },
  127. },
  128. }
  129. if level is None:
  130. return config[MACHINE_LEVEL]
  131. else:
  132. return config[level]
  133. config = configparser.ConfigParser()
  134. config_name = "config.ini"
  135. config.read(config_name, encoding="utf-8")
  136. # 应用名称
  137. APP_NAME = config.get("app", "app_name")
  138. # 应用版本号
  139. APP_VERSION = config.get("app", "version")
  140. # 是否开启调试模式
  141. IS_DEBUG = config.get("app", "debug")
  142. IS_UPLOAD_HLM = True if config.get("app", "is_upload") == "true" else False
  143. # 应用端口号
  144. PORT = config.get("app", "port")
  145. # 应用线程数
  146. APP_WORKS = config.get("app", "works")
  147. # 应用host地址
  148. APP_HOST = config.get("app", "host")
  149. # 应用服务启动名称
  150. APP_RUN = config.get("app", "app_run")
  151. # 日志名称
  152. LOG_FILE_NAME = config.get("log", "log_file_name")
  153. # 最大字节数
  154. MAX_BYTES = config.get("log", "max_bytes")
  155. print("Max bytes is", MAX_BYTES)
  156. # 备份数量
  157. BACKUP_COUNTS = config.get("log", "backup_counts")
  158. # 远程服务器地址
  159. HLM_HOST = config.get("log", "hlm_host")
  160. PROJECT = config.get("app", "project")
  161. # ----------------------------------
  162. mcu_config_dict = config.items("mcu_config")
  163. _mcu_config_dict = {}
  164. for i, k in mcu_config_dict:
  165. _mcu_config_dict[i] = int(k)
  166. # print(_mcu_config_dict)
  167. _config_mcu_config = get_config_by_items(config.items("mcu_config"))
  168. LEFT_FOOT_ACTION = _mcu_config_dict["left_foot_action"]
  169. LEFT_FOOT_PHOTOGRAPH = _mcu_config_dict["left_foot_photograph"]
  170. LEFT_FOOT_ACTION_1 = _mcu_config_dict["left_foot_action_1"]
  171. LEFT_FOOT_ACTION_2 = _mcu_config_dict["left_foot_action_2"]
  172. RIGHT_FOOT_ACTION = _mcu_config_dict["right_foot_action"]
  173. RIGHT_FOOT_PHOTOGRAPH = _mcu_config_dict["right_foot_photograph"]
  174. RIGHT_FOOT_ACTION_1 = _mcu_config_dict["right_foot_action_1"]
  175. RIGHT_FOOT_ACTION_2 = _mcu_config_dict["right_foot_action_2"]
  176. NEXT_STEP = int(get_dict_value(_config_mcu_config, "next_step", 6)) # 下一步
  177. MOVE_UP = _mcu_config_dict["move_up"]
  178. MOVE_DOWN = _mcu_config_dict["move_down"]
  179. STOP = _mcu_config_dict["stop"]
  180. # camera_config_dict = config.items("camera_config")
  181. # _camera_config_dict = {}
  182. # for i, k in mcu_config_dict:
  183. # _camera_config_dict[i] = int(k)
  184. # _camera_config_dict = get_config_by_items(camera_config_dict)
  185. # LOW_ISO = _camera_config_dict["low_iso"]
  186. # HIGH_ISO = _camera_config_dict["high_iso"]
  187. DOMAIN = (
  188. "https://dev2.valimart.net"
  189. if config.get("app", "env") != "dev"
  190. else "https://dev2.pubdata.cn"
  191. )
  192. Company = "惠利玛"
  193. is_test_plugins = true_divide
  194. OUT_PIC_MODE = "." + getSysConfigs("basic_configs", "image_out_format", "png") # ".png"
  195. OUT_PIC_SIZE = (
  196. [1600]
  197. if getSysConfigs("basic_configs", "main_image_size", [1600]) == ""
  198. else getSysConfigs("basic_configs", "main_image_size", [1600])
  199. ) # 主图大小
  200. Mode = getSysConfigs("other_configs", "product_type", "鞋类") # 程序执行类
  201. OUT_PIC_FACTOR = float(
  202. 1
  203. if getSysConfigs("basic_configs", "image_sharpening", "1") == ""
  204. else getSysConfigs("basic_configs", "image_sharpening", "1")
  205. ) # 图片锐化
  206. RESIZE_IMAGE_MODE = 1
  207. GRENERATE_MAIN_PIC_BRIGHTNESS = 254 # 色阶是否调整到位判断
  208. RUNNING_MODE = getSysConfigs("other_configs", "running_mode", "普通模式")
  209. DEFAULT_CUTOUT_MODE = getSysConfigs("other_configs", "cutout_mode", "普通抠图")
  210. CUTOUT_MODE = (
  211. 0 if getSysConfigs("other_configs", "cutout_mode", "普通抠图") == "普通抠图" else 1
  212. )
  213. import importlib
  214. # plugins = [
  215. # # "custom_plugins.plugins.detail_template.huilima.detail_huilima1.DetailPicGet",
  216. # ".custom_plugins.plugins.detail_template.huilima.detail_huilima1.DetailPicGet",
  217. # # "custom_plugins.plugins.detail_template.huilima.detail_huilima2.DetailPicGet",
  218. # # "custom_plugins.plugins.detail_template.huilima.detail_huilima3.DetailPicGet",
  219. # # "custom_plugins.plugins.detail_template.huilima.detail_huilima4.DetailPicGet",
  220. # # "custom_plugins.plugins.detail_template.xiaosushuoxie.detail_xiaosushuoxie1",
  221. # # "custom_plugins.plugins.detail_template.xiaosushuoxie.detail_xiaosushuoxie2",
  222. # # "custom_plugins.plugins.detail_template.xiaosushuoxie.detail_xiaosushuoxie3",
  223. # # "custom_plugins.plugins.detail_template.xiaosushuoxie.detail_xiaosushuoxie4",
  224. # # "custom_plugins.plugins.detail_template.xiaosushuoxie.detail_xiaosushuoxie5",
  225. # # "custom_plugins.plugins.detail_template.xiaosushuoxie.detail_xiaosushuoxie6",
  226. # # "custom_plugins.plugins.detail_template.hongqingting.detail_hongqingting1.DetailPicGet",
  227. # # "custom_plugins.plugins_mode.detail_generate_base.DetailBase",
  228. # # "custom_plugins.plugins_mode.pic_deal.PictureProcessing",
  229. # ]
  230. # def load_plugin(plugin_path):
  231. # module_name, class_name = plugin_path.rsplit(".", 1)
  232. # module = importlib.import_module(module_name)
  233. # return getattr(module, class_name)
  234. # loaded_plugins = [load_plugin(p.lstrip(".")) for p in plugins]
  235. OUT_PIC_QUALITY = "普通"
  236. GRENERATE_MAIN_PIC_BRIGHTNESS = int(
  237. getSysConfigs("other_configs", "grenerate_main_pic_brightness", 254)
  238. ) # 色阶是否调整到位判断
  239. SHADOW_PROCESSING = int(
  240. getSysConfigs("other_configs", "shadow_processing", 0)
  241. ) # 0表示要直线和曲线,1 表示只要直线
  242. LOWER_Y = int(
  243. getSysConfigs("other_configs", "lower_y", 4)
  244. ) # 鞋底以下多少距离作为阴影蒙版
  245. CHECK_LOWER_Y = int(
  246. getSysConfigs("other_configs", "check_lower_y", 4)
  247. ) # 检测亮度区域,倒数第几行
  248. IS_GET_GREEN_MASK = (
  249. True if getSysConfigs("other_configs", "is_get_green_mask", "否") == "是" else False
  250. ) # 是否进行绿幕抠图
  251. IMAGE_SAVE_MAX_WORKERS = int(
  252. getSysConfigs("other_configs", "image_save_max_workers", 20)
  253. ) # 批量保存的线程大小
  254. COLOR_GRADATION_CYCLES = int(
  255. getSysConfigs("other_configs", "color_gradation_cycles", 23)
  256. ) # 色阶处理循环次数
  257. def recordDataPoint(token=None, page="", uuid=None, data=""):
  258. """记录日志"""
  259. if token == None:
  260. return
  261. if page == "":
  262. return
  263. if uuid == None:
  264. return
  265. headers = {"Content-Type": "application/json", "authorization": "Bearer " + token}
  266. params = {
  267. "site": 1,
  268. "type": 5,
  269. "channel": "aigc-camera",
  270. "uuid": uuid,
  271. "page": page,
  272. "page_url": "/",
  273. "describe": {"action": page, "data": data},
  274. "time": 0,
  275. }
  276. return requests.post(
  277. headers=headers, data=json.dumps(params), url=DOMAIN + "/api/record/point"
  278. )
  279. async def sendSocketMessage(
  280. code=0, msg="", data=None, device_status=2, msg_type=""
  281. ):
  282. data = {
  283. "code": code,
  284. "msg": msg,
  285. "status": device_status,
  286. "data": data,
  287. "msg_type": msg_type,
  288. }
  289. await message_queue.put(data)
  290. def calculate_md5(filepath):
  291. # 打开文件,以二进制只读模式打开
  292. with open(filepath, "rb") as f:
  293. # 创建MD5哈希对象
  294. md5hash = hashlib.md5()
  295. # 循环读取文件的内容并更新哈希对象
  296. for chunk in iter(lambda: f.read(4096), b""):
  297. md5hash.update(chunk)
  298. # 返回MD5哈希的十六进制表示
  299. return md5hash.hexdigest()
  300. def handle_remove_readonly(func, path, exc):
  301. os.chmod(path, stat.S_IWRITE)
  302. func(path)