Example #1
0
    def draw_detection_region(frame: Frame, mask):
        """
        Rysuje na klatce obszar czułości kamery.

        :param Frame frame: Klatka obrazu.
        :return: Klatka obrazu z oznaczonym obszarem.
        :rtype: Frame
        """

        height, width = frame.size()
        horizontal_border = Configuration.horizontal_border()
        vertical_border = Configuration.vertical_border()
        lup = (int(horizontal_border), int(vertical_border))
        rup = (int(width - horizontal_border), int(vertical_border))
        llp = (int(horizontal_border), int(height - vertical_border))
        rlp = (int(width - horizontal_border), int(height - vertical_border))
        frame.img = cv2.line(frame.img, llp, lup, (0, 0, 255), thickness=3)
        frame.img = cv2.line(frame.img, llp, rlp, (0, 0, 255), thickness=3)
        frame.img = cv2.line(frame.img, rlp, rup, (0, 0, 255), thickness=3)
        frame.img = cv2.line(frame.img, lup, rup, (0, 0, 255), thickness=3)
        mask = cv2.line(mask, llp, lup, (0, 0, 255), thickness=3)
        mask = cv2.line(mask, llp, rlp, (0, 0, 255), thickness=3)
        mask = cv2.line(mask, rlp, rup, (0, 0, 255), thickness=3)
        mask = cv2.line(mask, lup, rup, (0, 0, 255), thickness=3)

        return frame, mask
Example #2
0
    def draw_detection_region(frame: Frame, mask):
        """
        Rysuje na klatce obszar czułości kamery.

        :param Frame frame: Klatka obrazu.
        :return: Klatka obrazu z oznaczonym obszarem.
        :rtype: Frame
        """

        height, width = frame.size()
        horizontal_border = Configuration.horizontal_border()
        vertical_border = Configuration.vertical_border()
        lup = (int(horizontal_border), int(vertical_border))
        rup = (int(width-horizontal_border), int(vertical_border))
        llp = (int(horizontal_border), int(height-vertical_border))
        rlp = (int(width-horizontal_border), int(height-vertical_border))
        frame.img = cv2.line(frame.img, llp, lup, (0, 0, 255), thickness=3)
        frame.img = cv2.line(frame.img, llp, rlp, (0, 0, 255), thickness=3)
        frame.img = cv2.line(frame.img, rlp, rup, (0, 0, 255), thickness=3)
        frame.img = cv2.line(frame.img, lup, rup, (0, 0, 255), thickness=3)
        mask = cv2.line(mask, llp, lup, (0, 0, 255), thickness=3)
        mask = cv2.line(mask, llp, rlp, (0, 0, 255), thickness=3)
        mask = cv2.line(mask, rlp, rup, (0, 0, 255), thickness=3)
        mask = cv2.line(mask, lup, rup, (0, 0, 255), thickness=3)

        return frame, mask
Example #3
0
    def get_ratio():
        """
        Oblicza stosunek długości na obrazie wyrażonej w pixelach do rzeczywistej długości w metrach.

        :return: Znaleziony stosunek.
        :rtype: float
        """

        return float(Configuration.meters_length()) / float(Configuration.pixel_length())
Example #4
0
    def __is_on_left(new_car: Vehicle):
        """
        Sprawdza czy samochód jest blisko lewej krawędzi obszaru czułości.

        :param Vehicle new_car: Obserwoway samochód.
        :return: Prawda/fałsz.
        :rtype: bool
        """

        hborder = Configuration.horizontal_border()
        distance = Configuration.distance_from_border()
        return new_car.centerx < (hborder + distance)
Example #5
0
    def __is_on_right(new_car):
        """
        Sprawdza czy samochód jest blisko prawej krawędzi obszaru czułości.

        :param Vehicle new_car: Obserwoway samochód.
        :return: Prawda/fałsz.
        :rtype: bool
        """

        hborder = Configuration.horizontal_border()
        distance = Configuration.distance_from_border()
        return new_car.centerx > (Follower.__frame_width - hborder - distance)
Example #6
0
    def __is_on_left(new_car: Vehicle):
        """
        Sprawdza czy samochód jest blisko lewej krawędzi obszaru czułości.

        :param Vehicle new_car: Obserwoway samochód.
        :return: Prawda/fałsz.
        :rtype: bool
        """

        hborder = Configuration.horizontal_border()
        distance = Configuration.distance_from_border()
        return new_car.centerx < (hborder + distance)
