<jupyter_start><jupyter_text>9517 Proj  1. Import library<jupyter_code>import pandas as pd
import cv2
import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVC, LinearSVC
from sklearn.model_selection import train_test_split
import pickle
import itertools

# need to delete
from skimage import feature as ft<jupyter_output><empty_output><jupyter_text>2. Define functions to extract features<jupyter_code>def get_color_hist(image, nbins, bins_range):
    '''
    Functions to extract histogram features
    '''
    # compute histogram from R, G, B 3-dim
    R_hist = np.histogram(image[:, :, 0], bins = nbins, range = bins_range)
    G_hist = np.histogram(image[:, :, 1], bins = nbins, range = bins_range)
    B_hist = np.histogram(image[:, :, 2], bins = nbins, range = bins_range)
    # concatenate the histograms to a single histogram features
    hist_features = np.concatenate((R_hist[0], G_hist[0], B_hist[0]))
    return hist_features
def get_hog_feature(image):
    '''
    Functions to extract hog features
    '''
    features, hog_image = ft.hog(image, orientations=12, pixels_per_cell=[20, 20], cells_per_block=[2,2], visualize = True, transform_sqrt= False)
    return features, hog_image
def extract_features(image):
    features = []
    hog_features = []
    # hog feature
    for channel in range(image.shape[2]):
        fea, hog_image = get_hog_feature(image[:, :, channel])
        hog_features.append(fea)
    hog_features = np.ravel(hog_features)
    features.append(hog_features)
    # histogram feature
    nbins = 32
    bin_range = (0, 256)
    features.append(get_color_hist(image, nbins, bin_range))
    return np.concatenate(features)<jupyter_output><empty_output><jupyter_text>3. Prepare dataset (train and test)<jupyter_code>def slice_window(img, xy_window=(200, 100), xy_overlap=(0.2, 0.2)):
    x_start_stop = [0, img.shape[1]]
    y_start_stop = [0, img.shape[0]]
    # count the size of slice window
    x_size = x_start_stop[1] - x_start_stop[0]
    y_size = y_start_stop[1] - y_start_stop[0]
    # count the num of pixel need for one step
    x_pix_per_step = np.int(xy_window[0] * (1 - xy_overlap[0]))
    y_pix_per_step = np.int(xy_window[1] * (1 - xy_overlap[1]))
    # count the num of windows
    x_buffer = np.int(xy_window[0] * (xy_overlap[0]))
    y_buffer = np.int(xy_window[1] * (xy_overlap[1]))
    x_windows = np.int((x_size - x_buffer) / x_pix_per_step)
    y_windows = np.int((y_size - y_buffer) / y_pix_per_step)
    
    # count 
    window_list = []
    print('slicing windows....')
    for y in range(y_windows):
        for x in range(x_windows):
            startx = x * x_pix_per_step + x_start_stop[0]
            endx = startx + xy_window[0]
            starty = y * y_pix_per_step + y_start_stop[0]
            endy = starty + xy_window[1]
            window_list.append(((startx, starty), (endx, endy)))
    return window_list
def get_data(basePath, clips_size, labelType):
    '''
    labelType: 1 as positive -1 as negtive
    '''
    train_data = []
    for index in range(1, clips_size + 1):
        imgs = []
        if index % 10 == 0:
            print('processing on', index)
        # store all train image into list
        img_dir = basePath + str(index) + '/imgs/040.jpg'
        gt_dir = basePath + str(index) + '/annotation.json'
        labels = get_ground_truth(gt_dir)
        

        img = cv2.imread(img_dir)
        if labelType == -1:
            min_left =  9999
            min_top = 9999
            max_right = 0
            max_bottom = 0
            # extract nocar image
            for label in labels:
                left = int(label['left'])
                top = int(label['top'])
                right = int(label['right'])
                bottom = int(label['bottom'])

                min_left = min(left, min_left)
                min_top = min(top, min_top)
                max_right = max(right, max_right)
                max_bottom = max(bottom, max_bottom)
            
            img_left_top = img[0:min_top, 0:min_left]
            img_right_bottom = img[max_bottom:720, max_right:1280]

            imgs.append(img_left_top)
            imgs.append(img_right_bottom)

            # imgs.append(new_img)
            # 1280 * 750
        else:
            for label in labels:
                left = int(label['left'])
                right = int(label['right'])
                top = int(label['top'])
                bot = int(label['bottom'])
                tmp_img = img[top:bot, left:right]
                imgs.append(tmp_img)
        
        for img in imgs:
            img = cv2.resize(img, (320, 180))
            feature_img = extract_features(img)
            train_data.append(feature_img)
            # store all labels into list
            train_data = list(itertools.chain(train_data))
    return train_data
