示例#1
0
def load_frames(video_path="project_video.mp4",
                start_frame=None,
                end_frame=None):
    from moviepy.editor import VideoFileClip
    import cv2
    from util import printProgressBar

    # The file referenced in clip1 is the original video before anything has been done to it
    input = VideoFileClip(video_path)
    #vid_clip = input.fl_image(process_image)

    len_frames = int(input.fps * input.duration)
    len_frames = len_frames if end_frame == None or end_frame > len_frames else end_frame
    i = 0
    # Initial call to print 0% progress
    printProgressBar(0, len_frames, 'Loading frames')
    frames = []
    for frame in input.iter_frames():
        if start_frame == None or i > start_frame:
            frames.append(frame)
            # Update Progress Bar
            printProgressBar(i + 1, len_frames, 'Loading frames')
            if i - 1 >= len_frames:
                break
        i = i + 1

    return frames, input.fps
示例#2
0
def generateMetaData(pathToFeatureSet,
                     fileName,
                     saveDirectory,
                     readDirectory='Data/Normalized Data'):
    #Generate label for meta data
    featureSet = loadFeatureSet(pathToFeatureSet)
    labels = getLables(featureSet)
    labels.append('AuthorName')
    #print(len(labels))
    fw = open(saveDirectory + '/' + fileName + '.tsv', 'w', encoding='UTF-8')
    writeToFile(labels, fw, '	')
    #For each of the Normalized Text get required feature value
    i, l = 0, 0
    authorList = os.listdir(readDirectory)
    for author in authorList:
        fileList = os.listdir(readDirectory + '/' + author)
        l += len(fileList)
    printProgressBar(0, l, prefix='Progress:', suffix='Complete', length=50)
    for author in authorList:
        fileList = os.listdir(readDirectory + '/' + author)
        for fileName in fileList:
            fr = open(readDirectory + '/' + author + '/' + fileName,
                      'r',
                      encoding='UTF-8')
            text = fr.read()
            #print('Author Name:', author, 'File Name:', fileName)
            featureValues = getFeatureValues(featureSet, text)
            featureValues.append(author)
            writeToFile(featureValues, fw, '	')
            printProgressBar(i + 1,
                             l,
                             prefix='Progress:',
                             suffix='Complete',
                             length=50)
            i += 1
示例#3
0
def feature_extract(image, i, size, param):
    """
    Dada uma image, extrai hist, co-matrix e LBP features de acordo com param
    """
    util.printProgressBar(i, size)
    features = []

    for ch in range(3):
        ch_img = image[:, :, ch]
        ch_img = (ch_img / 8)
        ch_img = ch_img.astype(util.np.int8)

        if param[0]:
            hist = util.cv2.calcHist([image], [ch], None, [256], [0, 256])
            features.extend(hist.flatten())

        if param[1]:
            co_matrix = greycomatrix(ch_img, [5], [0],
                                     32,
                                     symmetric=True,
                                     normed=True)
            features.extend(co_matrix.flatten())

        if param[2]:
            features.extend(get_lbp_features(image[:, :, ch], 12, 4))

    return util.np.array(features).ravel().flatten()
示例#4
0
def birds_eye_frames(bin_frames, M, width, height, src):
    import cv2
    import numpy as np
    from util import printProgressBar

    birds_eye_bin_frames = []
    #birds_eye_frames = []
    len_frames = len(frames)
    # Initial call to print 0% progress
    printProgressBar(0, len_frames, 'Birds eye frames')

    for i in range(len_frames):
        bin_frame = bin_frames[i]
        ### outcommented but used for debugging
        #frame = frames[i]

        # Draw red rectangle on frame to show src for transformation
        #pts = np.array(src, np.int32)
        #pts = pts.reshape((-1,1,2))
        #cv2.polylines(frame,[pts],True,(255,0,0))
        #birds_eye_frames.append(frame)

        # Use cv2.warpPerspective() to warp the image to a top-down view
        birds_eye_bin_frame = cv2.warpPerspective(bin_frame, M,
                                                  (width, height))
        birds_eye_bin_frames.append(birds_eye_bin_frame)
        #birds_eye_frame = cv2.warpPerspective(frame, M, (width, height))
        #birds_eye_frames.append(birds_eye_frame)

        # Update Progress Bar
        printProgressBar(i + 1, len_frames, 'Birds eye frames')

    return birds_eye_bin_frames  #, birds_eye_frames
示例#5
0
def color_gradient_frames(frames,
                          s_thresh=(125, 255),
                          sx_thresh=(30, 100),
                          sobel_kernel=3):
    import cv2
    import numpy as np
    from util import printProgressBar

    combined_binaries = []
    color_binaries = []
    len_frames = len(frames)
    # Initial call to print 0% progress
    printProgressBar(0, len_frames, 'Pipeline frames')
    for i in range(len_frames):
        img = np.copy(frames[i])

        # Convert to HLS colorspace
        hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS).astype(np.float)
        l_channel = hls[:, :, 1]
        s_channel = hls[:, :, 2]

        # Find the dark colors in the image to remove them from s and sx
        lower = np.array([0, 0, 0])
        upper = np.array([50, 50, 100])
        mask = cv2.inRange(img, lower, upper)
        black_binary = np.zeros_like(s_channel)
        black_binary[(mask > 0)] = 1

        # Sobelx - takes the derivate in x, absolute value, then rescale
        sobelx = cv2.Sobel(l_channel, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
        abs_sobelx = np.absolute(sobelx)
        scaled_sobelx = np.uint8(255 * abs_sobelx / np.max(abs_sobelx))

        # Threshold x gradient
        sxbinary = np.zeros_like(scaled_sobelx)
        sxbinary[(black_binary == 0) & (scaled_sobelx >= sx_thresh[0]) &
                 (scaled_sobelx <= sx_thresh[1])] = 1

        # Threshold color channel
        s_binary = np.zeros_like(s_channel)
        s_binary[(black_binary == 0) & (s_channel >= s_thresh[0]) &
                 (s_channel <= s_thresh[1])] = 1

        # If two of the three are activated, activate in the binary image
        combined_binary = np.zeros_like(sxbinary)
        combined_binary[(sxbinary == 1) | (s_binary == 1)] = 1
        combined_binaries.append(combined_binary)

        color_binary = np.dstack(
            (np.zeros_like(sxbinary), sxbinary, s_binary)) * 255
        color_binaries.append(color_binary)
        # Update Progress Bar
        printProgressBar(i + 1, len_frames, 'Pipeline frames')
    return combined_binaries, color_binaries
示例#6
0
def etlMain():
    # Find the oldest document inside MongoDB
    date = mongo.findOldest()

    # Determine how many documents we need to transfer
    numDocs = mysql.findOlder(date)
    transferedDocs = 0
    doc = mysql.getFirstDoc(date)
    while doc != None:
        mongo.insertDoc(doc)
        doc = mysql.getNextDoc()
        transferedDocs += 1
        util.printProgressBar(transferedDocs, numDocs)
示例#7
0
def undistort_frames(frames):
    import cv2
    from util import printProgressBar

    undistorted_frames = []
    len_frames = len(frames)
    # Initial call to print 0% progress
    printProgressBar(0, len_frames, 'Undistort frames')
    for i in range(len_frames):
        frame = frames[i]
        undistorted_frames.append(cv2.undistort(frame, mtx, dist, None, mtx))
        # Update Progress Bar
        printProgressBar(i + 1, len_frames, 'Undistort frames')
    return undistorted_frames
示例#8
0
def get_features_list(name,
                      img_paths,
                      orient=9,
                      pix_per_cell=8,
                      cell_per_block=2,
                      vis=False,
                      cspace='RGB',
                      spatial_size=(32, 32),
                      hist_bins=32,
                      hist_range=(0, 256),
                      hog_channel='ALL'):
    features = []
    img_hogs = []
    len_img_paths = len(img_paths)
    util.printProgressBar(0, len_img_paths, name)
    for i in range(len_img_paths):
        img_path = img_paths[i]
        img = mpimg.imread(img_path)
        if vis == True:
            img_features, img_hog = get_features(img,
                                                 orient=orient,
                                                 pix_per_cell=pix_per_cell,
                                                 cell_per_block=cell_per_block,
                                                 vis=vis,
                                                 cspace=cspace,
                                                 spatial_size=spatial_size,
                                                 hist_bins=hist_bins,
                                                 hist_range=hist_range,
                                                 hog_channel=hog_channel)
            img_hogs.append(img_hog)
        else:
            img_features = get_features(img,
                                        orient=orient,
                                        pix_per_cell=pix_per_cell,
                                        cell_per_block=cell_per_block,
                                        vis=vis,
                                        cspace=cspace,
                                        spatial_size=spatial_size,
                                        hist_bins=hist_bins,
                                        hist_range=hist_range,
                                        hog_channel=hog_channel)
        features.append(img_features)
        util.printProgressBar(i + 1, len_img_paths, name)
    if vis == True:
        return features, img_hogs

    return features
示例#9
0
    def hard_negative_mine(self, folder, epochs):
        # this function is used to  train svm by hard negative mining
        #initializing svm with default weights of positive sample means
        self.initialize_svm(folder)
        util.printProgressBar(0, epochs, prefix='Starting', suffix='complete')
        for epoch in range(epochs):
            # STEP 1: Mine for negative samples
            for i, img in enumerate(self.imgs):
                boxes, scores = self.edgebox.getproposals(img)
                if not isinstance(
                        boxes, tuple):  #because if boxes is tuple, its empty
                    boxes = util.cv2_to_numpy(boxes)
                    boxes, scores = self.filter_negsamples(boxes, scores, i)
                    if (boxes.shape[0] >
                            self.n):  #only do it if more than n samples
                        boxes, scores, _ = util.topn(boxes, scores, self.n)
                    if (boxes.shape[0] >
                            0):  #only do it some boxes survive the journey
                        boxes, scores, _ = util.nms(boxes, scores,
                                                    self.overlap_thresh)
                        self.neg_rects.append(boxes.tolist())
            #    else:
            #        print("no boxes detected!")

            # STEP 2: Add those samples into dataset
            self.populate_data(True)
            self.populate_data(False)

            # STEP 3: Prepare data
            X, y = self.prepare_data()

            # STEP 4: train the svm
            self.train_svm(X, y)
            util.printProgressBar(epoch + 1,
                                  epochs,
                                  prefix='Epoch %d' % (epoch + 1),
                                  suffix='complete')
        print('Training successfully finished after %d epochs' % epochs)
示例#10
0
    for fil in onlyfiles:
        # cargamos el archivo
        npy = np.load(os.path.join(dataset_path, str(fil)), allow_pickle=True)
        # separamos datos y etiquetas
        img, label = npy

        if label[0] < min_value:
            min_value = label[0]
        if label[0] > max_value:
            max_value = label[0]

        i = i + 1
        printProgressBar(i,
                         len(onlyfiles),
                         prefix='Buscando maximo y minimo:',
                         suffix='Completado (' + C +
                         str(psutil.virtual_memory()[2]) + B + '% RAM)',
                         length=100,
                         color=45)

print("\nMaximo: " + C + str(max_value) + B)
print("Minimo: " + C + str(min_value) + B + "\n")

#lista para guardar labels
label_list = []

#para cada .npy
i = 0
for fil in onlyfiles:
    # cargamos el archivo
    npy = np.load(os.path.join(dataset_path, str(fil)), allow_pickle=True)
示例#11
0
        targetv = (torch.from_numpy(np.argmax(big_target, axis = 1)).long()).cuda()
        trainingv, valv = torch.split(trainingv, int(trainingv.size()[0] * (1 - val_split)))
        trainingv_sv, valv_sv = torch.split(trainingv_sv, int(trainingv_sv.size()[0] * (1 - val_split)))
        targetv, val_targetv = torch.split(targetv, int(targetv.size()[0] * (1 - val_split)))
        samples_random = np.random.choice(range(len(trainingv)), valv.size()[0]/100)

        for j in range(0, trainingv.size()[0], batch_size):
            optimizer.zero_grad()
            out = gnn(trainingv[j:j + batch_size].cuda(), trainingv_sv[j:j + batch_size].cuda())
            l = loss(out, targetv[j:j + batch_size].cuda())
            l.backward()
            optimizer.step()
            loss_string = "Loss: %s" % "{0:.5f}".format(l.item())
            util.printProgressBar(j + batch_size, trainingv.size()[0], 
                                  prefix = "%s [%s/%s] " % (loss_string, 
                                                           j + batch_size, 
                                                           trainingv.size()[0]),
                                  length = 20)
        
        del trainingv, training_sv, targetv, valv, valv_sv, val_targetv
        
    for sub_X,sub_Y in data.generate_data():
        training = sub_X[2]
        training_sv = sub_X[3]
        target = sub_Y[0]

        # Training Set
        n_targets = target.shape[1]
        samples = [get_sample_train(training, training_sv, target, i) for i in range(n_targets)]
        trainings = [i[0] for i in samples]
        trainings_sv = [i[1] for i in samples]
示例#12
0
def getDailyAvgPlot():

    num_images = 0

    # contamos las imagenes totales
    for json_number, json_name in enumerate(
            glob.glob(os.path.join(saving_path, '*.json'))):

        f = open(json_name)
        j = json.load(f)

        i = 0
        for key in j:
            num_images = num_images + 1
            i += 1
        print("Numero del Json: " + V + str(json_number) + B)
        print("Imagenes en el Json: " + V + str(i) + B, end='\n\n')

    print(attr(4) + "Imagenes Totales:" + attr(0) + " " + V + str(num_images) +
          B,
          end='\n\n')

    i2 = 0
    label_list = []
    num_lamb = 0
    num_no_lamb = 0
    num_fail = 0
    daily_weight = {}
    one_for_day_done = False

    asker = ClasiLamb_2_1_ModelAsker()
    asker.loadModel(os.path.join(parent_folder, lamb_model_path))

    # para todos los .json de labels
    for json_number, json_name in enumerate(
            glob.glob(os.path.join(saving_path, '*.json'))):

        f = open(json_name)
        j = json.load(f)
        json_key_name = json_name[json_name.rfind("/") +
                                  1:json_name.rfind(".") - 1]
        daily_weight[json_key_name] = []

        # estructuramos las imagenes del json
        for key in j:
            # Control para recoger solo un peso por dia (debug de plot)
            if just_one_for_day:
                if one_for_day_done == False:
                    one_for_day_done = True
                else:
                    one_for_day_done = False
                    break
            # Comprobacion de maximo de imagenes. -1 para que no haya limite
            if max_out_img_num >= 0:
                if num_lamb == max_out_img_num:
                    break

            # cargamos cada imagen
            img_path = raw_dataset_path + str(
                j[key]["path_depth_default_image"])
            img = cv2.imread(img_path, flags=cv2.IMREAD_ANYDEPTH)

            # comprobamos si la imagen existe y ha sido cargada correctamente (si no, saltamos a la siguiente)
            try:
                if len(img) == 0:
                    print("[!] Error al cargar la imagen: " + V +
                          str(img_path) + B)
                    exit()
            except:
                num_fail += 1
                i2 += 1
                continue

            # preparamos la imagen para ser normalizada posteriormente (todo pixel que supere OVERRIDE_MAXVALUE se queda en OVERRIDE_MAXVALUE)
            if OVERRIDE_MAXVALUE != -1:
                img = np.clip(img, None, OVERRIDE_MAXVALUE)
            """
            # le hacemos crop
            x = 38
            y = 102
            h = 230
            w = 510
            img = img[y:y + h, x:x + w]
            """

            # invertimos la imagen
            # if OVERRIDE_MAXVALUE != -1:
            #   img = OVERRIDE_MAXVALUE-img

            # Normalizamos
            #img = img/OVERRIDE_MAXVALUE

            # Preguntamos a la red si la imagen es correcta
            # Si es correcta, la red sacara un 0, si no, sacara un 1
            res, val = asker.ask(img)
            if res == 1.0:
                is_correct_lamb = False
                one_for_day_done = False
            elif res == 0.0:
                is_correct_lamb = True

            # Si la imagen es correcta guardamos la imagen y la etiqueta, si no, descartamos
            if is_correct_lamb:

                # Acumulamos el peso en su dia, en funcion del nombre del json
                daily_weight[json_key_name].append(float(j[key]["weight"]))

                num_lamb += 1

            else:
                num_no_lamb += 1

            i2 += 1
            printProgressBar(i2,
                             num_images,
                             prefix='Estructurando JSON ' + str(json_number) +
                             ':',
                             suffix='Completado',
                             length=50,
                             color=33)

    # Procesamos el json en dos listas para pasarselo a la plot

    x_values = [str(key) for key in daily_weight.keys()]
    x_values.sort(key=lambda v: datetime.strptime(v, '%Y-%m-%d'),
                  reverse=False)

    y_values = []
    for k in x_values:
        try:
            y_values.append(mean(daily_weight[k]))
        except:
            y_values.append(0)

    # Montamos la plot
    color_list = [
        "deepskyblue", "aquamarine", "limegreen", "yellow", "gold",
        "darkorange", "lightcoral", "orchid", "mediumpurple"
    ]

    num_lambs = 1

    fig, axs = plt.subplots(num_lambs)

    i = 0

    bars = axs.bar(x_values, y_values, color=color_list[2])
    autolabel(bars, axs)

    plt.sca(axs)
    plt.xticks(rotation=-40, ha="left", size=8)
    plt.xticks(x_values)
    plt.ylabel("Peso [Kg]")
    plt.title("Peso Medio Diario", size=20)

    plt.show()

    y_values = []
    for k in x_values:
        y_values.append(len(daily_weight[k]))

    # Montamos la plot de cantidades
    color_list = [
        "deepskyblue", "aquamarine", "limegreen", "yellow", "gold",
        "darkorange", "lightcoral", "orchid", "mediumpurple"
    ]

    num_lambs = 1

    fig, axs = plt.subplots(num_lambs)

    i = 0

    bars = axs.bar(x_values, y_values, color=color_list[1])
    autolabel(bars, axs)

    plt.sca(axs)
    plt.xticks(rotation=-40, ha="left", size=8)
    plt.xticks(x_values)
    plt.ylabel("Numero de Imagenes")
    plt.title("Numero de Imagenes Diarias", size=20)

    plt.show()
示例#13
0
def getDailyAvgMultiPlot():

    num_images = 0

    # contamos las imagenes totales
    for json_number, json_name in enumerate(
            glob.glob(os.path.join(saving_path, '*.json'))):

        f = open(json_name)
        j = json.load(f)

        i = 0
        for key in j:
            num_images = num_images + 1
            i += 1
        print("Numero del Json: " + V + str(json_number) + B)
        print("Imagenes en el Json: " + V + str(i) + B, end='\n\n')

    print(attr(4) + "Imagenes Totales:" + attr(0) + " " + V + str(num_images) +
          B,
          end='\n\n')

    i2 = 0
    label_list = []
    num_lamb = 0
    num_no_lamb = 0
    num_fail = 0
    daily_weight = {}
    one_for_day_done = False

    asker = ClasiLamb_2_1_ModelAsker()
    asker.loadModel(os.path.join(parent_folder, lamb_model_path))

    # para todos los .json de labels
    for json_number, json_name in enumerate(
            glob.glob(os.path.join(saving_path, '*.json'))):

        f = open(json_name)
        j = json.load(f)
        json_key_name = json_name[json_name.rfind("/") +
                                  1:json_name.rfind(".") - 1]
        daily_weight[json_key_name] = []

        # estructuramos las imagenes del json
        for key in j:
            # Control para recoger solo un peso por dia (debug de plot)
            if just_one_for_day:
                if one_for_day_done == False:
                    one_for_day_done = True
                else:
                    one_for_day_done = False
                    break
            # Comprobacion de maximo de imagenes. -1 para que no haya limite
            if max_out_img_num >= 0:
                if num_lamb == max_out_img_num:
                    break

            # cargamos cada imagen
            img_path = raw_dataset_path + str(
                j[key]["path_depth_default_image"])
            img = cv2.imread(img_path, flags=cv2.IMREAD_ANYDEPTH)

            # comprobamos si la imagen existe y ha sido cargada correctamente (si no, saltamos a la siguiente)
            try:
                if len(img) == 0:
                    print("[!] Error al cargar la imagen: " + V +
                          str(img_path) + B)
                    exit()
            except:
                num_fail += 1
                i2 += 1
                continue

            # preparamos la imagen para ser normalizada posteriormente (todo pixel que supere OVERRIDE_MAXVALUE se queda en OVERRIDE_MAXVALUE)
            if OVERRIDE_MAXVALUE != -1:
                img = np.clip(img, None, OVERRIDE_MAXVALUE)
            """
            # le hacemos crop
            x = 38
            y = 102
            h = 230
            w = 510
            img = img[y:y + h, x:x + w]
            """

            # invertimos la imagen
            # if OVERRIDE_MAXVALUE != -1:
            #   img = OVERRIDE_MAXVALUE-img

            # Normalizamos
            #img = img/OVERRIDE_MAXVALUE

            # Preguntamos a la red si la imagen es correcta
            # Si es correcta, la red sacara un 0, si no, sacara un 1
            res, val = asker.ask(img)
            if res == 1.0:
                is_correct_lamb = False
                one_for_day_done = False
            elif res == 0.0:
                is_correct_lamb = True

            # Si la imagen es correcta guardamos la imagen y la etiqueta, si no, descartamos
            if is_correct_lamb:

                # Acumulamos el peso en su dia, en funcion del nombre del json
                daily_weight[json_key_name].append(float(j[key]["weight"]))

                num_lamb += 1

            else:
                num_no_lamb += 1

            i2 += 1
            printProgressBar(i2,
                             num_images,
                             prefix='Estructurando JSON ' + str(json_number) +
                             ':',
                             suffix='Completado',
                             length=50,
                             color=33)

    # Procesamos el json en dos listas para pasarselo a la plot
    # Pasamos de YYYY-MM-DD a DD-MM-YYYY

    x_values = [str(key) for key in daily_weight.keys()]

    x_values.sort(key=lambda v: datetime.strptime(v, '%Y-%m-%d'),
                  reverse=False)

    y_values = []
    for k in x_values:
        try:
            y_values.append(mean(daily_weight[k]))
        except:
            y_values.append(0)

    # Cortamos las listas en trocitos que correspondan a los periodos de tiempo seguidos
    long_x_list = [
        datetime.strptime(x, '%Y-%m-%d').strftime('%d-%m-%Y') for x in x_values
    ]
    long_y_list = y_values

    past_date = None
    all_json = {"0": {"x": [], "y": []}}
    current_set_int_key = 0
    for i in range(len(long_x_list)):
        tandas_diferentes = False
        current_date = long_x_list[i]

        # Obtenemos los datos de fecha y comparamos
        if past_date == None:
            # Primera fecha, no comprobamos nada
            tandas_diferentes = False
        else:
            # Comparamos fechas, as ver si se difieren en diff_days dias
            time_diff = datetime.strptime(current_date,
                                          '%d-%m-%Y') - datetime.strptime(
                                              past_date, '%d-%m-%Y')
            if time_diff.days >= 10:
                tandas_diferentes = True
            else:
                tandas_diferentes = False

        if tandas_diferentes:
            # Si este peso es de tanda diferente, terminamos esta
            # tanda y preparamos la siguiente
            current_set_int_key += 1
            all_json[str(current_set_int_key)] = {"x": [], "y": []}

        # Acumulamos un dia mas y preparamos el siguiente dia
        all_json[str(current_set_int_key)]["x"].append(current_date)
        all_json[str(current_set_int_key)]["y"].append(long_y_list[i])

        past_date = current_date

    # Montamos la plot de cantidades
    color_list = [
        "deepskyblue", "aquamarine", "limegreen", "yellow", "gold",
        "darkorange", "lightcoral", "orchid", "mediumpurple"
    ]

    num_lambs = len(all_json)

    fig, axs = plt.subplots(num_lambs)

    for i, key in enumerate(all_json):

        x_values = all_json[key]["x"]
        y_values = all_json[key]["y"]

        key_number = int(str(key))
        bars = axs[i].bar(x_values, y_values, color=color_list[i])
        autolabel(bars, axs[i])

        plt.sca(axs[i])
        plt.xticks(rotation=-40, ha="left", size=8)
        plt.ylabel("Peso [Kg]")
        plt.title("Tanda de pesos " + str(key), size=20)

    plt.show()

    fig.set_size_inches((20, 10 * num_lambs))

    fig.savefig("plot.png", dpi=300)
示例#14
0
def find_draw_lanes(bin_frames, frames, M):
    from util import printProgressBar
    import numpy as np
    import cv2
    from line import Line

    found_lanes_frames = []
    drawed_lanes_frames = []

    # Left and right line
    left_line = Line()
    right_line = Line()

    # Inverted M to transform image backwards
    Minv = np.linalg.inv(M)

    len_bin_frames = len(bin_frames)
    # Initial call to print 0% progress
    printProgressBar(0, len_bin_frames, 'Finding & drawing lane frames')

    for i in range(len_bin_frames):
        bin_frame = bin_frames[i]
        frame = frames[i]

        # Assuming you have created a warped binary image called "bin_frame"
        # Take a histogram of the bottom half of the image
        histogram = np.sum(bin_frame[int(bin_frame.shape[0] / 2):, :], axis=0)
        # Create an output image to draw on and  visualize the result
        out_img = np.dstack((bin_frame, bin_frame, bin_frame)) * 255
        # 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] * 3 / 4)
        left_line.all_x_base = np.argmax(histogram[:midpoint])
        rightx_base = np.argmax(histogram[midpoint:]) + midpoint
        # Choose the number of sliding windows
        nwindows = 9
        # Set height of windows
        window_height = np.int(bin_frame.shape[0] / nwindows)
        # Identify the x and y positions of all nonzero pixels in the image
        nonzero = bin_frame.nonzero()
        nonzeroy = np.array(nonzero[0])
        nonzerox = np.array(nonzero[1])
        # Current positions to be updated for each window
        left_line.all_x_current = left_line.all_x_base
        rightx_current = rightx_base
        # Set the width of the windows +/- margin
        margin = 100
        # Set minimum number of pixels found to recenter window
        minpix = 50
        # Create empty lists to receive left and right lane pixel indices
        left_lane_inds = []
        right_lane_inds = []

        # Step through the windows one by one
        for window in range(nwindows):
            # Identify window boundaries in x and y (and right and left)
            win_y_low = bin_frame.shape[0] - (window + 1) * window_height
            win_y_high = bin_frame.shape[0] - window * window_height
            win_xleft_low = left_line.all_x_current - margin
            win_xleft_high = left_line.all_x_current + margin
            win_xright_low = rightx_current - margin
            win_xright_high = rightx_current + margin
            # Draw the windows on the visualization image
            cv2.rectangle(out_img, (win_xleft_low, win_y_low),
                          (win_xleft_high, win_y_high), (0, 255, 0), 2)
            cv2.rectangle(out_img, (win_xright_low, win_y_low),
                          (win_xright_high, win_y_high), (0, 255, 0), 2)
            # 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) > minpix:
                left_line.all_x_current = np.int(
                    np.mean(nonzerox[good_left_inds]))
            if len(good_right_inds) > minpix:
                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)

        # Extract left and right line pixel positions
        left_line.all_x = nonzerox[left_lane_inds]
        left_line.all_y = nonzeroy[left_lane_inds]
        rightx = nonzerox[right_lane_inds]
        righty = nonzeroy[right_lane_inds]

        # Fit a second order polynomial to each
        left_fit = np.polyfit(left_line.all_y, left_line.all_x, 2)
        right_fit = np.polyfit(righty, rightx, 2)

        ploty = np.linspace(0, bin_frame.shape[0] - 1, bin_frame.shape[0])
        left_fitx = left_fit[0] * ploty**2 + left_fit[1] * ploty + left_fit[2]
        left_line.addXfitted(left_fitx)
        right_fitx = right_fit[0] * ploty**2 + right_fit[
            1] * ploty + right_fit[2]
        right_line.addXfitted(right_fitx)

        out_img[nonzeroy[left_lane_inds],
                nonzerox[left_lane_inds]] = [255, 0, 0]
        out_img[nonzeroy[right_lane_inds],
                nonzerox[right_lane_inds]] = [0, 0, 255]

        # Draw left line
        for x, y in zip(left_line.best_fit, ploty):
            y = int(y)
            x = int(x)
            if (x >= 0 and x < 1280 and y >= 0 and y < 720):
                out_img[y, x] = [255, 255, 0]

        # Draw left line
        for x, y in zip(right_line.best_fit, ploty):
            y = int(y)
            x = int(x)
            if (x >= 0 and x < 1280 and y >= 0 and y < 720):
                out_img[y, x] = [255, 255, 0]

        # Define y-value where we want radius of curvature
        y_eval = np.max(ploty)

        # Define conversions in x and y from pixels space to meters
        ym_per_pix = 30 / 720  # meters per pixel in y dimension
        xm_per_pix = 3.7 / 700  # meters per pixel in x dimension

        # Fit new polynomials to x,y in world space
        left_fit_cr = np.polyfit(left_line.all_y * ym_per_pix,
                                 left_line.all_x * xm_per_pix, 2)
        right_fit_cr = np.polyfit(righty * ym_per_pix, rightx * xm_per_pix, 2)
        # Calculate the new radii of curvature
        left_line.radius_of_curvature = (
            (1 +
             (2 * left_fit_cr[0] * y_eval * ym_per_pix + left_fit_cr[1])**2)**
            1.5) / np.absolute(2 * left_fit_cr[0])
        right_line.radius_of_curvature = (
            (1 +
             (2 * right_fit_cr[0] * y_eval * ym_per_pix + right_fit_cr[1])**2)
            **1.5) / np.absolute(2 * right_fit_cr[0])

        found_lanes_frames.append(out_img)

        # Create an image to draw the lines on
        warp_zero = np.zeros_like(frame).astype(np.uint8)
        color_warp = np.dstack((warp_zero, warp_zero, warp_zero))

        # Recast the x and y points into usable format for cv2.fillPoly()
        pts_left = np.array(
            [np.transpose(np.vstack([left_line.best_fit, ploty]))])
        pts_right = np.array(
            [np.flipud(np.transpose(np.vstack([right_line.best_fit, ploty])))])
        pts = np.hstack((pts_left, pts_right))

        # Draw the lane onto the warped blank image
        cv2.fillPoly(warp_zero, np.int_([pts]), (0, 255, 0))

        # Warp the blank back to original image space using inverse perspective matrix (Minv)
        newwarp = cv2.warpPerspective(warp_zero, Minv,
                                      (frame.shape[1], frame.shape[0]))
        # Combine the result with the original image
        result = cv2.addWeighted(frame, 1, newwarp, 0.3, 0)

        # Draw our radius of curvature is in meters on the result
        cv2.putText(
            result, 'Left curverad:  {:5d}m'.format(
                np.int(left_line.radius_of_curvature)), (10, 30),
            cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 165, 0), 2, cv2.LINE_AA)
        cv2.putText(
            result, 'Right curverad: {:5d}m'.format(
                np.int(right_line.radius_of_curvature)), (10, 70),
            cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 165, 0), 2, cv2.LINE_AA)
        cv2.putText(
            result, 'Diff. curverad: {:5d}m'.format(
                np.abs(
                    np.int(left_line.radius_of_curvature) -
                    np.int(right_line.radius_of_curvature))), (10, 110),
            cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 165, 0), 2, cv2.LINE_AA)

        height = frame.shape[0]
        width = frame.shape[1]
        car_position = width / 2
        l_fit_x_int = left_fit[0] * height**2 + left_fit[
            1] * height + left_fit[2]
        r_fit_x_int = right_fit[0] * height**2 + right_fit[
            1] * height + right_fit[2]
        lane_center_position = (r_fit_x_int + l_fit_x_int) / 2
        center_dist = (car_position - lane_center_position) * xm_per_pix
        if center_dist < 0:
            cv2.putText(
                result, 'Vehicle is {0:.2f}m left of center'.format(
                    np.abs(center_dist)), (10, 150), cv2.FONT_HERSHEY_SIMPLEX,
                1, (255, 165, 0), 2, cv2.LINE_AA)
        if center_dist > 0:
            cv2.putText(
                result, 'Vehicle is {0:.2f}m right of center'.format(
                    np.abs(center_dist)), (10, 150), cv2.FONT_HERSHEY_SIMPLEX,
                1, (255, 165, 0), 2, cv2.LINE_AA)

        drawed_lanes_frames.append(result)

        # Update Progress Bar
        printProgressBar(i + 1, len_bin_frames,
                         'Finding & drawing lane frames')

    return found_lanes_frames, drawed_lanes_frames
示例#15
0
env = gym.make('Taxi-v3')
QLearner = QLearningAgent.QLearningAgent(env.observation_space, env.action_space, epsilon=0.3, alpha=0.2, discount=0.9)

num_episodes = 1000
for episode in range(0, num_episodes):
    observation = env.reset()
    while True:
        action = QLearner.getAction(observation)
        next_observation, reward, done, info = env.step(action)
        QLearner.update(observation, action, next_observation, reward)
        observation = next_observation
        if done:
            break
    
    if episode % (num_episodes / 100) == 0:
        util.printProgressBar(episode, num_episodes)

print("DONE TRAINING")

for episode in range(0, 4):
    observation = env.reset()
    env.render()
    for i in range(0, 1000):
        action = QLearner.getAction(observation, False)
        next_observation, reward, done, info = env.step(action)
        env.render()
        observation = next_observation
        if done:
            print("Success!")
            print("")
            print("-------------------------------------------")
示例#16
0
def calc_distortion(camera_cal_img_path='camera_cal',
                    camera_cal_img_file_type='jpg',
                    nx=9,
                    ny=6,
                    debug=False,
                    write_undistorted_output=False):
    import numpy as np
    import glob
    import cv2
    from util import printProgressBar

    # Storing image path and image in a map together
    imagesMap = {}

    # Storing object points and image points
    obj_points = []
    img_points = []

    # Prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(8,5,0)
    objp = np.zeros((nx * ny, 3), np.float32)
    objp[:, :2] = np.mgrid[0:nx, 0:ny].T.reshape(-1, 2)

    img_paths = glob.glob(camera_cal_img_path + '/*.' +
                          camera_cal_img_file_type)
    len_img_paths = len(img_paths)

    printProgressBar(0, len_img_paths, 'Calculate camera distortion')
    # Looping over each image path in provided path for camera calculation images
    for i in range(len_img_paths):
        img_path = img_paths[i]
        if (debug):
            print("Reading chessboard image: {}".format(img_path))
        img = cv2.imread(img_path)
        imagesMap[img_path] = img
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        ret, corners = cv2.findChessboardCorners(gray, (nx, ny), None)
        if ret == True:
            if (debug):
                print("Found chessboard corners for {}".format(img_path))
            obj_points.append(objp)
            img_points.append(corners)
            cv2.drawChessboardCorners(img, (nx, ny), corners, ret)
        printProgressBar(i + 1, len_img_paths, 'Calculate camera distortion')

    # Camera calibration
    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points,
                                                       gray.shape[::-1], None,
                                                       None)

    # Output undistorted images to output_images folder
    if (write_undistorted_output):
        for img_path, img in imagesMap.items():
            new_img_path = img_path.replace(camera_cal_img_path,
                                            'output_images')
            if (debug):
                print("Writing undistorted chessboard image: {}".format(
                    new_img_path))
            undst_img = cv2.undistort(img, mtx, dist, None, mtx)
            cv2.imwrite(new_img_path, undst_img)

    return mtx, dist
def detect_vehicles(svc, frames, windows_list, orient, pix_per_cell,
                    cell_per_block, cspace, spatial_size, hist_bins,
                    hist_range):
    enhanced_frames = []
    len_frames = len(frames)
    util.printProgressBar(0, len_frames, 'Detecting vehicles')
    for i in range(len_frames):
        frame = frames[i]
        vehicle_windows = []
        for j in range(len(windows_list)):
            for k in range(len(windows_list[j])):
                window = windows_list[j][k]
                #(startx, starty), (endx, endy)
                startx = window[0][0]
                starty = window[0][1]
                endx = window[1][0]
                endy = window[1][1]
                diffx = endx - startx
                diffy = endy - starty
                diffxy = min(diffx, diffy)
                window_frame = frame[starty:starty + diffxy,
                                     startx:startx + diffxy]
                window_frame_resize = cv2.resize(window_frame, (64, 64))
                window_frame_resize_features = features.get_features(
                    window_frame_resize,
                    orient=orient,
                    pix_per_cell=pix_per_cell,
                    cell_per_block=cell_per_block,
                    cspace=cspace,
                    spatial_size=spatial_size,
                    hist_bins=hist_bins,
                    hist_range=hist_range)
                predictions = svc.predict([window_frame_resize_features])
                for prediction in predictions:
                    if prediction == 1:
                        vehicle_windows.append(window)

        threshold = 1
        heatmap, heat_windows = generate_heatmap(frame, vehicle_windows,
                                                 threshold)

        for car in cars:
            car.not_found_count += 1

        veh_windows = []
        for heat_window in heat_windows:
            left = heat_window[0][0]
            top = heat_window[0][1]
            right = heat_window[1][0]
            bottom = heat_window[1][1]
            center_h = left + int((right - left) / 2)
            center_v = top + int((bottom - top) / 2)
            center = (center_h, center_v)
            car_exists = False
            for car in cars:
                if car.is_fit(center):
                    car.add_center(center)
                    if car.found_count >= COUNT_FRAMES_DRAW_CAR:
                        veh_windows.append(car.avg_window)
                    car_exists = True
                    break

            if not car_exists:
                car = Car()
                cars.append(car)
                car.add_center(center)

        for car in cars:
            if car.not_found_count >= COUNT_FRAMES_DELETE_CARS:
                cars.remove(car)

        frame = lessons.draw_boxes(frame,
                                   veh_windows,
                                   color=(0, 0, 255),
                                   thick=3)

        enhanced_frames.append(frame)
        util.printProgressBar(i + 1, len_frames, 'Detecting vehicles')
    return enhanced_frames
示例#18
0
def getErrorPlot():

    # dataset (normalizamos las etiquetas entre 0 y 35 para los pesos
    dataset_loader = CLDL_r1(dataset_path,
                             img_transform=None,
                             manual_label_normalize=None)

    # Cargamos el modelo CNN
    asker = ClasiLamb_2_2_ModelAsker()
    asker.loadModel(weight_model_path)

    # Preparamos los Datos para hacer la plot. Para ello, leemos uno a uno todos los
    # elementos del data loader, guardamos el label y pasamos la imagen por la red.
    # Nos quedamos con el peso estimado tambien y lo guardamos todo en dos listas
    # separadas, pero cada indice relaciona el peso y la prediccion
    labels = []
    preds = []
    j = 0
    for x, y in dataset_loader:
        prediction = asker.ask(x) * 35
        # print("label: ", y, "    pred: ", prediction, "         diff: ", abs(y-prediction))
        labels.append(y)
        preds.append(prediction)

        printProgressBar(j,
                         len(dataset_loader),
                         prefix='Obteniendo predicciones de la red: ',
                         suffix='Completado',
                         length=50,
                         color=33)
        j += 1

        if j == 15211:
            break

    print(len(labels), " entradas en labels_and_preds.")
    print("y type: ", type(y))
    print("prediction type ", type(prediction))

    # Procesamos la estructura anterior creando las dos listas finales para la plot.
    # Estas son, una lista de labels (los kilogramos discretos, Ej: 14, 15, 16) y
    # una lista de valores, que seran la media de errores de los pesos que entren
    # dentro de esa categoria. Cada peso entra dentro de la categoria que tenga
    # mas cerca (Ej: en la categoria 16Kg entran desde 15.50 hasta 16.49)

    # Lista de valores discretos
    x_values = [
        i for i in range(int(round(min(labels), 0)),
                         int(round(max(labels), 0)) + 1)
    ]

    print(min(labels))
    print(max(labels))
    print(round(min(labels)))
    print(round(max(labels)))

    print(x_values)

    # Acumulador de errores en cada categoria de peso
    acum = [[] for i in range(len(x_values))]

    # Para cada peso en label, obtenemos su redondeo sin decimales, obtenemos el
    # idx de la categoria en la que iria y añadimos EL ERROR (diferencia) a la
    # lista de esa categoria
    for i, p in enumerate(labels):
        int_p = round(p, 0)
        cat_idx = x_values.index(int_p)
        acum[cat_idx].append(abs(p - preds[i]))

    print("Categoria  :  Nº de pesos en la categoria")
    for i in range(len(x_values)):
        print(x_values[i], " : ", len(acum[i]))

    # Metemos en el idx de cada categoria la media de pesos
    y_values = [[] for i in range(len(x_values))]

    for i in range(len(x_values)):
        try:
            y_values[i] = mean(acum[i])
        except:
            y_values[i] = 0

    print(y_values)

    # Montamos la plot
    color_list = [
        "deepskyblue", "aquamarine", "limegreen", "yellow", "gold",
        "darkorange", "lightcoral", "orchid", "mediumpurple"
    ]

    num_lambs = 1

    fig, axs = plt.subplots(num_lambs)

    i = 0

    bars = axs.bar(x_values, y_values, color=color_list[5])
    autolabel(bars, axs)

    plt.sca(axs)
    plt.xticks(rotation=-40, ha="left", size=8)
    plt.xticks(x_values)
    plt.ylabel("Error [Kg]")
    plt.title("Error segun el peso", size=20)

    plt.show()
示例#19
0
            #   img = OVERRIDE_MAXVALUE-img

            if not os.path.exists(dest_path):
                os.makedirs(dest_path)
            cv2.imwrite(os.path.join(dest_path,
                                     str(num_lamb) + ".png"), np.uint16(img))

            # guardamos la etiqueta
            label = j[key]["weight"]
            label_list.append(label)

            num_lamb += 1

        i2 += 1
        printProgressBar(i2,
                         num_images,
                         prefix='Estructurando JSON ' + str(json_number) + ':',
                         suffix='Completado',
                         length=100,
                         color=45)

# guardamos las etiquetas
if not os.path.exists(dest_path):
    os.makedirs(dest_path)
d = open(os.path.join(dest_path, "labels.npy"), "wb+")
np.save(d, np.array(label_list), allow_pickle=True)
d.close()

print("\n\nEl dataset resultado se compone de " + V + str(num_lamb) + B +
      " imagenes.\n\n")
    # Set this parameter to True when you're done with algorithm development:
    done_tweaking = False

    # Since we seed the randomness, we can avoid executing on the training set
    # again for some extra speed
    train_model = True

    '''
    Make predictions on the training set.
    '''
    if train_model:
        print("Running predictions on training set")
        preds_train = {}
        n = len(file_names_train)
        printProgressBar(0, n, prefix='Progress:', suffix='Complete', length=50)
        for i in range(len(file_names_train)):

            # read image using PIL:
            I = Image.open(os.path.join(data_path,file_names_train[i]))

            # convert to numpy array:
            I = np.asarray(I)

            preds_train[file_names_train[i]] = detect_red_light_mf(I)

            # Intermediate saves just incase
            if (i+1) % 50 == 0:
                # print("Writing to preds_train_{}.json".format(i+1))
                with open(os.path.join(preds_path,'preds_train_{}.json'.format(i+1)),'w') as f:
                    json.dump(preds_train,f)
示例#21
0
    # gradient backprop & optimize ONLY G's parameters
    G_loss.backward()
    G_optimizer.step()

    return G_loss.data.item()


def generate_image(epoch):
    with torch.no_grad():
        test_z = Variable(torch.randn(batch_size, z_dim))
        generated = G(test_z)
        save_image(generated.view(generated.size(0), 1, 28, 28),
                   f'./samples/sample_{epoch}.png')


n_epoch = 200

for epoch in range(1, n_epoch + 1):
    D_losses, G_losses = [], []
    for batch_idx, (x, _) in enumerate(train_loader):
        printProgressBar(batch_idx, len(train_loader),
                         f'Treinando - Epoch {epoch}')
        D_losses.append(D_train(x))
        G_losses.append(G_train(x))

    print('\n[%d/%d]: loss_d: %.3f, loss_g: %.3f' %
          ((epoch), n_epoch, torch.mean(torch.FloatTensor(D_losses)),
           torch.mean(torch.FloatTensor(G_losses))))

    generate_image(epoch)