def update(self,frame): #运动检测 if frame is None: return if not self.isWorking(): logger.error("运动检测器未初始化") raise Exception("运动检测器未初始化") item = None sample_frame = cv2.GaussianBlur(cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY),(21,21),0) diff = cv2.absdiff(self._background,sample_frame) diff = cv2.threshold(diff, 25, 255, cv2.THRESH_BINARY)[1] diff = cv2.dilate(diff, self.es, iterations=2) image, cnts, hierarchy = cv2.findContours(diff.copy(),cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) bigC = None bigMulti = 0 for c in cnts: if cv2.contourArea(c) < bigMulti: continue bigMulti = cv2.contourArea(c) (x,y,w,h) = cv2.boundingRect(c) bigC = ((x,y),(x+w,y+h)) message = {} if bigC is not None: center = IOUtil.countCenter(bigC) cv2.circle(frame,center,5,(55,255,155),1) cv2.rectangle(frame, bigC[0],bigC[1], (255,0,0), 2, 1) message['isGet'] = True message['center'] = center message["rect"] = bigC message['time'] = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime()) item = MessageItem(frame,message) else: message['isGet'] = False item = MessageItem(frame,message) return item
def update(self,frame): if frame is None: return if not self.isWorking(): logger.error('追踪器未初始化') raise Exception("跟踪器未初始化") item = None hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) dst = cv2.calcBackProject([hsv], [0], self.roi_hist, [0,180], 1) ret, self.track_window = cv2.meanShift(dst, self.track_window, self.term_crit) #ret, self.track_window = cv2.CamShift(dst, self.track_window, self.term_crit) x,y,w,h = self.track_window center = IOUtil.countCenter(((x,y),(x+w,y+h))) cv2.circle(frame,center,5,(55,255,155),1) cv2.rectangle(frame, (x,y), (x+w,y+h), 255, 2) cv2.putText(frame, 'Tracked', (x-25,y-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2, cv2.LINE_AA) screenSize = frame.shape #print(center[0],":",center[1],":",abs(center[0] - screenSize[1]),":",abs(center[1] - screenSize[0])) message = {} if(center[0] > self.loseDistance and center[1] > self.loseDistance and abs(center[0] - screenSize[1]) > self.loseDistance and abs(center[1] - screenSize[0]) > self.loseDistance): message['isGet'] = True message['center'] = center message["rect"] = ((x,y),(x+w,y+h)) message['time'] = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime()) else: message['isGet'] = False return MessageItem(frame,message)
def update(self,frame): if frame is None: return if not self.isWorking(): logger.error("追踪器未初始化") raise Exception("追踪器未初始化") message = {} item = None self.tracker.update(frame) box_predict = self.tracker.get_position() screenSize = frame.shape rect = ((int(box_predict.left()),int(box_predict.top())),(int(box_predict.right()),int(box_predict.bottom()))) center = IOUtil.countCenter(rect) cv2.rectangle(frame,rect[0],rect[1],(0,255,255),1) # 用矩形框标注出来 if(center[0] > self.loseDistance and center[1] > self.loseDistance and abs(center[0] - screenSize[1]) > self.loseDistance and abs(center[1] - screenSize[0]) > self.loseDistance): message['isGet'] = True message['center'] = center message["rect"] = rect message['time'] = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime()) else: message['isGet'] = False return MessageItem(frame,message)
def update(self,frame): if frame is None: return if not self.isWorking(): logger.error('追踪器未初始化') raise Exception("跟踪器未初始化") item = None message = {} result = cv2.matchTemplate(frame,self.objectFrame,self.method) minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(result) if maxVal > self.updateRet: self.objectFrame = frame[maxLoc[1]:maxLoc[1]+self.h,maxLoc[0]:maxLoc[0]+self.w] if maxVal > self.valThre: center = IOUtil.countCenter((maxLoc,(maxLoc[0] + self.w,maxLoc[1] + self.h))) cv2.rectangle(frame, maxLoc,(maxLoc[0] + self.w,maxLoc[1] + self.h), 255, 2) message['isGet'] = True message['center'] = center message["rect"] = (maxLoc,(maxLoc[0] + self.w,maxLoc[1] + self.h)) message['time'] = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime()) else: message['isGet'] = False return MessageItem(frame,message)
def update(self,frame): if frame is None: return if not self.isWorking(): logger.error('追踪器未初始化') raise Exception("追踪器未初始化") item = None message = {} status,coord = self.tracker.update(frame) if status: rect = ( (int(coord[0]),int(coord[1])), (int(coord[0] + coord[2]),int(coord[1]+coord[3]))) center = IOUtil.countCenter(rect) cv2.rectangle(frame, rect[0], rect[1], (255,0,0), 2, 1) message['isGet'] = True message['center'] = center message["rect"] = rect message['time'] = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime()) else: message['isGet'] = False return MessageItem(frame,message)