Example #7
0
    def __is_on_right(new_car):
        """
        Sprawdza czy samochód jest blisko prawej krawędzi obszaru czułości.

        :param Vehicle new_car: Obserwoway samochód.
        :return: Prawda/fałsz.
        :rtype: bool
        """

        hborder = Configuration.horizontal_border()
        distance = Configuration.distance_from_border()
        return new_car.centerx > (Follower.__frame_width - hborder - distance)
    def decay_learning_rate(epoch):
        """
        decay the learning rate after epoch specified in Cfg.lr_decay_after_epoch
        """

        # only allow decay for non-adaptive solvers
        assert Cfg.nn_solver in ("sgd", "momentum", "adam")

        if epoch >= Cfg.lr_decay_after_epoch:
            lr_new = (Cfg.lr_decay_after_epoch /
                      Cfg.floatX(epoch)) * Cfg.learning_rate_init
            return Cfg.floatX(lr_new)
        else:
            return Cfg.floatX(Cfg.learning_rate_init)
    def adjust_learning_rate_finetune(epoch):

        if Cfg.lr_drop and (epoch == Cfg.lr_drop_in_epoch):
            # Drop the learning rate in epoch specified in Cfg.lr_drop_after_epoch by factor Cfg.lr_drop_factor
            # Thus, a simple separation of learning into a "region search" and "finetuning" stage.
            lr_new = Cfg.floatX((1.0 / Cfg.lr_drop_factor) * Cfg.learning_rate)
            print("")
            print(
                "Learning rate drop in epoch {} from {:.6f} to {:.6f}".format(
                    epoch, Cfg.floatX(Cfg.learning_rate), lr_new))
            print("")
            Cfg.learning_rate = lr_new

        return lr_new
Example #10
0
    def draw_speed_region(frame: Frame):
        """
        Rysuje na obrazie dwie pionowe linie, służące pomiarowi osiąganej prędkości.

        :param Frame frame: Ramka obrazu wideo.
        :return: Ramka z narysowanymi liniami.
        :rtype: Frame
        """

        h, w = frame.size()
        border1 = int(Configuration.distance_border1())
        border2 = int(Configuration.distance_border2())
        frame.img = cv2.line(frame.img, (border1, 0), (border1, h), (255, 0, 255), thickness=4)
        frame.img = cv2.line(frame.img, (border2, 0), (border2, h), (255, 0, 255), thickness=4)
        return frame
Example #11
0
def script_mnist_number_classifier(arguments):
    """Script for training and evaluation the number classifier of the MNIST dataset

    :param arguments - arguments coming from the parser
    """
    # Create folders
    model_path = os.path.join(arguments.data_path, "models")
    os.makedirs(model_path, exist_ok=True)
    config_path = os.path.join(model_path, "configurations")
    os.makedirs(config_path, exist_ok=True)
    visualization_path = os.path.join(arguments.data_path, "visualization")
    os.makedirs(visualization_path, exist_ok=True)

    # Load dataset
    print("%%%%%%%% Loading dataset.")
    mnist_dataset = MnistDataset(normalize=True, nb_output_classes=10)
    x_train, y_train, x_test, y_test, image_height, image_width = mnist_dataset.get_dataset(
    )

    labels_train = np.argmax(y_train, axis=1)
    MnistDataset.visualize(visualization_path, 10, 10, "train", x_train,
                           labels_train)

    # Start the current model
    current_model_name = str(int(time.time()))
    current_model_path = os.path.join(model_path, current_model_name)
    os.makedirs(current_model_path, exist_ok=True)
    print("%%%%%%%% Starting model {}".format(current_model_name))

    # Create the config associated
    config = Configuration(current_model_name, image_height, image_width,
                           arguments.normalize, arguments.shuffle,
                           arguments.batch_size, arguments.nb_epoch,
                           arguments.nb_classes, arguments.validation_split)
    config.save(config.get_filename(config_path, current_model_name))

    # Number classifier
    number_classifier = NumberClassifier()

    # Training
    number_classifier.train(x_train, y_train, config, current_model_path)

    # Evaluation
    number_classifier.evaluate(x_test, y_test, visualization_path)

    print("%%%%%%%% Done model {}".format(current_model_name))
Example #12
0
    def __select(vehicles: list, frame: Frame):
        """
        Dokonuje selekcji znalezionych obiektów. Odrzuca obiekty znajdujące się przy krawędzi obrazu.

        :param list vehicles: Wektor potencjalnych samochodów.
        :param Frame frame: Klatka obrazu
        :return: Wyseleksjonowane pojazdy.
        :rtype:  list
        """

        height, width = frame.size()
        result = []
        for vehic in vehicles:
            horizontal_border = Configuration.horizontal_border()
            vertical_border = Configuration.vertical_border()
            if (vehic.centerx > horizontal_border) and (vehic.centerx < width-horizontal_border)\
                    and (vehic.centery > vertical_border) and (vehic.centery < height-vertical_border):
                result.append(vehic)
        return result
Example #13
0
    def __has_valid_size(region: np.ndarray):
        """
        Sprawdza czy obszar nie jest za mały oraz za duży.

        :param np.ndarray region: Obraz zwierający region oznaczony niezerową wartością.
        :return: Prawda/fałsz.
        :rtype: bool
        """

        max = (480*720/2)
        min = Configuration.pixel_limit()
        return (np.count_nonzero(region) >= min) and (np.count_nonzero(region) < max)
Example #14
0
    def __has_valid_size(region: np.ndarray):
        """
        Sprawdza czy obszar nie jest za mały oraz za duży.

        :param np.ndarray region: Obraz zwierający region oznaczony niezerową wartością.
        :return: Prawda/fałsz.
        :rtype: bool
        """

        max = 480 * 720 / 2
        min = Configuration.pixel_limit()
        return (np.count_nonzero(region) >= min) and (np.count_nonzero(region) < max)
Example #15
0
    def perform(frame: Frame, database, img_saver, run_classyfication=True):
        """
        Dokonuje przetwarzania ramki obrazu przez algorytm.

        :param frame: Ramka obrazu.
        :param database: Baza danych do zapisywania parametrów.
        :param img_saver: Obiekt zapisyjący pliki obrazów.
        :return: Przetworzona ramka.
        """

        vehicles, mask = Detector.find_vehicles(frame)
        objects = Follower.update(vehicles, frame, mask)

        records = []

        if run_classyfication:
            if objects is not None:
                for obj in objects:
                    record = Classyfication.perform(obj, Algorithm.file)
                    records.append(record)
                    if database is not None:
                        database.write(record, Algorithm.file)
                    if img_saver is not None:
                        img_saver.write(record, Algorithm.file)

        mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)

        # Rysowanie pojazdów.
        if Configuration.draw_cars():
            frame, mask = Detector.draw_vehicles(frame, mask, vehicles)

        # Rysowanie obszaru wykrywania.
        if Configuration.draw_detection_region():
            frame, mask = Detector.draw_detection_region(frame, mask)

        # Rysowanie obszaru pomiaru prędkości
        if Configuration.draw_speed_region():
            frame = Classyfication.draw_speed_region(frame)

        return frame, mask, records
Example #16
0
    def __select(vehicles: list, frame: Frame):
        """
        Dokonuje selekcji znalezionych obiektów. Odrzuca obiekty znajdujące się przy krawędzi obrazu.

        :param list vehicles: Wektor potencjalnych samochodów.
        :param Frame frame: Klatka obrazu
        :return: Wyseleksjonowane pojazdy.
        :rtype:  list
        """

        height, width = frame.size()
        result = []
        for vehic in vehicles:
            horizontal_border = Configuration.horizontal_border()
            vertical_border = Configuration.vertical_border()
            if (
                (vehic.centerx > horizontal_border)
                and (vehic.centerx < width - horizontal_border)
                and (vehic.centery > vertical_border)
                and (vehic.centery < height - vertical_border)
            ):
                result.append(vehic)
        return result
Example #17
0
    def find_color(image: np.ndarray):
        """
        Przeprowadza analizę dominujących kolorów, wybiera z nich największy.

        :param np.ndarray image: przeszukiwany obraz.
        :return: obraz wypełniony kolorem, wartość koloru.
        """

        # Znajdź najlepsze kolory.
        color_count = int(Configuration.color_number())
        colors, percents, color_bar = ColorDetector.__find_dominant_colors(image, n=color_count)
        # Wybierz największy kolor.
        best_percent = max(percents)
        best_percent_index = percents.index(best_percent)
        color = colors[best_percent_index]

        return color_bar, color
    def initialize_c_as_mean(self, inputs, n_batches, eps=0.1):
        """
        initialize c as the mean of the final layer representations from all samples propagated in n_batches
        """

        reps = self.get_OneClass_SVDD_network_reps(inputs)
        self.reps = reps
        print("[INFO:] Initializing c and Radius R value...")

        # consider the value all the number of batches (and thereby samples) to initialize from
        c = np.mean(reps, axis=0)

        # If c_i is too close to 0 in dimension i, set to +-eps.
        # Reason: a zero unit can be trivially matched with zero weights.
        c[(abs(c) < eps) & (c < 0)] = -eps
        c[(abs(c) < eps) & (c > 0)] = eps

        self.cvar = c  # Initialize the center

        # initialize R at the (1-nu)-th quantile of distances
        dist_init = np.sum((reps - c)**2, axis=1)
        out_idx = int(np.floor(len(reps) * Cfg.nu))
        sort_idx = dist_init.argsort()
        self.Rvar = Cfg.floatX(dist_init[sort_idx][-out_idx])
Example #19
0
    def __write_settings(self):
        """
        Zapisz wartości z widgetów
        """

        Configuration.distance_border1(self.border1_adjustment.get_value())
        Configuration.distance_border2(self.border2_adjustment.get_value())
        Configuration.meters_length(self.meters_length_spin_adjustment.get_value())
        Configuration.color_number(self.color_number_spin_adjustment.get_value())
        Configuration.horizontal_border(self.horizontal_border_spin_adjustment.get_value())
        Configuration.vertical_border(self.vertical_border_spin_adjustment.get_value())
        Configuration.pixel_limit(self.pixel_limit_spin_adjustment.get_value())
        Configuration.distance_from_border(self.distance_from_border_spin_adjustment.get_value())

        Configuration.draw_detection_region(self.draw_detection_region_check.get_active())
        Configuration.draw_speed_region(self.draw_speed_region_check.get_active())
        Configuration.draw_cars(self.draw_cars_check.get_active())
        Configuration.draw_conturs(self.draw_conturs_check.get_active())
        Configuration.draw_speed_info(self.draw_speed_info_check.get_active())
        Configuration.draw_size_info(self.draw_size_info_check.get_active())
        Configuration.draw_color_bar(self.draw_color_bar_check.get_active())

        Configuration.save_config()
Example #20
0
avi_files = listdir("/media/Dane/Dropbox/Studia/IV rok/inżynierka/Dane_AVI")

# zapisywanie wyników
db = Database()
img_saver = ImageSaver()

used_files_count = 0
unused_files_count = 0
not_found_files_count = 0
valid_detection_count = 0
bad_detection_count = 0
speed_errors = []
length_errors = []

Logger.start()
Configuration.load_config()


def test_sigle_file(file):
    Algorithm.file = file
    Algorithm.reset()
    input_video = VideoReader(file)
    result = []

    while 1:
        frame = Frame(input_video)

        if not input_video.is_good():
            break

        frame = Algorithm.resize(frame)
 def createConfiguration(self):
     config = Configuration()
     consoleConfig = LoggerConfig()
     config.add('console', consoleConfig)
     return config
Example #22
0
    def __write_settings(self):
        """
        Zapisz wartości z widgetów
        """

        Configuration.distance_border1(self.border1_adjustment.get_value())
        Configuration.distance_border2(self.border2_adjustment.get_value())
        Configuration.meters_length(
            self.meters_length_spin_adjustment.get_value())
        Configuration.color_number(
            self.color_number_spin_adjustment.get_value())
        Configuration.horizontal_border(
            self.horizontal_border_spin_adjustment.get_value())
        Configuration.vertical_border(
            self.vertical_border_spin_adjustment.get_value())
        Configuration.pixel_limit(self.pixel_limit_spin_adjustment.get_value())
        Configuration.distance_from_border(
            self.distance_from_border_spin_adjustment.get_value())

        Configuration.draw_detection_region(
            self.draw_detection_region_check.get_active())
        Configuration.draw_speed_region(
            self.draw_speed_region_check.get_active())
        Configuration.draw_cars(self.draw_cars_check.get_active())
        Configuration.draw_conturs(self.draw_conturs_check.get_active())
        Configuration.draw_speed_info(self.draw_speed_info_check.get_active())
        Configuration.draw_size_info(self.draw_size_info_check.get_active())
        Configuration.draw_color_bar(self.draw_color_bar_check.get_active())

        Configuration.save_config()
Example #23
0
 def __restore_default(self):
     Configuration.restore_default()
     self.__load_settings()
Example #24
0
    def perform(obj: ObjectRecord, file):
        """
        Przeprowadza ocenę koloru, rozmiaru i prędkości obiektu.

        :param ObjectRecord obj: Obiekt wykryty przez klasę Follower.
        :return: Parametry pojazdu.
        :rtype: dict
        """

        # Pobierz dane o pojeździe.
        new_car, old_car, new_frame, old_frame, mask = obj.unpack()

        # Wybierz rejon obrazu:
        x, y, w, h = new_car.get_coordinates()
        image = new_frame.img
        image_roi = new_frame.img[y:y+h, x:x+w, :]
        mask_roi = mask[y:y+h, x:x+w]

        # Wyznaczenie rozmiaru
        car_width = SizeMeasurment.calculate_width(new_car)
        car_height = SizeMeasurment.calculate_height(new_car)
        car_area = SizeMeasurment.calculate_area(mask_roi)

        # Wyznaczenie kolorów
        color_bar, color = ColorDetector.find_color(image_roi)

        # Wyznaczenie prędkości.
        speed = SpeedMeasurment.calculate_speed(new_car, new_frame, old_car, old_frame)

        # Narysowanie wyników na obrazie
        # Kontur
        if Configuration.draw_conturs():
            image = SizeMeasurment.draw_car_contour(image, new_car, mask)
        # Rozmiar
        if Configuration.draw_size_info():
            image = SizeMeasurment.draw_size_info(image, car_width, car_height, car_area)
        # Kolor
        if Configuration.draw_color_bar():
            image = ColorDetector.draw_color_bar(image, color_bar)

        # Prędkość
        if Configuration.draw_speed_info():
            image = SpeedMeasurment.draw_speed_info(new_car, speed, image)

        # Połącz obrazy pojazdu
        image = Classyfication.combine_images(old_frame.orginal_img, image, file)

        # Rekord zawierający informacje o pojeździe
        result = {"width": car_width, "height": car_height, "area": car_area, "speed": speed, "image": image,
                  "date": new_frame.creationTime, "color": color}

        msg = "Przprowadzono klasyfikację pojadu: "
        msg += "długość: %.2f, " % car_width
        msg += "wysokość: %.2f, " % car_height
        msg += "pole karoseri bocznej: %.2f, " % car_area
        msg += "prędkość: %.2f, " % speed
        msg += "data: %s." % str(new_frame.creationTime)

        Logger.info(msg)

        return result
Example #25
0
#!/usr/bin/env python3

__author__ = 'rafal'
__doc__ = 'Skrypt służacy do uruchamiania programu z wiersza poleceń.'

from gi.repository import Gtk
from gui.window_view import ProgramView
from src.logs import Logger
from src.config import Configuration

if __name__ == '__main__':
    Configuration.load_config()
    Logger.start()
    window = ProgramView()
    Logger.info("Uruchamiano główne okna programu.")
    Gtk.main()
Example #26
0
 def on_display_delay_scale_value_changed(self, object, data=None):
     print(self.display_delay_scale_adjustment.get_value())
     Configuration.play_delay(self.display_delay_scale_adjustment.get_value())
Example #27
0
    def __load_settings(self):
        """
        Wczytaj obecną konfigurację znajdującą się config.json do widgetów okna.
        """

        self.border1_adjustment.set_value(Configuration.distance_border1())
        self.border2_adjustment.set_value(Configuration.distance_border2())
        self.meters_length_spin_adjustment.set_value(Configuration.meters_length())
        self.color_number_spin_adjustment.set_value(Configuration.color_number())
        self.horizontal_border_spin_adjustment.set_value(Configuration.horizontal_border())
        self.vertical_border_spin_adjustment.set_value(Configuration.vertical_border())
        self.pixel_limit_spin_adjustment.set_value(Configuration.pixel_limit())
        self.distance_from_border_spin_adjustment.set_value(Configuration.distance_from_border())

        self.draw_detection_region_check.set_active(Configuration.draw_detection_region())
        self.draw_speed_region_check.set_active(Configuration.draw_speed_region())
        self.draw_cars_check.set_active(Configuration.draw_cars())
        self.draw_conturs_check.set_active(Configuration.draw_conturs())
        self.draw_speed_info_check.set_active(Configuration.draw_speed_info())
        self.draw_size_info_check.set_active(Configuration.draw_size_info())
        self.draw_color_bar_check.set_active(Configuration.draw_color_bar())
Example #28
0
    def __load_settings(self):
        """
        Wczytaj obecną konfigurację znajdującą się config.json do widgetów okna.
        """

        self.border1_adjustment.set_value(Configuration.distance_border1())
        self.border2_adjustment.set_value(Configuration.distance_border2())
        self.meters_length_spin_adjustment.set_value(
            Configuration.meters_length())
        self.color_number_spin_adjustment.set_value(
            Configuration.color_number())
        self.horizontal_border_spin_adjustment.set_value(
            Configuration.horizontal_border())
        self.vertical_border_spin_adjustment.set_value(
            Configuration.vertical_border())
        self.pixel_limit_spin_adjustment.set_value(Configuration.pixel_limit())
        self.distance_from_border_spin_adjustment.set_value(
            Configuration.distance_from_border())

        self.draw_detection_region_check.set_active(
            Configuration.draw_detection_region())
        self.draw_speed_region_check.set_active(
            Configuration.draw_speed_region())
        self.draw_cars_check.set_active(Configuration.draw_cars())
        self.draw_conturs_check.set_active(Configuration.draw_conturs())
        self.draw_speed_info_check.set_active(Configuration.draw_speed_info())
        self.draw_size_info_check.set_active(Configuration.draw_size_info())
        self.draw_color_bar_check.set_active(Configuration.draw_color_bar())
Example #29
0
sys.path.insert(1,
                'd:/Nam/Docs/Uni/3_Semester/Visualisierung/code_project/repo')

from pymongo import MongoClient, errors
from bson.objectid import ObjectId

from src.config import Configuration

MONGO_HOST = 'localhost'
MONGO_PORT = 8888
MONGO_USERNAME = '******'
MONGO_PASSWORD = '******'

maxSevSelDelay = 1

_cfg = Configuration()

try:
    client = MongoClient(host='localhost',
                         port=8888,
                         username='******',
                         password='******',
                         serverSelectionTimeoutMS=maxSevSelDelay)

    #logging.info(client.server_info())
    #logging.info(f"+{str(client.list_database_names())}")

except errors.ServerSelectionTimeoutError as err:
    # do whatever you need
    # tryagain later
    print(err)
Example #30
0
from src.config import Configuration

config_ = Configuration()
Example #31
0
 def __restore_default(self):
     Configuration.restore_default()
     self.__load_settings()
Example #32
0
    except KeyboardInterrupt:
        print("Force quitting…")
        sys.exit(1)


if __name__ == '__main__':
    # setup logger
    logging.basicConfig(
        format=u'%(asctime)s [%(levelname)s] <%(name)s> %(message)s',
        level=logging.DEBUG)

    # store initial signal handler
    original_sigint = signal.getsignal(signal.SIGINT)
    signal.signal(signal.SIGINT, exit_gracefully)

    # initialize application
    config = Configuration()
    api = ApiClient(host=config.api_host,
                    port=config.api_port,
                    login=config.api_login,
                    token=config.api_token)
    app = App(client=api)

    # start application
    app.start()

    # wait until application is shut down
    while app.state == AppState.Running:
        wait_signal()
    sys.exit(0)
Example #33
0
 def on_display_delay_scale_value_changed(self, object, data=None):
     print(self.display_delay_scale_adjustment.get_value())
     Configuration.play_delay(
         self.display_delay_scale_adjustment.get_value())