def get_supp_data(basePath):
    train_data = []
    for index in range(1, 9):
        imgs = []
        if index % 10 == 0:
            print('processing on', index)
        # store all train image into list
        img_dir = basePath + 'supp_img/000' + str(index) + '.jpg'
        gt_dir = basePath + '/annotation.json'
        labels = get_supp_truth(gt_dir, index)

        img = cv2.imread(img_dir)
        for label in labels[0]:
            left = int(label['left'])
            right = int(label['right'])
            top = int(label['top'])
            bot = int(label['bottom'])
            tmp_img = img[top:bot, left:right]
            imgs.append(tmp_img)
        
        for img in imgs:
            img = cv2.resize(img, (320, 180))
            feature_img = extract_features(img)
            train_data.append(feature_img)
            # store all labels into list
            train_data = list(itertools.chain(train_data))
    return train_data


def get_train_dataset(clips_size):
    basePath = './data/train/clips/'
    print("car......")
    car_train_data = get_data(basePath, clips_size, 1)
    print("non-car......")
    noncar_train_data = get_data(basePath, int(clips_size / 2), -1)
    train_data = np.vstack((car_train_data, noncar_train_data))
    labels = np.hstack((np.ones(len(car_train_data)), np.zeros(len(noncar_train_data))))

    # add supp pic data
    # supp_path = './data/supp/'
    # supp_data = get_supp_data(supp_path)
    # train_data = np.vstack((train_data, supp_data))
    # labels = np.hstack((labels, np.ones(len(supp_data))))
    
    return train_data, labels
def get_test_dataset(clips_size):
    basePath = './data/test/clips/'
    print("car......")
    car_train_data = get_data(basePath, clips_size, 1)
    print("non-car......")
    noncar_train_data = get_data(basePath, int(clips_size / 2), -1)
    train_data = np.vstack((car_train_data, noncar_train_data))
    labels = np.hstack((np.ones(len(car_train_data)), np.zeros(len(noncar_train_data))))
    return np.array(train_data), labels
def get_ground_truth(filepath):
    labels = []
    gt = pd.read_json(filepath)
    list = gt['bbox'].tolist()
    for car in list:
        labels.append(car)
    return labels
def get_supp_truth(filepath, index):
    labels = []
    gt = pd.read_json(filepath)

    gt = gt[gt['file_name']=='supp_img/000' + index + '.jpg']
    list = gt['bbox'].tolist()
    for car in list:
        labels.append(car)
    return labels<jupyter_output><empty_output><jupyter_text>2. SVM construction<jupyter_code>train_data, train_labels = get_train_dataset(100)
train_num = len(train_labels)
print("test..........")
test_data, test_labels = get_test_dataset(30)
def svm_train(train_data, train_labels, test_data, test_labels):
    # import svc model
    svc = LinearSVC()
    # fit the data
    svc.fit(train_data, train_labels)
    with open('model', 'wb') as f:
        pickle.dump(svc, f)
    
    test_acc = svc.score(test_data, test_labels)
    print('Test Accuracy: {}'.format(round(test_acc, 4)))

    return svc

svm_train(train_data, train_labels, test_data, test_labels)<jupyter_output>Test Accuracy: 0.9559
C:\Users\wjh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sklearn\svm\_base.py:986: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.
  "the number of iterations.", ConvergenceWarning)<jupyter_text>3. Descide Whether Have Cars Based On  The Model<jupyter_code>def search_window(img, clf, xy_window=(200, 100), xy_overlap=(0.8, 0.8)):
    windows = slice_window(img, xy_window, xy_overlap)

    on_windows = []
    for window in windows:
        new_img = img[window[0][1]:window[1][1], window[0][0]:window[1][0]]
        # print(window)
        # print(window)
        # plt.imshow(new_img)
        # plt.show()
        new_img = cv2.resize(new_img, (320, 180))
        features = extract_features(new_img).reshape(1, -1)
        pred = clf.predict(features)
        if pred == 1:
            on_windows.append(window)

    return on_windows
img_dir = './data/test/clips/52/imgs/040.jpg'
img = cv2.imread(img_dir)
dist_pickle = pickle.load(open("model", "rb"))

# img = cv2.resize(img, (320, 180))
# test_img = img[0:320, 0:180]
# plt.imshow(test_img)
# plt.show()
# test_img = cv2.resize(test_img, (320, 180))
# features = extract_features(test_img).reshape(1, -1)
# res = dist_pickle.predict(features)
# print(res)


on_windows = search_window(img, dist_pickle, xy_window=(150, 150), xy_overlap=(0, 0))
for window in on_windows:
    cv2.rectangle(img, (window[0][0], window[0][1]), (window[1][0], window[1][1]), (0,0,255), 2)

# print(on_windows)
plt.imshow(img)
plt.show()<jupyter_output>slicing windows....
{'fraction_non_alphanumeric': 0.12931214718795736, 'fraction_numerical': 0.02446163495713987, 'mean_word_length': 2.8100358422939067, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '152000', 'license': 'Apache-2.0', 'n_tokens_mistral': 3495, 'n_tokens_neox': 3284, 'n_words': 694, 'path': 'COMP9517/95/proj.ipynb', 'repo_name': 'Spacider/UNSW_2021T1', 'size': '9566'}