settings.py 11 KB

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