#!/usr/bin/env python3
# -*- coding: utf-8 -*-


import threading
import time
import cv2
import numpy as np

class cv_thread(threading.Thread):
    def __init__(self):
        super(cv_thread, self).__init__()
        self.daemon = True
        self._stopped = threading.Event()
        self._frame = None
        self._newFrameEvent = threading.Event()
        self.debugFrame = None
        
    def run(self):
        print("thread started\n")
        while not self._stopped.is_set():
            self._newFrameEvent.wait()
            if not (self._frame is None):
                time.sleep(1)
                self.debugFrame = cv2.GaussianBlur(self._frame, (5, 5), 2)
        print("thread stopped")
        
    def stop(self):
        self._stopped.set()
        self.join()

    def setframe(self, frame):
        if not self._newFrameEvent.is_set():
            self._frame = frame
            self._newFrameEvent.set()
            return True
        return False
    
    def isready(self):
        return not self._newFrameEvent.is_set()
    
