#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved. @software: PyCharm @Version: python3.6 @MODIFY DECORD:ansjer dev """ 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 import SERVER_TYPE from AnsjerPush.config import AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, APNS_MODE, APNS_CONFIG, BASE_DIR, \ JPUSH_CONFIG, FCM_CONFIG from Model.models import UidPushModel, AiService from Object import MergePic from Object.ETkObject import ETkObject from Object.ResponseObject import ResponseObject from Object.TokenObject import TokenObject from Object.utils import LocalDateTimeUtil from Service.CommonService import CommonService from Service.EquipmentInfoService import EquipmentInfoService # AI服务 class AiView(View): def get(self, request, *args, **kwargs): request.encoding = 'utf-8' operation = kwargs.get('operation') return self.validation(request.GET, request, operation) def post(self, request, *args, **kwargs): request.encoding = 'utf-8' operation = kwargs.get('operation') return self.validation(request.POST, request, operation) def validation(self, request_dict, request, operation): response = ResponseObject() if operation is None: return response.json(444, 'error path') elif operation == 'identification': # ai识别 return self.do_ai_identification(request.POST, response) else: token = request_dict.get('token', None) # 设备主键uid tko = TokenObject(token) response.lang = tko.lang if tko.code != 0: return response.json(tko.code) userID = tko.userID if operation == 'identification': # ai识别 return self.do_ai_identification(request_dict, response) else: return response.json(414) def do_ai_identification(self, request_dict, response): etk = request_dict.get('etk', None) n_time = request_dict.get('n_time', None) channel = request_dict.get('channel', '1') receiveTime = int(time.time()) logger = logging.getLogger('info') logger.info('进入-----------into----ai--api') logger.info("etk={etk}".format(etk=etk)) if not etk: return response.json(444) try: # 解密uid及判断长度 eto = ETkObject(etk) uid = eto.uid logger.info("uid={uid}".format(uid=uid)) if len(uid) != 20 and len(uid) != 14: return response.json(444) logger.info('uid={uid}'.format(uid=uid)) logger.info('n_time={n_time}'.format(n_time=n_time)) ##通过uid查出endTime是否过期,并且ai开关是否打开 AiServiceQuery = AiService.objects.filter(uid=uid, detect_status=1, use_status=1, endTime__gt=receiveTime). \ values('detect_group') if not AiServiceQuery.exists(): logger.info('none-----aiService') return response.json(173) detect_group = AiServiceQuery[0]['detect_group'] # {}?? # file_post_one = request_dict.get('fileOne', None) file_post_two = request_dict.get('fileTwo', None) file_post_three = request_dict.get('fileThree', None) file_list = [file_post_one, file_post_two, file_post_three] del file_post_one, file_post_two, file_post_three if not all(file_list): for k, val in enumerate(file_list): if not val: return response.json(444, '缺少第{k}张图'.format(k=k + 1)) 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_colnum = 1 # 合并成一张图后,一行有几个小图 image_size = MergePic.merge_images(dir_path, image_size, image_colnum) photo = open(dir_path + '.jpg', 'rb') # 打开合成图 # photo = open(r'E:\test---------------\test\snipaste20220121_215952.jpg', 'rb') # 识别合成图片 maxLabels = 50 # 最大标签 minConfidence = 70 # 置信度 ai_start_time = int(time.time()) client = boto3.client( 'rekognition', aws_access_key_id='AKIA2E67UIMD6JD6TN3J', aws_secret_access_key='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', region_name='us-east-1') # doc: rekognition_res = client.detect_labels( Image={'Bytes': photo.read()}, MaxLabels=maxLabels, MinConfidence=minConfidence) photo.close() if rekognition_res['ResponseMetadata']['HTTPStatusCode'] != 200: return response.json(173) # rekognition_res = '{"Labels":[{"Name":"Person","Confidence":99.55254364013672,"Instances":[{"BoundingBox":{"Width":0.07776174694299698,"Height":0.13592061400413513,"Left":0.38370513916015625,"Top":0.09075711667537689},"Confidence":99.55254364013672},{"BoundingBox":{"Width":0.10947742313146591,"Height":0.12066027522087097,"Left":0.2790755331516266,"Top":0.10242735594511032},"Confidence":99.54237365722656},{"BoundingBox":{"Width":0.2935298979282379,"Height":0.09244367480278015,"Left":0.6143953204154968,"Top":0.9052517414093018},"Confidence":98.82627868652344},{"BoundingBox":{"Width":0.35492533445358276,"Height":0.21574528515338898,"Left":0.3411630690097809,"Top":0.27151572704315186},"Confidence":96.74708557128906},{"BoundingBox":{"Width":0.39604419469833374,"Height":0.09648437798023224,"Left":0.060247838497161865,"Top":0.9000436663627625},"Confidence":95.03588104248047},{"BoundingBox":{"Width":0.1105344295501709,"Height":0.1257047802209854,"Left":0.0025259912945330143,"Top":0.8314586877822876},"Confidence":92.17312622070312},{"BoundingBox":{"Width":0.13166509568691254,"Height":0.12054375559091568,"Left":0.10105808824300766,"Top":0.8364697694778442},"Confidence":89.71287536621094},{"BoundingBox":{"Width":0.22752150893211365,"Height":0.09563954919576645,"Left":0.7430258989334106,"Top":0.8961490392684937},"Confidence":85.34542083740234},{"BoundingBox":{"Width":0.1297324150800705,"Height":0.096779465675354,"Left":0.4607183039188385,"Top":0.9025260806083679},"Confidence":83.4525375366211}],"Parents":[]},{"Name":"Human","Confidence":99.55254364013672,"Instances":[],"Parents":[]},{"Name":"Husky","Confidence":98.64888763427734,"Instances":[],"Parents":[{"Name":"Dog"},{"Name":"Pet"},{"Name":"Canine"},{"Name":"Animal"},{"Name":"Mammal"}]},{"Name":"Dog","Confidence":98.64888763427734,"Instances":[{"BoundingBox":{"Width":0.2157023847103119,"Height":0.2352331429719925,"Left":0.26413947343826294,"Top":0.5162186622619629},"Confidence":94.06558990478516},{"BoundingBox":{"Width":0.23323440551757812,"Height":0.2026243358850479,"Left":0.4748744070529938,"Top":0.5457579493522644},"Confidence":92.4560546875},{"BoundingBox":{"Width":0.31273096799850464,"Height":0.23413777351379395,"Left":0.6696768999099731,"Top":0.5142407417297363},"Confidence":91.39192199707031},{"BoundingBox":{"Width":0.27037277817726135,"Height":0.21958686411380768,"Left":0.053227268159389496,"Top":0.5305629968643188},"Confidence":90.0697250366211}],"Parents":[{"Name":"Pet"},{"Name":"Canine"},{"Name":"Animal"},{"Name":"Mammal"}]},{"Name":"Canine","Confidence":98.64888763427734,"Instances":[],"Parents":[{"Name":"Mammal"},{"Name":"Animal"}]},{"Name":"Pet","Confidence":98.64888763427734,"Instances":[],"Parents":[{"Name":"Animal"}]},{"Name":"Mammal","Confidence":98.64888763427734,"Instances":[],"Parents":[{"Name":"Animal"}]},{"Name":"Animal","Confidence":98.64888763427734,"Instances":[],"Parents":[]},{"Name":"Car","Confidence":96.32373809814453,"Instances":[{"BoundingBox":{"Width":0.5476366281509399,"Height":0.10842914879322052,"Left":0.38325461745262146,"Top":0.11447073519229889},"Confidence":96.32373809814453}],"Parents":[{"Name":"Vehicle"},{"Name":"Transportation"}]},{"Name":"Transportation","Confidence":96.32373809814453,"Instances":[],"Parents":[]},{"Name":"Vehicle","Confidence":96.32373809814453,"Instances":[],"Parents":[{"Name":"Transportation"}]},{"Name":"Automobile","Confidence":96.32373809814453,"Instances":[],"Parents":[{"Name":"Vehicle"},{"Name":"Transportation"}]},{"Name":"Clothing","Confidence":86.02703857421875,"Instances":[],"Parents":[]},{"Name":"Apparel","Confidence":86.02703857421875,"Instances":[],"Parents":[]},{"Name":"Puppy","Confidence":85.16007232666016,"Instances":[],"Parents":[{"Name":"Dog"},{"Name":"Pet"},{"Name":"Canine"},{"Name":"Animal"},{"Name":"Mammal"}]},{"Name":"Wheel","Confidence":74.5079116821289,"Instances":[{"BoundingBox":{"Width":0.0818198174238205,"Height":0.052956655621528625,"Left":0.4488227367401123,"Top":0.17395207285881042},"Confidence":74.5079116821289},{"BoundingBox":{"Width":0.09288611263036728,"Height":0.050751496106386185,"Left":0.7844013571739197,"Top":0.17587198317050934},"Confidence":51.4537353515625}],"Parents":[{"Name":"Machine"}]},{"Name":"Machine","Confidence":74.5079116821289,"Instances":[],"Parents":[]}],"LabelModelVersion":"2.0","ResponseMetadata":{"RequestId":"1088325e-996b-4982-9afb-bd95df6d6fb3","HTTPStatusCode":200,"HTTPHeaders":{"x-amzn-requestid":"1088325e-996b-4982-9afb-bd95df6d6fb3","content-type":"application/x-amz-json-1.1","content-length":"4291","date":"Tue, 15 Mar 2022 06:44:17 GMT"},"RetryAttempts":0}}' # rekognition_res = json.loads(rekognition_res) ai_end_time = int(time.time()) labels = self.labelsCoords(detect_group, rekognition_res, image_size, n_time) # 检查标签是否符合用户选择的识别类型 logger.info('-----记录返回labels') logger.info('labels') # return response.json(0,labels) if len(labels['label_list']) == 0: # 需要删除图片 # photo.close() # self.del_path(os.path.join(BASE_DIR, 'static/ai/' + uid)) logger.info('没有识别到任何标签-----------------') return response.json(10055) eventType = labels['eventType'] label_str = ','.join(labels['label_list']) new_bounding_box_dict = labels['new_bounding_box_dict'] logger.info(eventType) logger.info(label_str) # 上传缩略图到s3 file_dict = {} for i, val in enumerate(file_path_list): file_dict[val] = "{uid}/{channel}/{n_time}_{i}.jpeg".format(uid=uid, channel=channel, # 封面图 n_time=n_time, i=i) thread_task = threading.Thread(target=self.upload_s3, args=(file_dict, dir_path)) thread_task.start() # 存储消息以及推送 is_st = 4 # ai多图 # 查询推送数据 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 = [] apns_start_time = 0 apns_end_time = 0 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=eventType, device_uid=uid, device_nick_name=nickname, channel=channel, alarm='检查到{labels} \tChannel:{channel}'.format(labels=label_str, channel=channel), is_st=is_st, receive_time=receiveTime, 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': eventType, '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 res = self.do_apns(**kwargs) elif push_type == 1: # android gcm self.do_fcm(**kwargs) elif push_type == 2: # android jpush self.do_jpush(**kwargs) # if push_type == 1: # android gcm # logger.info('into-------gcm') # apns_start_time = int(time.time()) # res = self.do_fcm(**kwargs) # apns_end_time = int(time.time()) 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) def del_path(self, path): if not os.path.exists(path): return if os.path.isfile(path): os.remove(path) else: items = os.listdir(path) for f in items: c_path = os.path.join(path, f) if os.path.isdir(c_path): self.del_path(c_path) else: os.remove(c_path) os.rmdir(path) ## 检查是否有符合条件的标签,并且返回标签坐标位置信息 def labelsCoords(self, user_detect_group, rekognition_res, image_size, n_time): logger = logging.getLogger('info') labels = rekognition_res['Labels'] label_name = [] logger.info('--------识别到的标签-------') logger.info(labels) all_labels_type = { '1': ['Person', 'Human'], # 人 '2': ['Dog', 'Pet', 'Canine', 'Animal', 'Puppy', 'Cat'], # 动物 '3': ['Car', 'Vehicle', 'Transportation', 'Automobile', 'Bus'], # 车 '4': ['Carton', 'Cardboard', 'Package Delivery'] # 包裹 } # 找出识别的所有标签 for label in labels: label_name.append(label['Name']) for Parents in label['Parents']: label_name.append(Parents['Name']) logger.info('标签名------') logger.info(label_name) # 删除用户没有选择的ai识别类型, 并且得出最终识别结果 user_detect_list = user_detect_group.split(',') user_detect_list = [i.strip() for i in user_detect_list] conform_label_list = [] conform_user_d_group = set() for key, label_type_val in all_labels_type.items(): if key in user_detect_list: for label in label_type_val: if label in label_name: conform_user_d_group.add(key) conform_label_list.append(label) # 找出标签边框线位置信息 boundingBoxList = [] for label in labels: if label['Name'] in conform_label_list: for boundingBox in label['Instances']: boundingBoxList.append(boundingBox['BoundingBox']) # 找出边框位置信息对应的单图位置并重新计算位置比 merge_image_height = image_size['height'] # merge_image_width = image_size['width'] single_height = merge_image_height // image_size['num'] new_bounding_box_dict = {} new_bounding_box_dict['file_0'] = [] new_bounding_box_dict['file_1'] = [] new_bounding_box_dict['file_2'] = [] # new_bounding_box_dict['file_3'] = [] for k, val in enumerate(boundingBoxList): boundingBoxTop = merge_image_height * val['Top'] # 找出当前边框属于哪张图片范围 boxDict = {} for i in range(image_size['num']): min = i * single_height # 第n张图 max = (i + 1) * single_height if boundingBoxTop >= min and boundingBoxTop <= max: # print("属于第{i}张图".format(i=i+1)) boxDict['Width'] = val['Width'] boxDict['Height'] = merge_image_height * val['Height'] / single_height boxDict['Top'] = ((merge_image_height * val['Top']) - ( i * single_height)) / single_height # 减去前i张图片的高度 boxDict['Left'] = val['Left'] new_bounding_box_dict["file_{i}".format(i=i)].append(boxDict) # exit(new_bounding_box_list) conform_user_d_group = list(conform_user_d_group) if len(conform_user_d_group) > 1: conform_user_d_group.sort() eventType = ''.join(conform_user_d_group) # 组合类型 else: eventType = conform_user_d_group[0] return {'eventType': eventType, 'label_list': conform_label_list, 'new_bounding_box_dict': new_bounding_box_dict} def upload_s3(self, file_dict, dir_path): try: if SERVER_TYPE == "Ansjer.formal_settings": #存国外 aws_key = AWS_ACCESS_KEY_ID[1] aws_secret = AWS_SECRET_ACCESS_KEY[1] session = Session(aws_access_key_id=aws_key, aws_secret_access_key=aws_secret, region_name="us-east-1") s3 = session.resource("s3") bucket = "foreignpush" else: #存国内 aws_key = AWS_ACCESS_KEY_ID[0] aws_secret = AWS_SECRET_ACCESS_KEY[0] session = Session(aws_access_key_id=aws_key, aws_secret_access_key=aws_secret, region_name="cn-northwest-1") s3 = session.resource("s3") bucket = "push" for file_path, upload_path in file_dict.items(): upload_data = open(file_path, "rb") # upload_key = "test" s3.Bucket(bucket).put_object(Key=upload_path, Body=upload_data) # 需要删除图片 # self.del_path(dir_path) # self.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)