Example #1
0
 def _warning(self):
     #预警线程
     count = 0  #当前运动目标
     box = None  #运动目标位置
     logger.info("是否开启预警模式:" + str(self.isWarning))
     while self.isWarning:
         #若为运动检测模式,进入运动检测
         if self.isWatching:
             #若未初始化运动检测器,初始化运动检测器
             if not self.watchDog.isWorking():
                 self.watchDog.startWorking(self.frame)
             else:
                 self.item = self.watchDog.update(self.frame)
             if self.item is not None and self.item.getMessage()['isGet']:
                 #若发现动态物体,延时1秒,拍摄10张照片
                 count += 1
                 time.sleep(self.imageDelay)
                 box = IOUtil.countBox(self.item.getMessage()["rect"])
                 logger.info("发现一个运动物体位于" + str(box))
             else:
                 count = 0
             if count >= self.warnCount:
                 #一秒后退出动态监控状态,进入运动追踪模式
                 logger.info("累计侦测到目标十次运动,锁定目标,开启目标追踪模式")
                 count = 0
                 self.stopWatching()
                 self.startTracking(box)
         if self.isTracking:
             #若为运动追踪模式,开启运动追踪
             if not self.tracker.isWorking():
                 print('开始目标追踪,追踪范围为' + str(box))
                 self.tracker.startWorking(self.frame, box)
             else:
                 self.item = self.tracker.update(self.frame)
             if self.item is not None and self.item.getMessage()['isGet']:
                 pass
             else:
                 print("目标丢失,退出目标追踪模式,进入运动监控状态")
                 self.stopTracking()
                 self.startWatching()
Example #2
0
def warning(mydict, screenCenter):
    '''
	开启预警
	'''
    box = None
    warnImages = []
    watchDog = WatchDog()
    tracker = None
    if TRACKER_TYPE is 1:
        tracker = CamShiftTracker()
    elif TRACKER_TYPE is 2:
        tracker = DlibTracker()
    else:
        tracker = TemplateTracker()
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    IOUtil.mkdir(WARN_DIR)
    lastTrackTime = None
    isWatching = True
    isTracking = False
    direction = {
        "right": '{"module":2,"command":0}',
        "left": '{"module":2,"command":1}',
        "up": '{"module":2,"command":3}',
        "down": '{"module":2,"command":2}'
    }
    logger.info("是否开启预警模式:" + str(mydict['isWarning']))
    while mydict['isWarning']:
        item = None
        #若为运动检测模式,进入运动检测
        if isWatching:
            #若未初始化运动检测器,初始化运动检测器
            if not watchDog.isWorking():
                watchDog.startWorking(mydict['frame'])
            else:
                item = watchDog.update(mydict['frame'])
                mydict['item'] = item
            if item is not None and item.getMessage()['isGet']:
                #若发现动态物体,延时,拍摄10张照片
                imageFileName = WARN_DIR + IOUtil.getImageFileName()
                warnImages.append(imageFileName)
                time.sleep(MOVEMENT_TRACK_DELAY)
                box = IOUtil.countBox(item.getMessage()["rect"])
                logger.info("发现一个运动物体位于" + str(box))
                cv2.imwrite(imageFileName, item.getFrame())
                logger.info("写入一张预警图片:" + imageFileName)
            else:
                warnImages = []
            if len(warnImages) >= MOVE_TRACK_COUNT:
                #一秒后退出动态监控状态,进入运动追踪模式
                logger.info("累计侦测到目标十次运动,锁定目标,开启目标追踪模式,关闭运动检测")
                if IS_SEND_EMAIL and (
                        lastTrackTime is None
                        or time.time() - lastTrackTime > EMAIL_DELAY):
                    startSendEmail(warnImages)
                isWatching = False
                if watchDog.isWorking():
                    watchDog.stopWorking()
                logger.info("开启目标追踪...")
                isTracking = True
        if isTracking:
            #若为运动追踪模式,开启运动追踪
            if not tracker.isWorking():
                logger.info('开始目标追踪,初始化追踪范围为' + str(box))
                tracker.startWorking(item.getFrame(), box)
            else:
                item = tracker.update(mydict['frame'])
            if item is not None and item.getMessage()['isGet']:
                mydict['item'] = copy.copy(item)
                center = item.getMessage()['center']
                distance = center[0] - screenCenter[0]
                levelDirect = "mid"
                if distance < 0:
                    levelDirect = "right"
                elif distance > 0:
                    levelDirect = "left"
                levelDiss = abs(distance)
                if levelDirect is not "mid" and levelDiss > MOVEMENT_THRESHOLD:
                    sock.sendto(direction[levelDirect].encode(),
                                (CAMERA_COMMAND_IP, CAMERA_COMMAND_PORT))
                distance = center[1] - screenCenter[1]
                virtDirect = "mid"
                if distance < 0:
                    virtDirect = "up"
                elif distance > 0:
                    virtDirect = "down"
                virtDiss = abs(distance)
                if virtDirect is not "mid" and virtDiss > MOVEMENT_THRESHOLD:
                    sock.sendto(direction[virtDirect].encode(),
                                (CAMERA_COMMAND_IP, CAMERA_COMMAND_PORT))
            else:
                logger.info("目标丢失,退出目标追踪模式,进入运动监控状态")
                isTracking = False
                if tracker.isWorking():
                    tracker.stopWorking()
                logger.info("开启运动检测...")
                isWatching = True
                lastTrackTime = time.time()
    sock.close()
    logger.info("预警结束")