|
@@ -38,8 +38,23 @@ from Object.enums.MessageTypeEnum import MessageTypeEnum
|
|
|
from Service.CommonService import CommonService
|
|
|
from Service.DevicePushService import DevicePushService
|
|
|
from Service.EquipmentInfoService import EquipmentInfoService
|
|
|
+from Object.NovaImageTagObject import NovaImageTagObject
|
|
|
+from datetime import datetime
|
|
|
|
|
|
TIME_LOGGER = logging.getLogger('time')
|
|
|
+# 1. 声明一个全局变量,用于存放创建好的实例
|
|
|
+nova_image_tag_instance = None
|
|
|
+
|
|
|
+# 2. 根据条件,创建实例并赋值给这个全局变量
|
|
|
+# 这段代码会放在文件的最顶部,它只在 Gunicorn worker 进程启动时执行一次
|
|
|
+if CONFIG_INFO == CONFIG_EUR:
|
|
|
+ nova_image_tag_instance = NovaImageTagObject(
|
|
|
+ AWS_ACCESS_KEY_ID[1], AWS_SECRET_ACCESS_KEY[1], 'eu-west-2'
|
|
|
+ )
|
|
|
+else:
|
|
|
+ nova_image_tag_instance = NovaImageTagObject(
|
|
|
+ AWS_ACCESS_KEY_ID[1], AWS_SECRET_ACCESS_KEY[1], 'us-east-1'
|
|
|
+ )
|
|
|
|
|
|
|
|
|
# AI服务
|
|
@@ -110,7 +125,11 @@ class AiView(View):
|
|
|
for k, val in enumerate(file_list):
|
|
|
if not val:
|
|
|
return response.json(444, '缺少第{k}张图'.format(k=k + 1))
|
|
|
-
|
|
|
+ redis_obj = RedisObject(db=6)
|
|
|
+ ai_key = f'PUSH:AI:{uid}:{channel}'
|
|
|
+ ai_data = redis_obj.get_data(ai_key)
|
|
|
+ if ai_data:
|
|
|
+ return response.json(0, {'msg': 'Push again in one minute'})
|
|
|
# 查询推送数据
|
|
|
uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid). \
|
|
|
values('token_val', 'app_type', 'appBundleId', 'm_code', 'push_type', 'userID_id',
|
|
@@ -129,7 +148,6 @@ class AiView(View):
|
|
|
if (now_time - add_time) <= (3600 * 24 * 3):
|
|
|
ai_server = 'rekognition'
|
|
|
|
|
|
- redis_obj = RedisObject(db=6)
|
|
|
APP_NOTIFY_KEY = f'ASJ:NOTIFY:PUSH:{uid}:{channel}' # 推送间隔缓存KEY
|
|
|
push_cache_data = redis_obj.get_data(APP_NOTIFY_KEY)
|
|
|
is_push = False if push_cache_data else True
|
|
@@ -138,7 +156,22 @@ class AiView(View):
|
|
|
|
|
|
# APP推送提醒状态
|
|
|
notify = self.is_ai_push(uid, notify_data) if is_push else is_push
|
|
|
+ nova_key = f'PUSH:NOVA:LITE:{uid}'
|
|
|
+ nova = redis_obj.get_data(nova_key)
|
|
|
+ if nova: # AWS AI模型
|
|
|
+ sage_maker = SageMakerAiObject()
|
|
|
|
|
|
+ # AI nova识别异步存表&推送
|
|
|
+ push_thread = threading.Thread(
|
|
|
+ target=self.async_detection_image_label,
|
|
|
+ kwargs={'sage_maker': sage_maker, 'uid': uid, 'n_time': n_time, 'uid_push_qs': uid_push_qs,
|
|
|
+ 'channel': channel, 'file_list': file_list, 'notify': notify, 'detect_group': detect_group})
|
|
|
+ push_thread.start()
|
|
|
+
|
|
|
+ self.add_push_cache(APP_NOTIFY_KEY, redis_obj, push_cache_data,
|
|
|
+ uid_push_qs[0]['uid_set__new_detect_interval'])
|
|
|
+ redis_obj.set_data(ai_key, uid, 60)
|
|
|
+ return response.json(0)
|
|
|
if ai_server == 'sageMaker': # 自建模型sageMaker AI
|
|
|
sage_maker = SageMakerAiObject()
|
|
|
ai_result = sage_maker.sage_maker_ai_server(uid, file_list) # 图片base64识别AI标签
|
|
@@ -155,6 +188,7 @@ class AiView(View):
|
|
|
push_thread.start() # AI识别异步存表&推送
|
|
|
self.add_push_cache(APP_NOTIFY_KEY, redis_obj, push_cache_data,
|
|
|
uid_push_qs[0]['uid_set__new_detect_interval'])
|
|
|
+ redis_obj.set_data(ai_key, uid, 60)
|
|
|
return response.json(0)
|
|
|
TIME_LOGGER.info(f'uid={uid},sagemakerAI识别失败{ai_result}')
|
|
|
|
|
@@ -164,7 +198,7 @@ class AiView(View):
|
|
|
'uid_push_qs': uid_push_qs,
|
|
|
'channel': channel})
|
|
|
push_thread.start() # AI识别异步存表&推送
|
|
|
-
|
|
|
+ redis_obj.set_data(ai_key, uid, 60)
|
|
|
return response.json(0)
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
@@ -590,7 +624,7 @@ class AiView(View):
|
|
|
_jpush = jpush.JPush(app_key, master_secret)
|
|
|
push = _jpush.create_push()
|
|
|
push.audience = jpush.registration_id(token_val)
|
|
|
- push_data = {"alert": msg_text, "event_time": n_time, "event_type": event_type, "msg": "",
|
|
|
+ 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,
|
|
@@ -606,7 +640,7 @@ class AiView(View):
|
|
|
|
|
|
serverKey = FCM_CONFIG[appBundleId]
|
|
|
push_service = FCMNotification(api_key=serverKey)
|
|
|
- data = {"alert": msg_text, "event_time": n_time, "event_type": event_type, "msg": "",
|
|
|
+ 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,
|
|
@@ -630,7 +664,7 @@ class AiView(View):
|
|
|
try:
|
|
|
cli = apns2.APNSClient(mode=APNS_MODE,
|
|
|
client_cert=os.path.join(BASE_DIR, APNS_CONFIG[appBundleId]['pem_path']))
|
|
|
- push_data = {"alert": msg_text, "event_time": n_time, "event_type": event_type, "msg": "",
|
|
|
+ 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")
|
|
@@ -710,3 +744,45 @@ class AiView(View):
|
|
|
except Exception as e:
|
|
|
logger.info('{}识别后存S3与DynamoDB失败:{}'.format(uid, repr(e)))
|
|
|
return False
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def async_detection_image_label(sage_maker,uid, n_time, uid_push_qs,
|
|
|
+ channel, file_list, notify,detect_group):
|
|
|
+ final_results = AiView.get_nova_tag_recognition(file_list, uid)
|
|
|
+ if not final_results:
|
|
|
+ return
|
|
|
+ res = sage_maker.get_table_name(uid, final_results, detect_group)
|
|
|
+ sage_maker.save_push_message(uid, n_time, uid_push_qs, channel, res, file_list, notify)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def get_nova_tag_recognition(base64_images, uid):
|
|
|
+ TIME_LOGGER.info(f'uid:{uid} Nova标签识别开始')
|
|
|
+ try:
|
|
|
+ redis_obj = RedisObject(db=6)
|
|
|
+ now = datetime.now()
|
|
|
+ year_month_day = now.strftime("%Y%m%d") # 如20241005(单日标识)
|
|
|
+ daily_total_key = f"api:recognition:nova:daily:{year_month_day}:total"
|
|
|
+ # 使用incr命令保证原子操作
|
|
|
+ redis_obj.incr(daily_total_key, 1, 3600 * 24 * 7)
|
|
|
+ except Exception as e:
|
|
|
+ # Redis计数失败不影响主业务(降级处理),仅打日志
|
|
|
+ TIME_LOGGER.error(f"{uid}Redis统计接口访问量失败:{repr(e)}")
|
|
|
+ # 定义要检测的类别
|
|
|
+ categories_to_detect = ["person", "car", "pet", "package"]
|
|
|
+ # --- 记录process_image_batch执行时间 ---
|
|
|
+ start_time = time.perf_counter() # 高精度计时
|
|
|
+ final_results = nova_image_tag_instance.process_image_batch(
|
|
|
+ base64_images,
|
|
|
+ categories_to_detect,
|
|
|
+ uid
|
|
|
+ )
|
|
|
+ # 无论是否发生异常,都记录执行时间
|
|
|
+ end_time = time.perf_counter()
|
|
|
+ execution_time = end_time - start_time
|
|
|
+ TIME_LOGGER.info(
|
|
|
+ f'uid:{uid} Nova标签识别批量处理完成 | '
|
|
|
+ f'执行时间: {execution_time:.4f}秒'
|
|
|
+ )
|
|
|
+ return final_results
|