def write_database(): """ 每1秒更新一次数据库表PoseInfo的记录信息 :return: """ pose_url = Conf.Urls.PoseInfoUrl + '/UpdateOrCreatePoseInfo' for aged in ages.values(): temp_pose_info = PoseInfo(agesInfoId=aged.agesinfoid, date=aged.date, dateTime=aged.datetime, timeStand=int(float(aged.timestand)), timeSit=int(float(aged.timesit)), timeLie=int(float(aged.timelie)), timeDown=int(float(aged.timedown)), timeOther=int(float(aged.timeother)), isAlarm=aged.isalarm, status=aged.status) http_result = HttpHelper.create_item(pose_url, temp_pose_info) scheduler.enter(1, 0, write_database, ())
# 获取本机需要识别的摄像头视频地址信息 local_ip = '192.168.199.224' local_ip = '192.168.1.60' # 拼接url,参考接口文档 get_current_server_url = Conf.Urls.ServerInfoUrl + "/GetServerInfo?ip=" + local_ip print(get_current_server_url) current_server = HttpHelper.get_items(get_current_server_url) print(current_server) for camera in current_server.cameraInfos: # 遍历本服务器需要处理的摄像头 print(f'摄像头视频地址:{camera.videoAddress}') for aged in camera.roomInfo.agesInfos: # 遍历本摄像头所在房间的老人信息 print(f'被监控人:{aged.name}') ages[aged.id] = PoseInfo(agesInfoId=aged.id, date=time.strftime('%Y-%m-%dT00:00:00', time.localtime()), timeStand=0, timeSit=0, timeLie=0, timeDown=0, timeOther=0) # 读取摄像头视频内容,进行pose识别,然后更新各种状态的时间值 pose_detect_with_video(camera.videoAddress, aged.id) # 创建或更新PoseInfo数据库记录 pose_url = Conf.Urls.PoseInfoUrl + '/UpdateOrCreatePoseInfo' HttpHelper.create_item(pose_url, ages[aged.id])
def pose_detect_with_video(aged_id, classidx, human_box, parse_pose_demo_instance): use_aged = ages[aged_id] classidx = int(classidx) # detect if a new day come now_date = time.strftime('%Y-%m-%dT00:00:00', time.localtime()) if not now_date == use_aged.date: # a new day aged_status_reset(use_aged) parse_pose_demo_instance.is_first_frame = True use_aged.date = now_date if parse_pose_demo_instance.is_first_frame: # 第一帧,开始计时 # 从服务器获取当天的状态记录信息,进行本地值的更新,防止状态计时被重置 aged_status_sync(use_aged) parse_pose_demo_instance.is_first_frame = False else: last_pose_time = time.time( ) - parse_pose_demo_instance.last_time # 上一个状态至今的时间差,单位为s if use_aged.status == PoseStatus.Sit.value: use_aged.timesit += last_pose_time elif use_aged.status == PoseStatus.Down.value: use_aged.timedown += last_pose_time elif use_aged.status == PoseStatus.Lie.value: use_aged.timelie += last_pose_time elif use_aged.status == PoseStatus.Stand.value: use_aged.timestand += last_pose_time else: use_aged.timeother += last_pose_time parse_pose_demo_instance.last_time = time.time() if classidx == 0: now_status = PoseStatus.Sit.value elif classidx == 1: now_status = PoseStatus.Lie.value elif classidx == 2: now_status = PoseStatus.Stand.value elif classidx == 3: now_status = PoseStatus.Lie.value else: now_status = PoseStatus.Other.value now_date_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) if not now_status == use_aged.status: # 新的行为发生 if not now_date_time == use_aged.datetime: # 根据实际情况,每秒只记录一次状态更改操作 temp_detail_pose_info = DetailPoseInfo(agesInfoId=aged_id, dateTime=now_date_time, status=now_status) # 写数据记录到数据库表DetaiPoseInfo detail_pose_url = Conf.Urls.DetailPoseInfoUrl http_result = HttpHelper.create_item(detail_pose_url, temp_detail_pose_info) use_aged.datetime = now_date_time if parse_pose_demo_instance.camera_info.isUseSafeRegion: is_outer_chuang = False # 因为床的矩形坐标是在原图压缩1/2之后的值,所以下面的值也需要压缩1/2 xmin, ymin, xmax, ymax = int(human_box[0] / 2), int( human_box[1] / 2), int(human_box[2] / 2), int(human_box[3] / 2) if xmin > parse_pose_demo_instance.camera_info.rightBottomPointX \ or ymin > parse_pose_demo_instance.camera_info.rightBottomPointY\ or xmax < parse_pose_demo_instance.camera_info.leftTopPointX\ or ymax < parse_pose_demo_instance.camera_info.leftTopPointY: is_outer_chuang = True use_aged.isalarm = False # 判断当前状态是否需求报警 if is_outer_chuang: if now_status == PoseStatus.Lie.value: use_aged.isalarm = True now_status = PoseStatus.Down.value else: if now_status == PoseStatus.Down.value: # TODO:这里的给值是不对的,需要赋予识别服务的对应的需要报警的状态值 use_aged.isalarm = True use_aged.status = now_status
def parse(self): if not os.path.exists(self.output_path): os.mkdir(self.output_path) data_loader = WebcamLoader(self.camera_info.videoAddress).start() (fourcc, fps, frameSize) = data_loader.videoinfo() sys.stdout.flush() det_loader = DetectionLoader(data_loader, batchSize=self.detbatch).start() det_processor = DetectionProcessor(det_loader).start() aligner = AlignPoints() # Data writer # save_path = os.path.join(args.outputpath, 'AlphaPose_webcam' + webcam + '.avi') writer = DataWriter(self.save_video, self.output_path, cv2.VideoWriter_fourcc(*'XVID'), fps, frameSize, pos_reg_model=pos_reg_model, aligner=aligner).start() # 统计时间使用 runtime_profile = {'dt': [], 'pt': [], 'pn': []} sys.stdout.flush() batch_size = self.detbatch while True: try: start_time = getTime() with torch.no_grad(): (inps, orig_img, im_name, boxes, scores, pt1, pt2) = det_processor.read() if boxes is None or boxes.nelement() == 0: writer.save(None, None, None, None, None, orig_img, im_name.split('/')[-1]) continue ckpt_time, det_time = getTime(start_time) runtime_profile['dt'].append(det_time) # Pose Estimation datalen = inps.size(0) leftover = 0 if (datalen) % batch_size: leftover = 1 num_batches = datalen // batch_size + leftover hm = [] for j in range(num_batches): inps_j = inps[j * batch_size:min( (j + 1) * batch_size, datalen)].cuda() hm_j = pose_model(inps_j) hm.append(hm_j) hm = torch.cat(hm) ckpt_time, pose_time = getTime(ckpt_time) hm = hm.cpu().data writer.save(boxes, scores, hm, pt1, pt2, orig_img, im_name.split('/')[-1]) while not writer.result_Q.empty(): boxes, classidx = writer.result_Q.get() print('classidx:', classidx) for aged in self.camera.roomInfo.agesInfos: # 遍历本摄像头所在房间的老人信息,目前只考虑房间只有一个人 if not aged.id in ages.keys(): ages[aged.id] = PoseInfo( agesInfoId=aged.id, date=time.strftime('%Y-%m-%dT00:00:00', time.localtime()), timeStand=0, timeSit=0, timeLie=0, timeDown=0, timeOther=0) # 更新被监护对象各种状态的时间值 pose_detect_with_video(aged.id, classidx) break # 创建或更新PoseInfo数据库记录 pose_url = Conf.Urls.PoseInfoUrl + '/UpdateOrCreatePoseInfo' HttpHelper.create_item(pose_url, ages[aged.id]) ckpt_time, post_time = getTime(ckpt_time) except KeyboardInterrupt: break while (writer.running()): pass writer.stop()