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.

api.py 4.1 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. from api.model.model import PicoDet
  2. import time
  3. import cv2
  4. import json
  5. import datetime
  6. import os
  7. import sys
  8. from utils.utils import send_notifycation
  9. def getFile(ruleFile):
  10. if getattr(sys, 'frozen', False):
  11. absPath = os.path.dirname(os.path.abspath(sys.executable))
  12. elif __file__:
  13. absPath = os.path.dirname(os.path.abspath(__file__))
  14. else:
  15. absPath = ''
  16. return os.path.join(absPath,ruleFile)
  17. def get_settings_status_name(data, settings,setting_name):
  18. # 访问指定 "setting" 的信息
  19. for setting in data[settings]:
  20. if setting["name"] == setting_name:
  21. # 使用 filter() 和 map() 函数提取信息
  22. names = list(map(lambda x: x["name"], filter(lambda x: x["status"], setting["status"])))
  23. return names[0]
  24. def get_setting_status(data, settings,setting_name):
  25. # 使用 filter() 和 map() 函数提取信息
  26. status = list(map(lambda x: x["status"], filter(lambda x: x["name"] == setting_name, data[settings])))
  27. return status[0]
  28. def get_photo(cap):
  29. # cap = cv2.VideoCapture(0) # 开启摄像头
  30. f, frame = cap.read() # 将摄像头中的一帧图片数据保存
  31. return frame
  32. # print('开始咯!')
  33. # while cv2.waitKey(1)==-1:
  34. # #计时
  35. def get_photo_detect(cap,net):
  36. '''
  37. 输入{图像,网络模型},输出[预测时间,预测结果]
  38. '''
  39. result = net.detect_img(get_photo(cap),show_result=False)
  40. return result
  41. def get_photo_detect_img(cap,net):
  42. '''
  43. 输入{图像,网络模型},输出[预测时间,预测图片]
  44. '''
  45. start = time.time()
  46. [result,img] = net.detect_img(get_photo(cap),show_result=True)
  47. # cv2.imshow('Video Cam', result)
  48. # # print(result)
  49. end = time.time()
  50. # print('time:',end-start)
  51. # cap.release() # 关闭摄像头
  52. # # cv2.destroyAllWindows()
  53. return [end-start,result,img]
  54. def get_photo_base64(cap):
  55. import base64
  56. # import numpy as np
  57. img = get_photo(cap)
  58. image = cv2.imencode('.jpg',img)[1]
  59. image_code = str(base64.b64encode(image))[2:-1]
  60. return image_code
  61. def mat2base64(frame):
  62. import base64
  63. # import numpy as np
  64. image = cv2.imencode('.jpg',frame)[1]
  65. image_code = str(base64.b64encode(image))[2:-1]
  66. return image_code
  67. class API:
  68. '''本地API,供前端JS调用'''
  69. window = None
  70. net = None
  71. cap = None
  72. args = None
  73. def __init__(self):
  74. with open(getFile("config.json"),'r',encoding='utf8')as fp:
  75. self.args = json.load(fp)
  76. self.net = PicoDet(
  77. get_settings_status_name(self.args,"ModelSetting","模型版本设置"),
  78. self.args['classfile'],
  79. prob_threshold=float(get_setting_status(self.args,"ModelSetting",'confThreshold')),
  80. iou_threshold=float(get_setting_status(self.args,"ModelSetting",'nmsThreshold')))
  81. # net.detect_folder(args['img_fold'], args['result_fold'])
  82. # 调用摄像头拍摄照片
  83. self.cap = cv2.VideoCapture(0) # 开启摄像头
  84. # cv2.namedWindow('Video Cam', cv2.WINDOW_NORMAL)
  85. if(self.args['toggle'] and self.args['tip']):
  86. send_notifycation("功能触发:鼠标悬停","EMC[嘴控]")
  87. def getPrimaryImg(self):
  88. self.window.evaluate_js('getPythonData()')
  89. return get_photo_base64(self.cap)
  90. def getDetectImg(self):
  91. [time,result,img] = get_photo_detect_img(self.cap,self.net)
  92. return [time,result,mat2base64(img)]#AttributeError: 'InferenceSession' object has no attribute 'detect'
  93. def getIndexSetting(self):
  94. toggle = self.args['toggle']
  95. tip = self.args['tip']
  96. control = self.args['control']
  97. return [toggle,tip,control]
  98. def getAdvanceSetting(self):
  99. return [self.args['ControlSetting'], self.args['ModelSetting'], self.args['otherSetting']]
  100. def changeSetting(self,data):
  101. self.args.update(data)
  102. with open(getFile("config.json"),'w',encoding='utf8')as fp:
  103. json.dump(self.args,fp,ensure_ascii=False,indent=4)