How to prevent the duplicate result from previous frame in python object tracking
Currently, I work on Object detection, tracking and counting and I want to store the result from object detection ,tracking, and counting and whenever the vehicle cross the line, the result always give me duplicate. how can i prevent that
and here for the camera code
class Camera(BaseCamera): """ OpenCV video stream """ video_source = 0 start, end = Point(0, 500), Point(1280, 500) detector = Detector() tracker = ByteTrack() line_zone = LineZone(start=start, end=end) annotator = LineZoneAnnotator()
def __init__(self, enable_detection: bool = False):
video_source = os.environ.get("VIDEO_SOURCE")
try:
video_source = int(video_source)
except Exception as exp: # pylint: disable=broad-except
if not video_source:
raise EnvironmentError("Cannot open the video source!") from exp
finally:
Camera.set_video_source(video_source)
super().__init__()
self.enable_detection = enable_detection
@staticmethod
def set_video_source(source):
"""Set video source"""
Camera.video_source = source
@classmethod
def frames(cls):
"""
Get video frame
"""
camera = cv2.VideoCapture(Camera.video_source)
if not camera.isOpened():
raise RuntimeError("Could not start camera.")
while True:
# read current frame
ret, img = camera.read()
# Loop back
if not ret:
camera.set(cv2.CAP_PROP_POS_FRAMES, 0)
continue
# Object detection
results = cls.detector(image=img)
selected_classes = [ 2, 3]
tensorflow_results = results.detections
cls.annotator.annotate(img, cls.line_zone)
if not tensorflow_results:
yield cv2.imencode(".jpg", img)[1].tobytes()
continue
detections = Detections.from_tensorflow(tensorflow_results=tensorflow_results)
detections = cls.tracker.update_with_detections(detections=detections)
detections = detections[np.isin(detections.class_id, selected_classes)]
result=cls.line_zone.trigger(detections)
if type(result)!=type(None) and len(result)>=3:
print(result[2])
img = visualize(image=img, detections=detections)
# encode as a jpeg image and return it
yield cv2.imencode(".jpg", img)[1].tobytes()