async def _capture(self, websocket): response_frame = response_pb2.FrameResponse() response_frame.code = 1 response_frame.type = response_pb2.FrameResponse.VIDEO ret, img = Camera.get_frame() if ret: response_frame.frame = ImageUtils.img2bytes(img) await websocket.send(response_frame.SerializeToString()) self.__outline.add_frame(img)
async def execute(self, websocket): self.__capture_scheduler.add_job(self._capture, 'interval', [websocket], id=self.__capture_job_id, max_instances=3, seconds=1 / 7) await asyncio.wait([ asyncio.ensure_future(self._wait_stop(websocket)), ]) # 直到收到停止命令 background, max_difference_frame = self.__outline.get_max_difference_frame( ) response_frame = response_pb2.FrameResponse() response_frame.code = 1 response_frame.type = response_pb2.FrameResponse.OUTLINE response_frame.frame = ImageUtils.img2bytes(max_difference_frame) await websocket.send(response_frame.SerializeToString()) response_frame.type = response_pb2.FrameResponse.BACKGROUND response_frame.frame = ImageUtils.img2bytes(background) await websocket.send(response_frame.SerializeToString())
async def _send_frame(self, websocket): response = response_pb2.FrameResponse() response.code = 1 response.type = response_pb2.FrameResponse.VIDEO while self.__capture: ret, img = Camera.get_frame() if ret: old_position, new_position, contour = self.__tracker.track(img) print(old_position, new_position) # 把轮廓绘制到帧上 if contour is not None: cv2.drawContours(img, [contour], 0, (255, 0, 0), 2) response.frame = ImageUtils.img2bytes(img) try: await websocket.send(response.SerializeToString()) except ConnectionClosed: self.__capture = False await asyncio.sleep(1 / 10)