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 10 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
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. def getModelSetting(args):
  68. import requests
  69. url = 'https://gitee.com/JiangNoah/emc/raw/master/model.json'
  70. r = requests.get(url)
  71. model_dict = (r.json())["models"]
  72. #将get_settings_status_name(args,"ModelSetting","模型版本设置")在model_dict的list中对应的status设置为true
  73. for model in model_dict:
  74. if model["name"] == get_settings_status_name(args,"ModelSetting","模型版本设置"):
  75. model["status"] = True
  76. else:
  77. model["status"] = False
  78. args["ModelSetting"][0]["status"] = model_dict
  79. return args
  80. class API:
  81. '''本地API,供前端JS调用'''
  82. window = None
  83. net = None
  84. cap = None
  85. args = None
  86. def __init__(self):
  87. with open(getFile("config.json"),'r',encoding='utf8')as fp:
  88. self.args = json.load(fp)
  89. self.args = getModelSetting(self.args)
  90. self.net = PicoDet(
  91. get_settings_status_name(self.args,"ModelSetting","模型版本设置"),
  92. self.args['classfile'],
  93. prob_threshold=float(get_setting_status(self.args,"ModelSetting",'confThreshold')),
  94. iou_threshold=float(get_setting_status(self.args,"ModelSetting",'nmsThreshold')))
  95. # net.detect_folder(args['img_fold'], args['result_fold'])
  96. # 调用摄像头拍摄照片
  97. self.cap = cv2.VideoCapture(0) # 开启摄像头
  98. # cv2.namedWindow('Video Cam', cv2.WINDOW_NORMAL)
  99. if(self.args['toggle'] and self.args['tip']):
  100. send_notifycation("功能触发:鼠标悬停","EMC[嘴控]")
  101. def getPrimaryImg(self,type="不裁剪"):
  102. # 三种处理图片的方式:不裁剪、居中裁剪成224*224、居中裁剪成320*320。
  103. if(type=="不裁剪"):
  104. return get_photo_base64(self.cap)
  105. elif(type=="居中裁剪成224*224"):
  106. img = get_photo(self.cap)
  107. height=len(img)
  108. width=len(img[0])
  109. if(height>224 and width>224):
  110. y0 = height//2
  111. x0 = width//2
  112. x1 = x0-112
  113. y1 = y0-112
  114. x2 = x0+112
  115. y2 = y0+112
  116. img = img[y1:y2, x1:x2]
  117. return mat2base64(img)
  118. elif(type=="居中裁剪成320*320"):
  119. img = get_photo(self.cap)
  120. height=len(img)
  121. width=len(img[0])
  122. if(height>320 and width>320):
  123. y0 = height//2
  124. x0 = width//2
  125. x1 = x0-160
  126. y1 = y0-160
  127. x2 = x0+160
  128. y2 = y0+160
  129. img = img[y1:y2, x1:x2]
  130. return mat2base64(img)
  131. def getDetectImg(self):
  132. [time,result,img] = get_photo_detect_img(self.cap,self.net)
  133. return [time,result,mat2base64(img)]#AttributeError: 'InferenceSession' object has no attribute 'detect'
  134. def getIndexSetting(self):
  135. toggle = self.args['toggle']
  136. tip = self.args['tip']
  137. control = self.args['control']
  138. return [toggle,tip,control]
  139. def getAdvanceSetting(self):
  140. return [self.args['ControlSetting'], self.args['ModelSetting'], self.args['otherSetting']]
  141. def changeSetting(self,data):
  142. self.args.update(data)
  143. with open(getFile("config.json"),'w',encoding='utf8')as fp:
  144. json.dump(self.args,fp,ensure_ascii=False,indent=4)
  145. def getPersonalizationPrimaryImg(self):
  146. #判断是否存在"primary_img"文件夹
  147. if(os.path.exists(getFile("primary_img"))==False):
  148. #不存在则创建
  149. os.mkdir(getFile("primary_img"))
  150. #返回该文件夹下jpg文件的数量和列表
  151. return [len(os.listdir(getFile("primary_img"))),os.listdir(getFile("primary_img"))]
  152. def setPersonalizationPrimaryImg(self,type="不裁剪"):
  153. #判断是否存在"primary_img"文件夹
  154. if(os.path.exists(getFile("primary_img"))==False):
  155. #不存在则创建
  156. os.mkdir(getFile("primary_img"))
  157. #保存图片
  158. # 图片名字是当前时间
  159. # 三种处理图片的方式:不裁剪、居中裁剪成224*224、居中裁剪成320*320。
  160. if(type=="不裁剪"):
  161. cv2.imwrite(getFile("primary_img")+"/"+time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())+".jpg",get_photo(self.cap))
  162. elif(type=="居中裁剪成224*224"):
  163. img = get_photo(self.cap)
  164. height=len(img)
  165. width=len(img[0])
  166. if(height>224 and width>224):
  167. y0 = height//2
  168. x0 = width//2
  169. x1 = x0-112
  170. y1 = y0-112
  171. x2 = x0+112
  172. y2 = y0+112
  173. img = img[y1:y2, x1:x2]
  174. cv2.imwrite(getFile("primary_img")+"/"+time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())+".jpg",img)
  175. elif(type=="居中裁剪成320*320"):
  176. img = get_photo(self.cap)
  177. height=len(img)
  178. width=len(img[0])
  179. if(height>320 and width>320):
  180. y0 = height//2
  181. x0 = width//2
  182. x1 = x0-160
  183. y1 = y0-160
  184. x2 = x0+160
  185. y2 = y0+160
  186. img = img[y1:y2, x1:x2]
  187. cv2.imwrite(getFile("primary_img")+"/"+time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())+".jpg",img)
  188. #返回该文件夹下jpg文件的数量和列表
  189. return [len(os.listdir(getFile("primary_img"))),os.listdir(getFile("primary_img"))]
  190. def openPrimaryImgFiles(self):
  191. #判断是否存在"primary_img"文件夹
  192. if(os.path.exists(getFile("primary_img"))==False):
  193. #不存在则创建
  194. os.mkdir(getFile("primary_img"))
  195. #打开文件夹
  196. os.startfile(getFile("primary_img"))
  197. def getPrimaryListImg(self,img_name):
  198. #判断是否存在"primary_img"文件夹
  199. if(os.path.exists(getFile("primary_img"))==False):
  200. #不存在则创建
  201. os.mkdir(getFile("primary_img"))
  202. #读取图片
  203. img = cv2.imread(getFile("primary_img")+"/"+img_name)
  204. #返回图片
  205. return mat2base64(img)
  206. def saveAnnotationData(self,data,dataset):
  207. #判断是否存在"annotation_data"文件夹
  208. if(os.path.exists(getFile("annotation_data"))==False):
  209. #不存在则创建
  210. os.mkdir(getFile("annotation_data"))
  211. #保存COCO数据集
  212. with open(getFile("annotation_data")+"/"+"dataset.json",'w',encoding='utf8')as fp:
  213. json.dump(dataset,fp,ensure_ascii=False,indent=4)
  214. #保存标注数据
  215. with open(getFile("annotation_data")+"/"+"data.json",'w',encoding='utf8')as fp:
  216. json.dump(data,fp,ensure_ascii=False,indent=4)
  217. def getAnnotationData(self):
  218. #判断是否存在"annotation_data"文件夹
  219. if(os.path.exists(getFile("annotation_data"))==False):
  220. #不存在则创建
  221. os.mkdir(getFile("annotation_data"))
  222. #判断是否存在"dataset.json"文件
  223. if(os.path.exists(getFile("annotation_data")+"/"+"dataset.json")):
  224. #存在则读取
  225. with open(getFile("annotation_data")+"/"+"dataset.json",'r',encoding='utf8')as fp:
  226. dataset = json.load(fp)
  227. else:
  228. #不存在则创建
  229. return {}
  230. #判断是否存在"data.json"文件
  231. if(os.path.exists(getFile("annotation_data")+"/"+"data.json")):
  232. #存在则读取
  233. with open(getFile("annotation_data")+"/"+"data.json",'r',encoding='utf8')as fp:
  234. data = json.load(fp)
  235. else:
  236. #不存在则创建
  237. return {}
  238. #返回数据
  239. return {"mergeData":dataset,"annotationData":data}