加载算法
This commit is contained in:
parent
0bd71ba016
commit
681866ba3e
|
@ -1,7 +1,12 @@
|
|||
|
||||
|
||||
import base64
|
||||
from io import BytesIO
|
||||
import requests
|
||||
import grpc
|
||||
import BeiHangGrpc_pb2_grpc
|
||||
import BeiHangGrpc_pb2
|
||||
# from apps.utils.tools import convert_to_base64
|
||||
|
||||
|
||||
def helmet(ip: str, pic_url: str):
|
||||
|
@ -20,17 +25,22 @@ def helmet(ip: str, pic_url: str):
|
|||
# with open(image_path, 'rb') as f:
|
||||
# image_base64 = str(base64.b64encode(f.read()), encoding='utf-8')
|
||||
# request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id,imgsbase64=image_base64)
|
||||
|
||||
request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgUrl=pic_url)
|
||||
base64img = str(base64.b64encode(BytesIO(requests.get(url=path).content).read()), 'utf-8')
|
||||
request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgsbase64=base64img)
|
||||
# request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgUrl=pic_url)
|
||||
response = stub.sendHelmet_Info(request)
|
||||
if response.helmetinfoList:
|
||||
for i in response.helmetinfoList:
|
||||
if i.head_helmet == 0:
|
||||
if i.head_helmet == 0: # 未戴安全帽事件成立
|
||||
return True, response
|
||||
return False, response
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
path = 'http://10.99.5.79:20309/media/2022/08/25/dsf_e9de9deb-213e-11ed-884a-e4246c7d1635_11584620_11593689.jpg'
|
||||
base64img = str(base64.b64encode(BytesIO(requests.get(url=path).content).read()), 'utf-8')
|
||||
with open('face_test.txt', 'w') as f:
|
||||
f.write(base64img)
|
||||
helmet(
|
||||
ip='122.224.229.24:2000',
|
||||
pic_url='http://1.203.161.103:2800/media/2022/07/06/dsf_bf6446cc-f9cd-11ec-bd2d-e4246c7d1635_34597240_34614457.jpg')
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1,18 +1,40 @@
|
|||
|
||||
import importlib
|
||||
from django.conf import settings
|
||||
import logging
|
||||
myLogger = logging.getLogger('log')
|
||||
|
||||
|
||||
algo_dict = {
|
||||
"helmet": "apps.helmet.algo.helmet"
|
||||
}
|
||||
|
||||
|
||||
class AiService:
|
||||
def ai_analyse(codes: list, global_img: str, face_img: str=None):
|
||||
"""多线程进行算法分析图片
|
||||
|
||||
def analyse(codes: list, path: str):
|
||||
"""多线程进行算法分析图片
|
||||
|
||||
Args:
|
||||
codes: 算法列表
|
||||
path (str): 图片地址
|
||||
"""
|
||||
for i in codes:
|
||||
if i in algo_dict:
|
||||
pass
|
||||
Args:
|
||||
codes: 算法列表
|
||||
global_img (str): 全景图片地址
|
||||
face_img (str): 人脸图片地址
|
||||
"""
|
||||
results = {}
|
||||
for i in codes:
|
||||
if i in algo_dict: # 如果算法支持
|
||||
module, func = algo_dict[i].rsplit(".", 1)
|
||||
m = importlib.import_module(module)
|
||||
f = getattr(m, func)
|
||||
try:
|
||||
is_happend, res = False, None
|
||||
if i == 'helmet': # 如果是安全帽识别
|
||||
if face_img:
|
||||
is_happend, res = f(ip=settings.AI_IP, path=face_img)
|
||||
else:
|
||||
is_happend, res = f(ip=settings.AI_IP, path=global_img)
|
||||
else:
|
||||
is_happend, res = f(ip=settings.AI_IP, path=global_img)
|
||||
if is_happend:
|
||||
results.update({i: res})
|
||||
except Exception:
|
||||
myLogger.error('算法处理错误', exc_info=True)
|
||||
return results
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
|
||||
from apps.ai.main import ai_analyse
|
||||
from apps.opm.models import Operation, Opl, OplWorker
|
||||
from apps.utils.sms import send_sms
|
||||
import requests
|
||||
|
@ -67,17 +68,6 @@ def get_ep_default():
|
|||
}
|
||||
|
||||
|
||||
def algo_handle(codes: list, data: dict):
|
||||
"""算法处理
|
||||
|
||||
Args:
|
||||
code (str): 算法标识列表
|
||||
data (dict): 需要处理的图片信息
|
||||
|
||||
"""
|
||||
return ['helmet']
|
||||
|
||||
|
||||
def notify_event(event: Event, voice_msg=''):
|
||||
"""事件后续处理:
|
||||
|
||||
|
@ -237,10 +227,10 @@ def dispatch_dahua_event(data: dict):
|
|||
ep = None # 对应人员
|
||||
if alarm_type == 1001003: # 内部人员
|
||||
ep = Employee.objects.filter(id_number=data['info']['extend']['candidateInfo'][0]['id']).first()
|
||||
ec_codes = algo_handle(algo_codes, data={}) # 算法处理
|
||||
ec_codes = ai_analyse(codes=algo_codes, global_img=global_img_o, face_img=face_img_o) # 算法处理
|
||||
if ec_codes: # 如果触发事件
|
||||
# 获取本次所有发生事件种类
|
||||
ecs = EventCate.objects.filter(code__in=ec_codes)
|
||||
ecs = EventCate.objects.filter(code__in=ec_codes.keys())
|
||||
# 创建事件
|
||||
event = Event()
|
||||
event.face_img = save_dahua_pic(face_img_o)
|
||||
|
@ -277,10 +267,10 @@ def dispatch_dahua_event(data: dict):
|
|||
algo_codes = list(AlgoChannel.objects.filter(vchannel=vchannel, algo__self_algo=True).exclude(
|
||||
algo__code=None).order_by('algo__priority', 'algo__create_time').values_list('algo__code', flat=True))
|
||||
if algo_codes:
|
||||
ec_codes = algo_handle(algo_codes, data={}) # 算法处理
|
||||
ec_codes = ai_analyse(algo_codes, global_img=global_img_o) # 算法处理
|
||||
if ec_codes:
|
||||
# 获取本次所有发生事件种类
|
||||
ecs = EventCate.objects.filter(code__in=ec_codes)
|
||||
ecs = EventCate.objects.filter(code__in=ec_codes.keys())
|
||||
obj_cate = 'other'
|
||||
ep = None
|
||||
if 'helmet' in ec_codes:
|
||||
|
|
|
@ -78,7 +78,7 @@ class SpClient:
|
|||
if not settings.SP_ENABLED:
|
||||
raise ParseError('音响对接未启用')
|
||||
self.log = {"requested_at": now(), "id": uuid.uuid4(), "path": url, "method": method,
|
||||
"params": params, "body": json, "target": "dahua", "result": 10}
|
||||
"params": params, "body": json, "target": "speaker", "result": 10}
|
||||
files = None
|
||||
if file_path_rela: # 相对路径
|
||||
files = {'file': open(settings.BASE_DIR + file_path_rela, 'rb')}
|
||||
|
|
|
@ -93,7 +93,7 @@ class XxClient:
|
|||
if ret['errorCode'] != 0:
|
||||
err_detail = dict(detail='寻息错误:' + '|'.join(ret['errorMsg']),
|
||||
code='xx_' + str(ret['errorCode']))
|
||||
self.handle_log(result='fail', response=None)
|
||||
self.handle_log(result='fail', response=ret)
|
||||
if raise_exception:
|
||||
raise ParseError(**err_detail)
|
||||
return 'fail', err_detail
|
||||
|
|
|
@ -74,7 +74,7 @@ class WxClient:
|
|||
timeout=timeout, files=files, verify=False)
|
||||
except Exception:
|
||||
errors = traceback.format_exc()
|
||||
myLogger.error(errors)
|
||||
myLogger.error('微信错误', exc_info=True)
|
||||
self.handle_log(result='error', errors=errors)
|
||||
if raise_exception:
|
||||
raise APIException(**WX_REQUEST_ERROR)
|
||||
|
|
|
@ -49,7 +49,7 @@ class WxmpClient:
|
|||
timeout=timeout, files=files, verify=False)
|
||||
except Exception:
|
||||
errors = traceback.format_exc()
|
||||
myLogger.error(errors)
|
||||
myLogger.error('微信小程序错误', exc_info=True)
|
||||
self.handle_log(result='error', errors=errors)
|
||||
if raise_exception:
|
||||
raise APIException(**WX_REQUEST_ERROR)
|
||||
|
|
|
@ -45,7 +45,7 @@ def run_task(ticket_id: str):
|
|||
f(ticket)
|
||||
except Exception:
|
||||
err_detail = traceback.format_exc()
|
||||
myLogger.error(err_detail)
|
||||
myLogger.error('工作流脚本执行失败', exc_info=True)
|
||||
script_result = False
|
||||
script_result_msg = err_detail
|
||||
ticket = Ticket.objects.filter(id=ticket_id).first()
|
||||
|
|
|
@ -405,5 +405,3 @@ WXMP_APPSECRET = conf.WXMP_APPSECRET
|
|||
WX_ENABLED = conf.WX_ENABLED
|
||||
WX_APPID = conf.WX_APPID
|
||||
WX_APPSECRET = conf.WX_APPSECRET
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue