Exemplo n.º 1
0
def get_all_images(dirpath, imgs=None):
    '''
    Prepares images for display in 4 columns on web page
    :param dirpath: Path to folder with images
    :param imgs: List of images. Optional parameter. If set images are taken from this list.
                 If not images are taken from dirpath
    :return: List with four items - each item is another list with almost the same number of images.
    '''
    image_data = []
    if imgs is None:
        imgs = get_all_images_in_dir(dirpath)
        random.shuffle(imgs)
        for img in imgs:
            image_data.append(ImageData(img, 0.0))
    else:
        for img_path, similarity in imgs.items():
            image_data.append(ImageData(os.path.basename(img_path),
                                        similarity))
        image_data = sorted(image_data,
                            key=lambda x: x.similarity,
                            reverse=False)

    image_data = image_data[0:MAX_IMAGES]
    n = len(image_data) // 4
    if len(image_data) % 4 > 1:
        n += 1
    all_imgs = [
        image_data[0:n], image_data[n:2 * n], image_data[2 * n:3 * n],
        image_data[3 * n:]
    ]
    return all_imgs
Exemplo n.º 2
0
    def _get_or_create_datasource(self, name):
        """ Returns the data source associated with the given name, or creates
        it if it doesn't exist.
        """

        if name not in self.datasources:
            data = self.data.get_data(name)

            if type(data) in (list, tuple):
                data = array(data)

            if isinstance(data, ndarray):
                if len(data.shape) == 1:
                    ds = ArrayDataSource(data, sort_order="none")
                elif len(data.shape) == 2:
                    ds = ImageData(data=data, value_depth=1)
                elif len(data.shape) == 3 and data.shape[2] in (3,4):
                    ds = ImageData(data=data, value_depth=int(data.shape[2]))
                else:
                    raise ValueError("Unhandled array shape in creating new "
                                     "plot: %s" % str(data.shape))
            elif isinstance(data, AbstractDataSource):
                ds = data
            else:
                raise ValueError("Couldn't create datasource for data of "
                                 "type %s" % type(data))

            self.datasources[name] = ds

        return self.datasources[name]
    def process_image_for_vehicles(self, image):
        vehicle_heatmap = find_cars(image)
        imageData = ImageData(image, None, None, None, None)
        imageData.set_vehicle_heatmap(vehicle_heatmap)

        self.__save_image_data(imageData)
        self.draw_vehicle_boxes(image)
        return image
Exemplo n.º 4
0
def train_model(source_data_path: str, model_path: str, batch_size: int,
                epochs: int, dropout: float, learning_rate: float,
                weight_decay: float, early_stop_option: bool,
                is_multilabel: bool, split_labels_by: str):
    logger.info('Start data loading')
    data_loader = ImageLoaderFromFolders()
    data = data_loader.load_images_with_labels(source_data_path,
                                               split_labels_by=split_labels_by)
    logger.info('Data loading finished')

    image_data = ImageData(data, 0.7, 0.3, 0)
    n_classes = len(image_data.labels)
    model = get_pretrained_model_for_transfer_learning(n_classes,
                                                       is_multilabel, dropout)
    Learner = get_learner(is_multilabel)
    learner = Learner(model)

    logger.info('Start model training')
    _, _ = learner.fit_model(image_data,
                             image_transforms_training=TransformsTraining,
                             image_transforms_validation=TransformsTest,
                             batch_size=batch_size,
                             epochs=epochs,
                             learning_rate=learning_rate,
                             weight_decay=weight_decay,
                             early_stop_option=early_stop_option)

    save_pickle_file(learner, model_path)
    logger.info('Model training finished')
    logger.info(f'Model saved to path: {model_path}')

    logger.info('Evaluating model performance')
    images = image_data.get_images('validation')
    true_classes = image_data.get_classes('validation')
    predicted_classes, probabilities = learner.predict(images, TransformsTest)

    Interpreter = get_interpreter(is_multilabel)
    interpreter = Interpreter(images, predicted_classes, true_classes,
                              probabilities, learner.class_to_label_mapping)
    accuracy = interpreter.calculate_accuracy()
    accuracy_by_label = interpreter.calculate_accuracy_by_label()
    confusion_matrix = interpreter.calculate_confusion_matrix()

    logger.info(f'Overall accuracy of the model: {accuracy}')
    logger.info(f'Accuracy by label: \n{accuracy_by_label}')
    logger.info(f'Confusion matrix: \n{confusion_matrix}')
Exemplo n.º 5
0
 def train_model(self, data: List[dict]):
     image_data = ImageData(data, self.p_train, self.p_valid, 0.0)
     losses, losses_valid = self.learner.fit_model(
         image_data,
         image_transforms_training=TransformsTraining,
         image_transforms_validation=TransformsTest,
         batch_size=BATCH_SIZE,
         epochs=EPOCHS,
         learning_rate=LEARNING_RATE,
         weight_decay=WEIGHT_DECAY,
         early_stop_option=USE_EARLY_STOP)
     self.min_valid_loss = np.min(losses_valid)
def poly_search(img, left_fit, right_fit):
    # Assume you now have a new warped binary image
    # from the next frame of video (also called "binary_warped")
    # It's now much easier to find line pixels!
    nonzero = img.nonzero()
    nonzeroy = np.array(nonzero[0])
    nonzerox = np.array(nonzero[1])

    left_fit_left = (left_fit[0] * (nonzeroy**2) + left_fit[1] * nonzeroy +
                     left_fit[2] - WINDOW_MARGIN)
    left_fit_right = (left_fit[0] * (nonzeroy**2) + left_fit[1] * nonzeroy +
                      left_fit[2] + WINDOW_MARGIN)

    right_fit_left = (right_fit[0] * (nonzeroy**2) + right_fit[1] * nonzeroy +
                      right_fit[2] - WINDOW_MARGIN)
    right_fit_right = (right_fit[0] * (nonzeroy**2) + right_fit[1] * nonzeroy +
                       right_fit[2] + WINDOW_MARGIN)

    left_lane_inds = ((nonzerox > left_fit_left) & (nonzerox < left_fit_right))
    right_lane_inds = ((nonzerox > right_fit_left) &
                       (nonzerox < right_fit_right))

    imageData = ImageData(img, nonzerox, nonzeroy, left_lane_inds,
                          right_lane_inds)

    try:
        imageData.left_fit_x()
        imageData.right_fit_x()
    except np.linalg.linalg.LinAlgError:
        return None

    return imageData
    def process_image(self, image):
        undist = self.__undistort(image)
        vehicle_heatmap = find_cars(image)

        # binary = self.__transform_to_binary(image)
        # binary_warped = self.__warp_image(binary)

        imageData = ImageData(image, None, None, None, None)
        # imageData = None
        # if (self.__last_image_data):
        #     imageData = poly_search(binary_warped, self.__last_image_data.left_fit(), self.__last_image_data.right_fit())

        # if (not imageData):
        #     imageData = full_search(binary_warped)

        imageData.set_vehicle_heatmap(vehicle_heatmap)

        self.__save_image_data(imageData)

        # result = self.__draw_lane_box(undist, binary_warped)
        # self.draw_vehicle_boxes(result)
        # self.__write_info(result)
        self.draw_vehicle_boxes(image)
        return image
Exemplo n.º 8
0
    async def look_for_image(self):
        # prevent running the function if the AI is running on an image(s) in the accessed directory
        if self.mutex == 2:
            print("waiting for mutex 1")
            return
        # print if the unaccessed directory is empty
        if len(os.listdir(self.unaccess)) == 0:
            print("Unaccessed file empty")
        # run through each image (should be asynchronous, can also make it run through one at a time and have the AI run after each image if this becomes an issue)
        for img in os.listdir(self.unaccess):
            img_path = self.unaccess + "\\" + img  #append the image name to the unaccessed file path
            if img not in img_data:  # make sure we haven't already parsed this image

                self.mutex = 1  # lock the mutex

                #print(img_path)
                data = ImageData(
                    img_path)  #get the image's information ->image_data.py
                #print(data)
                img_name = data.getData()
                gps_calc = GPSCalc(
                )  # instantiate the GPSCalc class -> gps_coord.py
                try:  # prevent images without proper metadata from being parsed
                    lat = self.img_data[img_name][
                        'Latitude']  # latitude that was extracted in image_data.py
                    lon = self.img_data[img_name][
                        'Longitude']  # longitude that was extracted in image_data.py
                    elevation = gps_calc.getElevation(
                        lat, lon)  # get the elevation -> gps_coord.py
                    altitude = self.img_data[img_name][
                        'Altitude']  # # altitude that was extracted in image_data.py
                    #print("elevation")
                    #print(str(elevation))

                    img = PIL.Image.open(
                        img_path
                    )  # open the image in pillow to dynamically get the dimensions

                    #print(img)

                    img_w, img_h = img.size  # image dimensions

                    img.close(
                    )  # close the image - not doing this will cause issues when trying to move the image from one folder to the other later on
                    #print(img_w)
                    #print(img_h)
                    gps = gps_calc.getGPS(
                        img_w, img_h, elevation, lat, lon, altitude, img_path
                    )  # get the GPS value for the center of the image -> gps_coord.py

                    #print("gps")
                    #print(gps)

                    #append the dictionary with the corner gps coordinates
                    self.img_data[img_name]['top_right'] = gps[0]
                    self.img_data[img_name]['top_left'] = gps[1]
                    self.img_data[img_name]['bottom_right'] = gps[2]
                    self.img_data[img_name]['bottom_left'] = gps[3]
                    #print(img_name)
                    img_new = self.access + '\\' + img_name  # new file path for the image since it has been accessed now
                except KeyError:
                    print(
                        "Warning, " + img_name +
                        " does not have appropriate metadata embedded. This image will not be parsed and will be removed from the unaccessed folder."
                    )
                    os.remove(img_path)
                    return
                try:  # prevent issues with stopping the program, restarting and then placing an image in the unaccessed folder that also exists in the accessed folder
                    print("moving " + img_name +
                          " from unaccessed location to accessed location")
                    os.rename(
                        img_path,
                        img_new)  # move this image to the accessed folder
                    self.queue.append(
                        img_new
                    )  # put the image in queue to be accessed by the AI
                    self.mutex = 2
                except FileExistsError:
                    print(
                        "Error, file " + img_name +
                        " already exists in accessed folder, deleting file instead."
                    )
                    os.remove(img_path)
                    pass

            else:  # if the image placed in the unaccessed folder has already been parsed, remove it
                print(
                    img +
                    " has already been accessed. Deleting from unaccessed directory."
                )
                os.remove(img_path)
