Beispiel #1
0
    def __init__(self, *args, **kwargs):
        """
        Constructor for CollectProcess class.
        Initialize the fast, brisk and bf detectors. Loads the templates data and
        create the directory for collection.

        :param args:
        :param kwargs:
        """
        self.__fast = cv2.FastFeatureDetector_create(threshold=50,
                                                     nonmaxSuppression=50)
        self.__br = cv2.BRISK_create()
        self.__bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

        try:
            model_type = kwargs['model_type']
        except KeyError:
            model_type = 'CT'

        self.__templates = make_templates(model_type, self.__fast, self.__br)

        self.__modes = args
        self.__kwargs = kwargs

        self.__dp = DataPath()

        self.__file_index = self.__dp.get_index('collected')
        self.__dir = os.path.join(self.__dp.collected, str(self.__file_index))
        os.mkdir(self.__dir)

        self.__index = 0

        col_names = ('detections', 'intensity', 'fast_kp', 'process_time')
        self.__df = pd.DataFrame(columns=col_names)
Beispiel #2
0
def combine_all():

    dp = DataPath()

    for directory in os.listdir(dp.collected):
        df = combine_batch(directory, dp.collected)
        df.to_csv(os.path.join(dp.combined, '{:s}.csv'.format(directory)))
Beispiel #3
0
def make_templates(model_type, fast, br):

    p = DataPath()
    path = os.path.join(p.templates, model_type)
    templates = []
    for x in os.listdir(path):
        name = x.split(".")[0]
        original = cv2.imread(os.path.join(path, x), 1)
        templates.append(Template(original, name, fast, br))

    return templates
Beispiel #4
0
def steps(example=0):

    from src.process import ImageProcess as ip
    from src.templates import make_templates
    from src.paths import DataPath

    options = [
        'norm', 'gray', 'blurred1', 'opening1', 'threshold1', 'masked1',
        'opening2', 'blurred2', 'threshold2'
    ]

    fast = cv2.FastFeatureDetector_create(threshold=50, nonmaxSuppression=50)
    br = cv2.BRISK_create()
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

    ct_models = make_templates('CT', fast, br)

    dp = DataPath()

    try:
        file = os.listdir(dp.examples)[example]
    except KeyError:
        print('Invalid example')

    for option in options:

        image = cv2.imread(os.path.join(dp.examples, file))

        process = ip(image, ct_models, fast, br, bf, 'draw', debug=option)

        if option == 'norm':
            test_image = cv2.cvtColor(process.test, cv2.COLOR_BGR2RGB)
            plt.imshow(test_image)
        else:
            plt.imshow(process.test, cmap='gray')

        plt.show()
Beispiel #5
0
def samples():

    from src.process import ImageProcess as ip
    from src.templates import make_templates
    from src.paths import DataPath

    fast = cv2.FastFeatureDetector_create(threshold=50, nonmaxSuppression=50)
    br = cv2.BRISK_create()
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

    ct_models = make_templates('CT', fast, br)

    dp = DataPath()

    for file in os.listdir(dp.examples):

        image = cv2.imread(os.path.join(dp.examples, file))

        process = ip(image, ct_models, fast, br, bf, 'draw')

        image = cv2.cvtColor(process.image, cv2.COLOR_BGR2RGB)

        plt.imshow(image)
        plt.show()
Beispiel #6
0
class CollectProcess:
    """
    Collect Process class to harvest data
    """
    def __init__(self, *args, **kwargs):
        """
        Constructor for CollectProcess class.
        Initialize the fast, brisk and bf detectors. Loads the templates data and
        create the directory for collection.

        :param args:
        :param kwargs:
        """
        self.__fast = cv2.FastFeatureDetector_create(threshold=50,
                                                     nonmaxSuppression=50)
        self.__br = cv2.BRISK_create()
        self.__bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

        try:
            model_type = kwargs['model_type']
        except KeyError:
            model_type = 'CT'

        self.__templates = make_templates(model_type, self.__fast, self.__br)

        self.__modes = args
        self.__kwargs = kwargs

        self.__dp = DataPath()

        self.__file_index = self.__dp.get_index('collected')
        self.__dir = os.path.join(self.__dp.collected, str(self.__file_index))
        os.mkdir(self.__dir)

        self.__index = 0

        col_names = ('detections', 'intensity', 'fast_kp', 'process_time')
        self.__df = pd.DataFrame(columns=col_names)

    def info(self):
        if self.__df.empty:
            return "\nError! No data was collected"
        else:
            det_count = sum(self.__df['detections'].tolist())
            mean_inten = np.mean(self.__df['intensity'].tolist())
            mean_time = np.mean(self.__df['process_time'].tolist())
            return ("\nProcess completed\n\n"
                    "Collected total of {:d} samples\n"
                    "Detection count: {:d}\n"
                    "Average intensity: {:.5f}\n"
                    "Average time: {:.5f}\n".format(self.__index, det_count,
                                                    mean_inten, mean_time))

    def collect_from_screen(self, width, heigth, windowed=True, time_out=1):

        self.__time_out = time_out

        if width <= 0 or heigth <= 0:
            raise ValueError("Incorrect dimensions for screen capture")

        # if on windowed mode, lower the capture area
        if windowed:
            padding = 27
            heigth += padding

        if 'debug' in self.__modes:
            while True:
                # captures the screen with ImageGrab in RGB.
                screen = np.array(
                    ImageGrab.grab(bbox=(0, padding, width, heigth)))
                # converts it to BGR
                screen = cv2.cvtColor(screen, cv2.COLOR_RGB2BGR)
                process = self.analyse_frame(screen)

                cv2.imshow('AimAssistant', process.image)

                if cv2.waitKey(25) & 0xFF == ord('q'):
                    cv2.destroyAllWindows()
                    break
        else:
            while True:
                screen = np.array(
                    ImageGrab.grab(bbox=(0, padding, width, heigth)))
                screen = cv2.cvtColor(screen, cv2.COLOR_RGB2BGR)
                process = self.analyse_frame(screen)
                if 'lim' in self.__kwargs:
                    if self.__index >= self.__kwargs['lim']:
                        break

        print(self.info())
        if not self.__df.empty:
            csv_name = 'process_{:d}.csv'.format(self.__file_index)
            self.__df.to_csv(os.path.join(self.__dp.dataframe, csv_name))
        else:
            shutil.rmtree(self.__dir)

    def collect_from_video(self, file, frame_limit):
        """
        """
        if not os.path.exists(os.path.join(self.__dp.video, file)):
            raise OSError

        # Open the video file
        cap = cv2.VideoCapture(path)
        if 'debug' in self.__modes:
            while cap.isOpened():
                # Read the frame
                ret, frame = cap.read()
                process = self.analyse_frame(frame)

                cv2.imshow('AimAssistant', process.image)

                if cv2.waitKey(25) & 0xFF == ord('q'):
                    cv2.destroyAllWindows()
                    break
        else:
            while cap.isOpened():
                # Read the frame
                ret, frame = cap.read()
                process = self.analyse_frame(frame)

        #TODO saving the data part.

    def analyse_frame(self, image):

        gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
        intensity = cv2.mean(gray)[0]
        kp = self.__fast.detect(gray, None)

        process_time = time()
        process = ImageProcess(image, self.__templates, self.__fast, self.__br,
                               self.__bf, 'collect')
        process_time = time() - process_time

        if process.detections:
            print("Detected.")
            new_row = [
                len(process.detections), intensity,
                len(kp), process_time
            ]

            self.__df.loc[self.__index] = new_row
            # Save the data into packet for further use
            packet = {'image': image, 'detections': process.detection_data}

            file_name = os.path.join(self.__dir,
                                     "{:d}.pickle".format(self.__index))
            with open(file_name, 'wb') as file:
                pickle.dump(packet, file)

            self.__index += 1

            # Sleep so we don't analyse the same frame multiple times
            sleep(self.__time_out)

        return process
Beispiel #7
0
 def __init__(self):
     """
     Constructor for Judge class
     """
     self.__dp = DataPath()
Beispiel #8
0
def RPNplus():

    modelPath = './models/model.npy'
    vggModelPath = './models/vgg16.npy'

    checkFile(vggModelPath)
    checkFile(modelPath)

    image_height = 720
    image_width = 960

    testDeal = data_engine.RPN_Test()

    # Change to gpu if you have a NVIDIA gpu speeds ups the process a lot
    with tf.device("/cpu:0"):
        sess = tf.Session()
        image = tf.placeholder(tf.float32, [1, image_height, image_width, 3])

        cnn = RPN(vggModelPath, modelPath)
        with tf.name_scope('content_rpn'):
            cnn.build(image)

        path = DataPath()
        startTime = time.time()
        name = 'RPNplus'
        df_columns = ('correct_RPN', 'incorrect_RPN', 'RPN_detections',
                      'RPN_time')
        number = 0
        for directory in os.listdir(path.collected):
            result_file = os.path.join(path.dataframes,
                                       '{:s}_{:}.csv'.format(name, directory))
            if not os.path.exists(result_file):
                df = pd.DataFrame(columns=df_columns)
                files = os.path.join(path.collected, directory)
                image_number = 0
                for file in os.listdir(files):

                    index = int(file.split(".")[0])

                    with open(os.path.join(files, file), 'rb') as f:
                        data = pickle.load(f)
                        im = data['image']
                        detections = data['detections']

                    detected, c = detect_RPN(im, testDeal, cnn, image, sess)

                    correct = 0
                    for x in detections:
                        z, y = detect_RPN(x, testDeal, cnn, image, sess)
                        correct = correct + z

                    expected = len(detections)
                    incorrect = expected - correct

                    new_row = {
                        'correct_RPN': correct,
                        'incorrect_RPN': incorrect,
                        'RPN_detections': detected,
                        'RPN_time': c
                    }

                    df.loc[index] = new_row

                    print("Processed", image_number)
                    image_number = image_number + 1
                df.to_csv(result_file)
                print('total use time : %ds' % (time.time() - startTime))