def test_prosac(self): """ 改进抽样一致 """ Camera.reset() self.__last_frame = None print("PROSAC") for i in range(1, 30): start = time.clock() _, frame = Camera.get_frame() if self.__last_frame is None: self.__last_frame = frame continue img = cv2.absdiff(frame, self.__last_frame) self.__last_frame = frame img = cv2.GaussianBlur(img, (5, 5), 2.5) img = ImageUtils.morphology(img, cv2.MORPH_DILATE, 16) _, img = ImageUtils.binary(img, threshold_type=cv2.THRESH_OTSU) # 计算特征点 key_points, descriptors = ImageUtils.get_key_points(frame, img) matches = ImageUtils.knn_match(self.__target_descriptors, descriptors) if len(matches) > 0: # 匹配到合适的特征点 points = ImageUtils.get_matches_points(key_points, matches) src_key_points = ImageUtils.get_matches_points( self.__target_key_points, matches, 1) # PROSAC去除错误点 _, mask = cv2.findHomography(src_key_points, points, cv2.RHO) if mask is not None: points_after = points[mask.ravel() == 1] end = time.clock() print("%d\t%d\t%d\t%f" % (i, len(points), len(points_after), end - start))
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 _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)