def full_search(binary_warped):

    debugImage = DebugImage(binary_warped)

    histogram = np.sum(binary_warped[binary_warped.shape[0] / 2:, :], axis=0)

    # Find the peak of the left and right halves of the histogram
    # These will be the starting point for the left and right lines
    midpoint = np.int(histogram.shape[0] / 2)

    leftx_base = np.argmax(histogram[:midpoint])
    rightx_base = np.argmax(histogram[midpoint:]) + midpoint

    # Set height of windows
    window_height = np.int(binary_warped.shape[0] / N_WINDOWS)

    # Identify the x and y positions of all nonzero pixels in the image
    nonzero = binary_warped.nonzero()
    nonzeroy = np.array(nonzero[0])
    nonzerox = np.array(nonzero[1])

    # Current positions to be updated for each window
    leftx_current = leftx_base
    rightx_current = rightx_base

    # Create empty lists to receive left and right lane pixel indices
    left_lane_inds = []
    right_lane_inds = []

    for window in range(N_WINDOWS):
        # Identify window boundaries in x and y (and right and left)
        win_y_low = binary_warped.shape[0] - (window + 1) * window_height
        win_y_high = binary_warped.shape[0] - window * window_height

        win_xleft_low = leftx_current - WINDOW_MARGIN
        win_xleft_high = leftx_current + WINDOW_MARGIN
        win_xright_low = rightx_current - WINDOW_MARGIN
        win_xright_high = rightx_current + WINDOW_MARGIN

        # Draw the windows on the visualization image
        debugImage.draw_rectangle((win_xleft_low, win_y_low),
                                  (win_xleft_high, win_y_high))
        debugImage.draw_rectangle((win_xright_low, win_y_low),
                                  (win_xright_high, win_y_high))

        # Identify the nonzero pixels in x and y within the window
        good_left_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) &
                          (nonzerox >= win_xleft_low) &
                          (nonzerox < win_xleft_high)).nonzero()[0]
        good_right_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) &
                           (nonzerox >= win_xright_low) &
                           (nonzerox < win_xright_high)).nonzero()[0]

        # Append these indices to the lists
        left_lane_inds.append(good_left_inds)
        right_lane_inds.append(good_right_inds)

        # If you found > minpix pixels, recenter next window on their mean position
        if len(good_left_inds) > WINDOW_MIN_PX:
            leftx_current = np.int(np.mean(nonzerox[good_left_inds]))
        if len(good_right_inds) > WINDOW_MIN_PX:
            rightx_current = np.int(np.mean(nonzerox[good_right_inds]))

    # Concatenate the arrays of indices
    left_lane_inds = np.concatenate(left_lane_inds)
    right_lane_inds = np.concatenate(right_lane_inds)

    imageData = ImageData(binary_warped, nonzerox, nonzeroy, left_lane_inds,
                          right_lane_inds)

    debugImage.color_lane(imageData.lefty(), imageData.leftx(), [255, 0, 0])
    debugImage.color_lane(imageData.righty(), imageData.rightx(), [0, 0, 255])
    # debugImage.show_image(imageData.left_fit_x(), imageData.right_fit_x(), imageData.ploty())

    return imageData
Exemplo n.º 10
0
    except:
        print('save failed')


def load(path):
    try:
        model = load_model(path)
        print('Model loaded')
    except:
        print('load failed')


def predict(model, img, img_size):
    img = img.reshape((1, 1, img_size, img_size))
    img = img.astype('float32')
    img = img / 255.0

    result = model.predict_proba(img)
    max_index = np.argmax(result)

    return max_index, result[0][max_index]


if __name__ == '__main__':
    # set dataset
    dataset = ImageData('/Users/songheqi/train_set/')
    model = build_model()
    model = train_model(model, dataset.x_train, dataset.y_train)
    evaluate_model(model, dataset.x_test, dataset.y_test)
    save(model, '/Users/songheqi/model/model2.h5')
Exemplo n.º 11
0
Arquivo: main.py Projeto: ychaim/Final
import cv2
import detector
import bin
import copy
from image_data import ImageData
import characteran
from edgefinder import EdgeFinder
from transformation import Transformation
from textline import TextLine
from segment.ocr import OCR
import numpy as np
from province_detect.state_detector import SateDetector
img = cv2.imread('q.jpeg')
img_data = ImageData(img)
rows, cols = img.shape[:2]
grays, regions = detector.detect(img)
print type(grays[0])
for numberPlate in range(len(grays)):
    img_data.crop_gray = grays[numberPlate]
    img_data.regionOfInterest = regions[numberPlate]
    an_img = copy.copy(img_data.crop_gray)
    img_data.thresholds = bin.produceThresholds(an_img)
    # cv2.imshow("1", img_data.thresholds[0])
    # cv2.imshow("2", img_data.thresholds[1])
    # cv2.imshow("3", img_data.thresholds[2])

    # cv2.imwrite("plate.jpg", img_data.crop_gray)
    # cv2.imwrite("thresh.jpg", img_data.thresholds[0])

    img_data = characteran.characteranalysis(img_data)
    if img_data.disqualified == True: