123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- import base64
- import json
- import logging
- import os
- import threading
- import time
- import apns2
- import boto3
- import jpush
- from boto3.session import Session
- from django.views.generic.base import View
- from pyfcm import FCMNotification
- from AnsjerPush.Config.aiConfig import LABEL_DICT, AI_IDENTIFICATION_TAGS_DICT
- from AnsjerPush.config import APNS_MODE, APNS_CONFIG, BASE_DIR, \
- JPUSH_CONFIG, FCM_CONFIG, ACCESS_KEY_ID, SECRET_ACCESS_KEY, REGION_NAME, PUSH_BUCKET
- from Model.models import UidPushModel, AiService
- from Object.ETkObject import ETkObject
- from Object.MergePic import ImageProcessing
- from Object.ResponseObject import ResponseObject
- from Object.utils import LocalDateTimeUtil
- from Service.CommonService import CommonService
- from Service.EquipmentInfoService import EquipmentInfoService
- class AiView(View):
- def get(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- operation = kwargs.get('operation')
- return self.validation(request.GET, operation)
- def post(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- operation = kwargs.get('operation')
- return self.validation(request.POST, operation)
- def validation(self, request_dict, operation):
- response = ResponseObject()
- if operation == 'identification': # ai识别推送
- return self.identification(request_dict, response)
- else:
- return response.json(414)
- def identification(self, request_dict, response):
- """
- ai识别推送
- @param request_dict: 请求数据
- @request_dict etk: uid token
- @request_dict n_time: 设备的当前时间
- @request_dict channel: 通道
- @request_dict fileOne: 图片一
- @request_dict fileTwo: 图片二
- @request_dict fileThree: 图片三
- @param response: 响应
- @return: response
- """
- etk = request_dict.get('etk', None)
- n_time = request_dict.get('n_time', None)
- channel = request_dict.get('channel', '1')
- file_one = request_dict.get('fileOne', None)
- file_two = request_dict.get('fileTwo', None)
- file_three = request_dict.get('fileThree', None)
- if not all([etk, n_time]):
- return response.json(444)
- # 解密etk并判断uid长度
- eto = ETkObject(etk)
- uid = eto.uid
- logger = logging.getLogger('info')
- logger.info('---进入ai识别推送接口--- etk:{}, uid:{}'.format(etk, uid))
- receive_time = int(time.time())
- file_list = [file_one, file_two, file_three]
- # 查询设备是否有使用中的ai服务
- ai_service_qs = AiService.objects.filter(uid=uid, detect_status=1, use_status=1, endTime__gt=receive_time). \
- values('detect_group')
- if not ai_service_qs.exists():
- return response.json(173)
- detect_group = ai_service_qs[0]['detect_group']
- try:
- dir_path = os.path.join(BASE_DIR, 'static/ai/' + uid + '/' + str(n_time))
- if not os.path.exists(dir_path):
- os.makedirs(dir_path)
- file_path_list = []
- for i, val in enumerate(file_list):
- val = val.replace(' ', '+')
- val = base64.b64decode(val)
- file_path = "{dir_path}/{n_time}_{i}.jpg".format(dir_path=dir_path, n_time=n_time, i=i)
- file_path_list.append(file_path)
- with open(file_path, 'wb') as f:
- f.write(val)
- f.close()
- image_size = 0 # 每张小图片的大小,等于0是按原图大小进行合并
- image_row = 1 # 合并成一张图后,一行有几个小图
- ImageProcessingObj = ImageProcessing(dir_path, image_size, image_row)
- image_info_dict = ImageProcessing.merge_images(ImageProcessingObj)
- photo = open(dir_path + '.jpg', 'rb') # 打开合成图
- # 识别合成图片
- maxLabels = 50 # 最大标签
- minConfidence = 80 # 置信度
- client = boto3.client(
- 'rekognition',
- aws_access_key_id='AKIA2E67UIMD6JD6TN3J',
- aws_secret_access_key='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',
- region_name='us-east-1')
- # doc: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/rekognition.html#Rekognition.Client.detect_labels
- rekognition_res = client.detect_labels(
- Image={'Bytes': photo.read()},
- MaxLabels=maxLabels,
- MinConfidence=minConfidence)
- photo.close()
- if rekognition_res['ResponseMetadata']['HTTPStatusCode'] != 200:
- return response.json(5)
- label_dict = self.handle_rekognition_res(detect_group, rekognition_res, image_info_dict)
- if not label_dict['label_list']:
- # 需要删除图片
- # photo.close()
- # self.del_path(os.path.join(BASE_DIR, 'static/ai/' + uid))
- return response.json(0)
- event_type = label_dict['event_type']
- label_str = ','.join(label_dict['label_list'])
- new_bounding_box_dict = label_dict['new_bounding_box_dict']
- # 上传缩略图到s3
- file_dict = {}
- for i, val in enumerate(file_path_list):
- # 封面图
- file_dict[val] = '{}/{}/{}_{}.jpeg'.format(uid, channel, n_time, i)
- upload_images_thread = threading.Thread(target=self.upload_images, args=(file_dict, dir_path))
- upload_images_thread.start()
- # 存储消息以及推送
- is_st = 3 # 多图
- # 查询推送数据
- uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid). \
- values('token_val', 'app_type', 'appBundleId', 'm_code', 'push_type', 'userID_id',
- 'userID__NickName',
- 'lang', 'm_code', 'tz', 'uid_set__nickname', 'uid_set__detect_interval',
- 'uid_set__detect_group',
- 'uid_set__channel')
- if not uid_push_qs.exists():
- return response.json(173)
- uid_push_list = []
- for qs in uid_push_qs:
- uid_push_list.append(qs)
- nickname = uid_push_list[0]['uid_set__nickname']
- if not nickname:
- nickname = uid
- eq_list = []
- userID_ids = []
- local_date_time = ''
- for up in uid_push_list:
- push_type = up['push_type']
- appBundleId = up['appBundleId']
- token_val = up['token_val']
- lang = up['lang']
- tz = up['tz']
- if tz is None or tz == '':
- tz = 0
- local_date_time = CommonService.get_now_time_str(n_time=n_time, tz=tz, lang='cn')
- logger.info('----AI消息存库{},{},{}'.format(uid, local_date_time, tz))
- local_date_time = local_date_time[0:10]
- # 以下是存库
- userID_id = up["userID_id"]
- if userID_id not in userID_ids:
- now_time = int(time.time())
- eq_list.append(EquipmentInfoService.get_equipment_info_obj(
- local_date_time,
- device_user_id=userID_id,
- event_time=n_time,
- event_type=event_type,
- device_uid=uid,
- device_nick_name=nickname,
- channel=channel,
- alarm='检查到{} \tChannel:{}'.format(label_str, channel),
- is_st=is_st,
- receive_time=receive_time,
- add_time=now_time,
- storage_location=2,
- border_coords=json.dumps(new_bounding_box_dict)
- ))
- userID_ids.append(userID_id)
- # 推送标题
- msg_title = self.get_msg_title(appBundleId=appBundleId, nickname=nickname)
- # 推送内容
- msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz, label_list=label_str)
- kwargs = {
- 'uid': uid,
- 'channel': channel,
- 'event_type': event_type,
- 'n_time': n_time,
- 'appBundleId': appBundleId,
- 'token_val': token_val,
- 'msg_title': msg_title,
- 'msg_text': msg_text,
- }
- try:
- # 推送消息
- if push_type == 0: # ios apns
- self.do_apns(**kwargs)
- elif push_type == 1: # android gcm
- self.do_fcm(**kwargs)
- elif push_type == 2: # android jpush
- self.do_jpush(**kwargs)
- except Exception as e:
- logger.info(
- "errLine={errLine}, errMsg={errMsg}".format(errLine=e.__traceback__.tb_lineno, errMsg=repr(e)))
- continue
- # 分表批量存储
- if eq_list and len(eq_list) > 0:
- logger.info("AI存库中........")
- week = LocalDateTimeUtil.date_to_week(local_date_time)
- result = EquipmentInfoService.equipment_info_bulk_create(week, eq_list)
- logger.info("-.-存库结果{}".format(result))
- return response.json(0)
- except Exception as e:
- print(e)
- data = {
- 'errLine': e.__traceback__.tb_lineno,
- 'errMsg': repr(e)
- }
- return response.json(48, data)
- @staticmethod
- def handle_rekognition_res(detect_group, rekognition_res, image_info_dict):
- """
- 处理识别结果,匹配检测类型,并且返回标签坐标位置信息
- @param detect_group: 检测类型
- @param rekognition_res: 识别响应
- @param image_info_dict: 合成的图片信息
- @return: label_dict
- """
- logger = logging.getLogger('info')
- labels = rekognition_res['Labels']
- logger.info('--------识别到的标签-------:{}'.format(labels))
- label_name = []
- label_list = []
- # 找出识别的所有标签
- for label in labels:
- label_name.append(label['Name'])
- for Parents in label['Parents']:
- label_name.append(Parents['Name'])
- logger.info('------标签名------:{}'.format(label_name))
- # 删除用户没有选择的ai识别类型, 并且得出最终识别结果
- user_detect_list = detect_group.split(',')
- user_detect_list = [i.strip() for i in user_detect_list]
- conform_label_list = []
- conform_detect_group = set()
- for key, label_type_val in LABEL_DICT.items():
- if key in user_detect_list:
- for label in label_type_val:
- if label in label_name:
- conform_detect_group.add(key)
- conform_label_list.append(label)
- # 找出标签边框线位置信息
- bounding_box_list = []
- for label in labels:
- if label['Name'] in conform_label_list:
- for label_instance in label['Instances']:
- bounding_box_list.append(label_instance['BoundingBox'])
- # 找出边框位置信息对应的单图位置并重新计算位置比
- merge_image_height = image_info_dict['height']
- single_height = merge_image_height // image_info_dict['num']
- new_bounding_box_dict = {
- 'file_0': [],
- 'file_1': [],
- 'file_2': []
- }
- for k, val in enumerate(bounding_box_list):
- bounding_box_top = merge_image_height * val['Top']
- # 找出当前边框属于哪张图片范围
- box_dict = {}
- for i in range(image_info_dict['num']):
- top_min = i * single_height
- top_max = (i + 1) * single_height
- if bounding_box_top >= top_min and bounding_box_top <= top_max:
- box_dict['Width'] = val['Width']
- box_dict['Height'] = merge_image_height * val['Height'] / single_height
- # 减去前i张图片的高度
- box_dict['Top'] = ((merge_image_height * val['Top']) - (i * single_height)) / single_height
- box_dict['Left'] = val['Left']
- new_bounding_box_dict['file_{i}'.format(i=i)].append(box_dict)
- # 组织返回数据
- if not conform_detect_group: # 没有识别到符合的标签
- event_type = ''
- label_list = []
- else:
- conform_detect_group = list(conform_detect_group)
- if len(conform_detect_group) > 1:
- conform_detect_group.sort()
- # 集成识别标签
- for label_key in conform_detect_group:
- label_list.append(AI_IDENTIFICATION_TAGS_DICT[label_key])
- event_type = ''.join(conform_detect_group) # 组合类型
- else:
- label_list.append(AI_IDENTIFICATION_TAGS_DICT[conform_detect_group[0]])
- event_type = conform_detect_group[0]
- logger.info('------conform_detect_group------ {}'.format(conform_detect_group))
- label_dict = {
- 'event_type': event_type,
- 'label_list': label_list,
- 'new_bounding_box_dict': new_bounding_box_dict
- }
- logger.info('------label_dict------ {}'.format(label_dict))
- return label_dict
- @staticmethod
- def upload_images(file_dict, dir_path):
- """
- 上传图片
- @param file_dict: S3图片路径
- @param dir_path: 本地图片路径
- @return: boolean
- """
- try:
- s3 = Session(
- aws_access_key_id=ACCESS_KEY_ID,
- aws_secret_access_key=SECRET_ACCESS_KEY,
- region_name=REGION_NAME
- ).resource('s3')
- for file_path, upload_path in file_dict.items():
- upload_data = open(file_path, 'rb')
- s3.Bucket(PUSH_BUCKET).put_object(Key=upload_path, Body=upload_data)
- # 删除图片
- CommonService.del_path(dir_path)
- CommonService.del_path(dir_path + '.jpg')
- return True
- except Exception as e:
- print(repr(e))
- return False
- def get_msg_title(self, appBundleId, nickname):
- package_title_config = {
- 'com.ansjer.customizedd_a': 'DVS',
- 'com.ansjer.zccloud_a': 'ZosiSmart',
- 'com.ansjer.zccloud_ab': '周视',
- 'com.ansjer.adcloud_a': 'ADCloud',
- 'com.ansjer.adcloud_ab': 'ADCloud',
- 'com.ansjer.accloud_a': 'ACCloud',
- 'com.ansjer.loocamccloud_a': 'Loocam',
- 'com.ansjer.loocamdcloud_a': 'Anlapus',
- 'com.ansjer.customizedb_a': 'COCOONHD',
- 'com.ansjer.customizeda_a': 'Guardian365',
- 'com.ansjer.customizedc_a': 'PatrolSecure',
- }
- if appBundleId in package_title_config.keys():
- return package_title_config[appBundleId] + '(' + nickname + ')'
- else:
- return nickname
- def get_msg_text(self, channel, n_time, lang, tz, label_list):
- n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz, lang=lang)
- if lang == 'cn':
- msg = '摄像头AI识别到了{}'.format(label_list)
- send_text = '{msg} 通道:{channel} 日期:{date}'.format(msg=msg, channel=channel, date=n_date)
- else:
- msg = 'Camera AI recognizes {}'.format(label_list)
- send_text = '{msg} channel:{channel} date:{date}'.format(msg=msg, channel=channel, date=n_date)
- return send_text
- def do_jpush(self, uid, channel, appBundleId, token_val, event_type, n_time, msg_title, msg_text):
- app_key = JPUSH_CONFIG[appBundleId]['Key']
- master_secret = JPUSH_CONFIG[appBundleId]['Secret']
- # 此处换成各自的app_key和master_secre
- _jpush = jpush.JPush(app_key, master_secret)
- push = _jpush.create_push()
- push.audience = jpush.registration_id(token_val)
- push_data = {"alert": "Motion ", "event_time": n_time, "event_type": event_type, "msg": "",
- "received_at": n_time, "sound": "sound.aif", "uid": uid, "zpush": "1", "channel": channel}
- android = jpush.android(alert=msg_text, priority=1, style=1, alert_type=7,
- big_text=msg_text, title=msg_title,
- extras=push_data)
- push.notification = jpush.notification(android=android)
- push.platform = jpush.all_
- res = push.send()
- print(res)
- return res.status_code
- def do_fcm(self, uid, channel, appBundleId, token_val, event_type, n_time, msg_title, msg_text):
- try:
- serverKey = FCM_CONFIG[appBundleId]
- push_service = FCMNotification(api_key=serverKey)
- data = {"alert": "Motion ", "event_time": n_time, "event_type": event_type, "msg": "",
- "received_at": n_time, "sound": "sound.aif", "uid": uid, "zpush": "1", "channel": channel}
- result = push_service.notify_single_device(registration_id=token_val, message_title=msg_title,
- message_body=msg_text, data_message=data,
- extra_kwargs={
- 'default_vibrate_timings': True,
- 'default_sound': True,
- 'default_light_settings': True
- })
- print('fcm push ing')
- print(result)
- return result
- except Exception as e:
- return 'serverKey abnormal'
- def do_apns(self, uid, channel, appBundleId, token_val, event_type, n_time, msg_title, msg_text):
- logger = logging.getLogger('info')
- logger.info("进来do_apns函数了")
- logger.info(token_val)
- logger.info(APNS_MODE)
- logger.info(os.path.join(BASE_DIR, APNS_CONFIG[appBundleId]['pem_path']))
- try:
- cli = apns2.APNSClient(mode=APNS_MODE,
- client_cert=os.path.join(BASE_DIR, APNS_CONFIG[appBundleId]['pem_path']))
- push_data = {"alert": "Motion ", "event_time": n_time, "event_type": event_type, "msg": "",
- "received_at": n_time, "sound": "", "uid": uid, "zpush": "1", "channel": channel}
- alert = apns2.PayloadAlert(body=msg_text, title=msg_title)
- payload = apns2.Payload(alert=alert, custom=push_data, sound="default")
- n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW)
- res = cli.push(n=n, device_token=token_val, topic=appBundleId)
- if res.status_code == 200:
- return res.status_code
- else:
- logger.info('apns push fail')
- logger.info(res.reason)
- return res.status_code
- except (ValueError, ArithmeticError):
- return 'The program has a numeric format exception, one of the arithmetic exceptions'
- except Exception as e:
- print(repr(e))
- logger.info(repr(e))
- return repr(e)
|