|
-
- from api.model.model import PicoDet
- import time
- import cv2
- import json
- import datetime
- import os
- import sys
-
- def getFile(ruleFile):
- if getattr(sys, 'frozen', False):
- absPath = os.path.dirname(os.path.abspath(sys.executable))
- elif __file__:
- absPath = os.path.dirname(os.path.abspath(__file__))
- else:
- absPath = ''
- return os.path.join(absPath,ruleFile)
-
-
- def get_settings_status_name(data, settings,setting_name):
- # 访问指定 "setting" 的信息
- for setting in data[settings]:
- if setting["name"] == setting_name:
- # 使用 filter() 和 map() 函数提取信息
- names = list(map(lambda x: x["name"], filter(lambda x: x["status"], setting["status"])))
- return names[0]
- def get_setting_status(data, settings,setting_name):
- # 使用 filter() 和 map() 函数提取信息
- status = list(map(lambda x: x["status"], filter(lambda x: x["name"] == setting_name, data[settings])))
- return status[0]
-
- def get_photo(cap):
- # cap = cv2.VideoCapture(0) # 开启摄像头
- f, frame = cap.read() # 将摄像头中的一帧图片数据保存
- return frame
- # print('开始咯!')
- # while cv2.waitKey(1)==-1:
- # #计时
- def get_photo_detect(cap,net):
- '''
- 输入{图像,网络模型},输出[预测时间,预测结果]
- '''
- result = net.detect_img(get_photo(cap),show_result=False)
- return result
- def get_photo_detect_img(cap,net):
- '''
- 输入{图像,网络模型},输出[预测时间,预测图片]
- '''
- start = time.time()
- [result,img] = net.detect_img(get_photo(cap),show_result=True)
- # cv2.imshow('Video Cam', result)
- # # print(result)
- end = time.time()
- # print('time:',end-start)
- # cap.release() # 关闭摄像头
- # # cv2.destroyAllWindows()
- return [end-start,result,img]
- def get_photo_base64(cap):
- import base64
- # import numpy as np
- img = get_photo(cap)
- image = cv2.imencode('.jpg',img)[1]
- image_code = str(base64.b64encode(image))[2:-1]
- return image_code
- def mat2base64(frame):
- import base64
- # import numpy as np
- image = cv2.imencode('.jpg',frame)[1]
- image_code = str(base64.b64encode(image))[2:-1]
- return image_code
-
- class API:
- '''本地API,供前端JS调用'''
- window = None
- net = None
- cap = None
- args = None
-
-
- def __init__(self):
- with open(getFile("config.json"),'r',encoding='utf8')as fp:
- self.args = json.load(fp)
-
- self.net = PicoDet(
- get_settings_status_name(self.args,"ModelSetting","模型版本设置"),
- self.args['classfile'],
- prob_threshold=get_setting_status(self.args,"ModelSetting",'confThreshold'),
- iou_threshold=get_setting_status(self.args,"ModelSetting",'nmsThreshold'))
-
- # net.detect_folder(args['img_fold'], args['result_fold'])
-
- # 调用摄像头拍摄照片
- self.cap = cv2.VideoCapture(0) # 开启摄像头
-
- # cv2.namedWindow('Video Cam', cv2.WINDOW_NORMAL)
-
-
-
- def getPrimaryImg(self):
- self.window.evaluate_js('getPythonData()')
-
- return get_photo_base64(self.cap)
-
- def getDetectImg(self):
- [time,result,img] = get_photo_detect_img(self.cap,self.net)
- return [time,result,mat2base64(img)]#AttributeError: 'InferenceSession' object has no attribute 'detect'
- def getIndexSetting(self):
- toggle = self.args['toggle']
- tip = self.args['tip']
- control = self.args['control']
- return [toggle,tip,control]
- def getAdvanceSetting(self):
- return [self.args['ControlSetting'], self.args['ModelSetting'], self.args['otherSetting']]
- def changeSetting(self,data):
- self.args.update(data)
- with open(getFile("config.json"),'w',encoding='utf8')as fp:
- json.dump(self.args,fp,ensure_ascii=False,indent=4)
-
|