
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
import time 
import pickle

def preprocessImage(image):
    

    #convert opencv numpy array to PIL image
    
    #PADDING
        
    #get size of image
    x,y = image.size

    #get maximum one to extend other edge to same length to make image square
    square_edge = max(x,y)

    #create just black image with size of square_edge X square_edge
    padded_image = Image.new('RGB', (square_edge, square_edge), (0,0,0))


    #copy available image into black square image to get padded version of image
    #In Python 3, to get an integer result from a division you need to use // instead of /
    up_left_coordinate = ((square_edge - x) // 2, (square_edge - y) // 2)


    #Note: in this library, cartesian pixel coordinate system with (0,0) in the upper left corner is used
    # so, to centre old image on square padded image, corresponding upperleft coordinate would be obtained by the above formula

    padded_image.paste(image, up_left_coordinate)

    image = padded_image




    #RESCALING TO 224 X 224

    RESCALE_SIZE = 224


    #utilize resize function of pillow library
    rescaled_image = image.resize((RESCALE_SIZE,RESCALE_SIZE))
    image = rescaled_image



    #CONVERT IMAGES INTO NUMPY ARRAY BEFORE NORMALIZATION


    image = np.asarray(image).astype(np.float32)


    

    #DO NORMALIZATION
    image = np.true_divide(image,255)

    image[:,:,0] = np.subtract(image[:,:,0],0.485)
    image[:,:,1] = np.subtract(image[:,:,1],0.456)
    image[:,:,2] = np.subtract(image[:,:,2],0.406)

    image[:,:,0] = np.true_divide(image[:,:,0],0.229)
    image[:,:,1] = np.true_divide(image[:,:,1],0.224)
    image[:,:,2] = np.true_divide(image[:,:,2],0.225)

    processed_image = image



    return processed_image

#function ends ------------------------



#TESTING


import glob

import cv2 as cv
import numpy as np
import sys

import torch
from resnet import resnet50
from sklearn import preprocessing
from PIL import Image



model = resnet50(pretrained=True)
model.eval()

#READ TEST IMAGES
test_images = []

start = time.time()


#load models
svm_classifiers = []
model_pathes= ['model1.sav','model2.sav','model3.sav','model4.sav','model5.sav','model6.sav','model7.sav','model8.sav','model9.sav','model10.sav']
for model_path in model_pathes:

    loaded_model = pickle.load(open(model_path, 'rb'))
    svm_classifiers.append(loaded_model)




#read all images under the test/images folder
i = range(0,100)
for image_path in i:
    
    #read image
    image = cv.imread("test/images/"+str(image_path)+".JPEG") # load an image_path
    
   

    #convert image to rgb
    image = cv.cvtColor(image, cv.COLOR_BGR2RGB)

    #add image to list
    test_images.append(image)   



# 1.Extract candidate windows

modelPath = "model.yml"
windows = []
i = 1
#for each image extract window
for image in test_images:

    edge_detection = cv.ximgproc.createStructuredEdgeDetection(modelPath)
    rgb_im = cv.cvtColor(image, cv.COLOR_BGR2RGB)
    edges = edge_detection.detectEdges(np.float32(rgb_im) / 255.0)

    orimap = edge_detection.computeOrientation(edges)
    edges = edge_detection.edgesNms(edges, orimap)

    edge_boxes = cv.ximgproc.createEdgeBoxes()
    edge_boxes.setMaxBoxes(50)

    #get boxes values consist of x,y,width,height
    boxes = edge_boxes.getBoundingBoxes(edges, orimap)

    windows.append(boxes)
    



# 2.Classification and localization
optimum_windows = np.asarray([],dtype=np.int8)
predictions = np.asarray([],dtype=np.int8)


#for each image 
for i in range(0,len(windows)):

    test_image = test_images[i]

    candidate_windows = windows[i]

    


    windows_predictions = np.asarray([],dtype=np.int8)
    windows_probabilities= np.asarray([]) 
    a = []
    #for each window
    for j in range(0,len(candidate_windows)):

        #get coordinates and length values
        a = candidate_windows[j]
        x, y, w, h = candidate_windows[j]

        #crop image to window obtained by box edge method
        #Note: opencv images are stored in numpy array, top left point corresponds to 0,0 coordinate
        cropped_image = test_image[y:y+h,x:x+w,:]
        #convert opencv numpy array to PIL image
        cropped_image = Image.fromarray(cropped_image.astype('uint8'), 'RGB')

        cropped_image = preprocessImage(cropped_image)

        # we append an augmented dimension to indicate batch_size, which is one
        cropped_image = np.reshape(cropped_image, [1, 224, 224, 3])


        # model takes as input images of size [batch_size, 3, im_height, im_width]
        cropped_image = np.transpose(cropped_image, [0, 3, 1, 2])
        #(For the second arguement) i in the j-th place in the tuple means a’s i-th axis becomes a.transpose()’s j-th axis.
        #e.g. shape of the new array would be [1,3,224,224]


        # convert the Numpy image to torch.FloatTensor
        tensor_image = torch.from_numpy(cropped_image)



        # extract features
        feature_vector = model(tensor_image)


        # convert the features of type torch.FloatTensor to a Numpy array
        # so that you can either work with them within the sklearn environment
        # or save them as .mat files


        feature_vector.data.cpu().numpy()

        feature_vector = feature_vector.data.numpy()
        #Note: the version placed in the project document ends up with an error


        #before adding list, normalize feature vector
        feature_vector = preprocessing.normalize(feature_vector,norm='l2')

        feature_vector = np.squeeze(feature_vector)


    
        classifier_scores = np.asarray([])
        
        #apply each of binary svm classifier to window
        for classifier in svm_classifiers:
            
            #get window score for each binary classifier            
            classifier_scores = np.append(classifier_scores,classifier.predict_log_proba(feature_vector.reshape(1,-1))[0][1])
            
            #Note: (error)array.reshape(1, -1) if it contains a single sample
    
        window_prediction = np.argmax(classifier_scores)+1  
        window_probability = np.amax(classifier_scores)
        
        windows_predictions = np.append(windows_predictions,window_prediction)
        windows_probabilities = np.append(windows_probabilities,window_probability)

    image_prediction = windows_predictions[np.argmax(windows_probabilities)]
   
    optimum_window = candidate_windows[np.argmax(windows_probabilities)]
    
    print(optimum_window)

    optimum_window = np.reshape(optimum_window,4)

    optimum_windows = np.append(optimum_windows,[optimum_window])
    
    predictions = np.append(predictions,image_prediction) 


label_dictionary = {1:'n01615121', 2:'n02099601', 3:'n02123159',4:'n02129604', 5:'n02317335', 6:'n02391049', 7:'n02410509', 8:'n02422699', 9:'n02481823',10:'n02504458'}


prediction_labels = [label_dictionary[label] for label in predictions]

optimum_windows = optimum_windows.astype(int).reshape(len(test_images),4)

np.savetxt("windows.txt",optimum_windows,delimiter = ',')
with open('predictions.txt', 'w') as f:
    for item in prediction_labels:
        f.write("%s\n" % item)








end = time.time()

print("total execution time : " + str(end - start))
06d40bf36a1883ddd7320ed8da2c0fb01079f3af
<mask token>


def preprocessImage(image):
    x, y = image.size
    square_edge = max(x, y)
    padded_image = Image.new('RGB', (square_edge, square_edge), (0, 0, 0))
    up_left_coordinate = (square_edge - x) // 2, (square_edge - y) // 2
    padded_image.paste(image, up_left_coordinate)
    image = padded_image
    RESCALE_SIZE = 224
    rescaled_image = image.resize((RESCALE_SIZE, RESCALE_SIZE))
    image = rescaled_image
    image = np.asarray(image).astype(np.float32)
    image = np.true_divide(image, 255)
    image[:, :, 0] = np.subtract(image[:, :, 0], 0.485)
    image[:, :, 1] = np.subtract(image[:, :, 1], 0.456)
    image[:, :, 2] = np.subtract(image[:, :, 2], 0.406)
    image[:, :, 0] = np.true_divide(image[:, :, 0], 0.229)
    image[:, :, 1] = np.true_divide(image[:, :, 1], 0.224)
    image[:, :, 2] = np.true_divide(image[:, :, 2], 0.225)
    processed_image = image
    return processed_image


<mask token>

<mask token>


def preprocessImage(image):
    x, y = image.size
    square_edge = max(x, y)
    padded_image = Image.new('RGB', (square_edge, square_edge), (0, 0, 0))
    up_left_coordinate = (square_edge - x) // 2, (square_edge - y) // 2
    padded_image.paste(image, up_left_coordinate)
    image = padded_image
    RESCALE_SIZE = 224
    rescaled_image = image.resize((RESCALE_SIZE, RESCALE_SIZE))
    image = rescaled_image
    image = np.asarray(image).astype(np.float32)
    image = np.true_divide(image, 255)
    image[:, :, 0] = np.subtract(image[:, :, 0], 0.485)
    image[:, :, 1] = np.subtract(image[:, :, 1], 0.456)
    image[:, :, 2] = np.subtract(image[:, :, 2], 0.406)
    image[:, :, 0] = np.true_divide(image[:, :, 0], 0.229)
    image[:, :, 1] = np.true_divide(image[:, :, 1], 0.224)
    image[:, :, 2] = np.true_divide(image[:, :, 2], 0.225)
    processed_image = image
    return processed_image


<mask token>
model.eval()
<mask token>
for model_path in model_pathes:
    loaded_model = pickle.load(open(model_path, 'rb'))
    svm_classifiers.append(loaded_model)
<mask token>
for image_path in i:
    image = cv.imread('test/images/' + str(image_path) + '.JPEG')
    image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
    test_images.append(image)
<mask token>
for image in test_images:
    edge_detection = cv.ximgproc.createStructuredEdgeDetection(modelPath)
    rgb_im = cv.cvtColor(image, cv.COLOR_BGR2RGB)
    edges = edge_detection.detectEdges(np.float32(rgb_im) / 255.0)
    orimap = edge_detection.computeOrientation(edges)
    edges = edge_detection.edgesNms(edges, orimap)
    edge_boxes = cv.ximgproc.createEdgeBoxes()
    edge_boxes.setMaxBoxes(50)
    boxes = edge_boxes.getBoundingBoxes(edges, orimap)
    windows.append(boxes)
<mask token>
for i in range(0, len(windows)):
    test_image = test_images[i]
    candidate_windows = windows[i]
    windows_predictions = np.asarray([], dtype=np.int8)
    windows_probabilities = np.asarray([])
    a = []
    for j in range(0, len(candidate_windows)):
        a = candidate_windows[j]
        x, y, w, h = candidate_windows[j]
        cropped_image = test_image[y:y + h, x:x + w, :]
        cropped_image = Image.fromarray(cropped_image.astype('uint8'), 'RGB')
        cropped_image = preprocessImage(cropped_image)
        cropped_image = np.reshape(cropped_image, [1, 224, 224, 3])
        cropped_image = np.transpose(cropped_image, [0, 3, 1, 2])
        tensor_image = torch.from_numpy(cropped_image)
        feature_vector = model(tensor_image)
        feature_vector.data.cpu().numpy()
        feature_vector = feature_vector.data.numpy()
        feature_vector = preprocessing.normalize(feature_vector, norm='l2')
        feature_vector = np.squeeze(feature_vector)
        classifier_scores = np.asarray([])
        for classifier in svm_classifiers:
            classifier_scores = np.append(classifier_scores, classifier.
                predict_log_proba(feature_vector.reshape(1, -1))[0][1])
        window_prediction = np.argmax(classifier_scores) + 1
        window_probability = np.amax(classifier_scores)
        windows_predictions = np.append(windows_predictions, window_prediction)
        windows_probabilities = np.append(windows_probabilities,
            window_probability)
    image_prediction = windows_predictions[np.argmax(windows_probabilities)]
    optimum_window = candidate_windows[np.argmax(windows_probabilities)]
    print(optimum_window)
    optimum_window = np.reshape(optimum_window, 4)
    optimum_windows = np.append(optimum_windows, [optimum_window])
    predictions = np.append(predictions, image_prediction)
<mask token>
np.savetxt('windows.txt', optimum_windows, delimiter=',')
with open('predictions.txt', 'w') as f:
    for item in prediction_labels:
        f.write('%s\n' % item)
<mask token>
print('total execution time : ' + str(end - start))

<mask token>


def preprocessImage(image):
    x, y = image.size
    square_edge = max(x, y)
    padded_image = Image.new('RGB', (square_edge, square_edge), (0, 0, 0))
    up_left_coordinate = (square_edge - x) // 2, (square_edge - y) // 2
    padded_image.paste(image, up_left_coordinate)
    image = padded_image
    RESCALE_SIZE = 224
    rescaled_image = image.resize((RESCALE_SIZE, RESCALE_SIZE))
    image = rescaled_image
    image = np.asarray(image).astype(np.float32)
    image = np.true_divide(image, 255)
    image[:, :, 0] = np.subtract(image[:, :, 0], 0.485)
    image[:, :, 1] = np.subtract(image[:, :, 1], 0.456)
    image[:, :, 2] = np.subtract(image[:, :, 2], 0.406)
    image[:, :, 0] = np.true_divide(image[:, :, 0], 0.229)
    image[:, :, 1] = np.true_divide(image[:, :, 1], 0.224)
    image[:, :, 2] = np.true_divide(image[:, :, 2], 0.225)
    processed_image = image
    return processed_image


<mask token>
model = resnet50(pretrained=True)
model.eval()
test_images = []
start = time.time()
svm_classifiers = []
model_pathes = ['model1.sav', 'model2.sav', 'model3.sav', 'model4.sav',
    'model5.sav', 'model6.sav', 'model7.sav', 'model8.sav', 'model9.sav',
    'model10.sav']
for model_path in model_pathes:
    loaded_model = pickle.load(open(model_path, 'rb'))
    svm_classifiers.append(loaded_model)
i = range(0, 100)
for image_path in i:
    image = cv.imread('test/images/' + str(image_path) + '.JPEG')
    image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
    test_images.append(image)
modelPath = 'model.yml'
windows = []
i = 1
for image in test_images:
    edge_detection = cv.ximgproc.createStructuredEdgeDetection(modelPath)
    rgb_im = cv.cvtColor(image, cv.COLOR_BGR2RGB)
    edges = edge_detection.detectEdges(np.float32(rgb_im) / 255.0)
    orimap = edge_detection.computeOrientation(edges)
    edges = edge_detection.edgesNms(edges, orimap)
    edge_boxes = cv.ximgproc.createEdgeBoxes()
    edge_boxes.setMaxBoxes(50)
    boxes = edge_boxes.getBoundingBoxes(edges, orimap)
    windows.append(boxes)
optimum_windows = np.asarray([], dtype=np.int8)
predictions = np.asarray([], dtype=np.int8)
for i in range(0, len(windows)):
    test_image = test_images[i]
    candidate_windows = windows[i]
    windows_predictions = np.asarray([], dtype=np.int8)
    windows_probabilities = np.asarray([])
    a = []
    for j in range(0, len(candidate_windows)):
        a = candidate_windows[j]
        x, y, w, h = candidate_windows[j]
        cropped_image = test_image[y:y + h, x:x + w, :]
        cropped_image = Image.fromarray(cropped_image.astype('uint8'), 'RGB')
        cropped_image = preprocessImage(cropped_image)
        cropped_image = np.reshape(cropped_image, [1, 224, 224, 3])
        cropped_image = np.transpose(cropped_image, [0, 3, 1, 2])
        tensor_image = torch.from_numpy(cropped_image)
        feature_vector = model(tensor_image)
        feature_vector.data.cpu().numpy()
        feature_vector = feature_vector.data.numpy()
        feature_vector = preprocessing.normalize(feature_vector, norm='l2')
        feature_vector = np.squeeze(feature_vector)
        classifier_scores = np.asarray([])
        for classifier in svm_classifiers:
            classifier_scores = np.append(classifier_scores, classifier.
                predict_log_proba(feature_vector.reshape(1, -1))[0][1])
        window_prediction = np.argmax(classifier_scores) + 1
        window_probability = np.amax(classifier_scores)
        windows_predictions = np.append(windows_predictions, window_prediction)
        windows_probabilities = np.append(windows_probabilities,
            window_probability)
    image_prediction = windows_predictions[np.argmax(windows_probabilities)]
    optimum_window = candidate_windows[np.argmax(windows_probabilities)]
    print(optimum_window)
    optimum_window = np.reshape(optimum_window, 4)
    optimum_windows = np.append(optimum_windows, [optimum_window])
    predictions = np.append(predictions, image_prediction)
label_dictionary = {(1): 'n01615121', (2): 'n02099601', (3): 'n02123159', (
    4): 'n02129604', (5): 'n02317335', (6): 'n02391049', (7): 'n02410509',
    (8): 'n02422699', (9): 'n02481823', (10): 'n02504458'}
prediction_labels = [label_dictionary[label] for label in predictions]
optimum_windows = optimum_windows.astype(int).reshape(len(test_images), 4)
np.savetxt('windows.txt', optimum_windows, delimiter=',')
with open('predictions.txt', 'w') as f:
    for item in prediction_labels:
        f.write('%s\n' % item)
end = time.time()
print('total execution time : ' + str(end - start))

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
import time
import pickle


def preprocessImage(image):
    x, y = image.size
    square_edge = max(x, y)
    padded_image = Image.new('RGB', (square_edge, square_edge), (0, 0, 0))
    up_left_coordinate = (square_edge - x) // 2, (square_edge - y) // 2
    padded_image.paste(image, up_left_coordinate)
    image = padded_image
    RESCALE_SIZE = 224
    rescaled_image = image.resize((RESCALE_SIZE, RESCALE_SIZE))
    image = rescaled_image
    image = np.asarray(image).astype(np.float32)
    image = np.true_divide(image, 255)
    image[:, :, 0] = np.subtract(image[:, :, 0], 0.485)
    image[:, :, 1] = np.subtract(image[:, :, 1], 0.456)
    image[:, :, 2] = np.subtract(image[:, :, 2], 0.406)
    image[:, :, 0] = np.true_divide(image[:, :, 0], 0.229)
    image[:, :, 1] = np.true_divide(image[:, :, 1], 0.224)
    image[:, :, 2] = np.true_divide(image[:, :, 2], 0.225)
    processed_image = image
    return processed_image


import glob
import cv2 as cv
import numpy as np
import sys
import torch
from resnet import resnet50
from sklearn import preprocessing
from PIL import Image
model = resnet50(pretrained=True)
model.eval()
test_images = []
start = time.time()
svm_classifiers = []
model_pathes = ['model1.sav', 'model2.sav', 'model3.sav', 'model4.sav',
    'model5.sav', 'model6.sav', 'model7.sav', 'model8.sav', 'model9.sav',
    'model10.sav']
for model_path in model_pathes:
    loaded_model = pickle.load(open(model_path, 'rb'))
    svm_classifiers.append(loaded_model)
i = range(0, 100)
for image_path in i:
    image = cv.imread('test/images/' + str(image_path) + '.JPEG')
    image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
    test_images.append(image)
modelPath = 'model.yml'
windows = []
i = 1
for image in test_images:
    edge_detection = cv.ximgproc.createStructuredEdgeDetection(modelPath)
    rgb_im = cv.cvtColor(image, cv.COLOR_BGR2RGB)
    edges = edge_detection.detectEdges(np.float32(rgb_im) / 255.0)
    orimap = edge_detection.computeOrientation(edges)
    edges = edge_detection.edgesNms(edges, orimap)
    edge_boxes = cv.ximgproc.createEdgeBoxes()
    edge_boxes.setMaxBoxes(50)
    boxes = edge_boxes.getBoundingBoxes(edges, orimap)
    windows.append(boxes)
optimum_windows = np.asarray([], dtype=np.int8)
predictions = np.asarray([], dtype=np.int8)
for i in range(0, len(windows)):
    test_image = test_images[i]
    candidate_windows = windows[i]
    windows_predictions = np.asarray([], dtype=np.int8)
    windows_probabilities = np.asarray([])
    a = []
    for j in range(0, len(candidate_windows)):
        a = candidate_windows[j]
        x, y, w, h = candidate_windows[j]
        cropped_image = test_image[y:y + h, x:x + w, :]
        cropped_image = Image.fromarray(cropped_image.astype('uint8'), 'RGB')
        cropped_image = preprocessImage(cropped_image)
        cropped_image = np.reshape(cropped_image, [1, 224, 224, 3])
        cropped_image = np.transpose(cropped_image, [0, 3, 1, 2])
        tensor_image = torch.from_numpy(cropped_image)
        feature_vector = model(tensor_image)
        feature_vector.data.cpu().numpy()
        feature_vector = feature_vector.data.numpy()
        feature_vector = preprocessing.normalize(feature_vector, norm='l2')
        feature_vector = np.squeeze(feature_vector)
        classifier_scores = np.asarray([])
        for classifier in svm_classifiers:
            classifier_scores = np.append(classifier_scores, classifier.
                predict_log_proba(feature_vector.reshape(1, -1))[0][1])
        window_prediction = np.argmax(classifier_scores) + 1
        window_probability = np.amax(classifier_scores)
        windows_predictions = np.append(windows_predictions, window_prediction)
        windows_probabilities = np.append(windows_probabilities,
            window_probability)
    image_prediction = windows_predictions[np.argmax(windows_probabilities)]
    optimum_window = candidate_windows[np.argmax(windows_probabilities)]
    print(optimum_window)
    optimum_window = np.reshape(optimum_window, 4)
    optimum_windows = np.append(optimum_windows, [optimum_window])
    predictions = np.append(predictions, image_prediction)
label_dictionary = {(1): 'n01615121', (2): 'n02099601', (3): 'n02123159', (
    4): 'n02129604', (5): 'n02317335', (6): 'n02391049', (7): 'n02410509',
    (8): 'n02422699', (9): 'n02481823', (10): 'n02504458'}
prediction_labels = [label_dictionary[label] for label in predictions]
optimum_windows = optimum_windows.astype(int).reshape(len(test_images), 4)
np.savetxt('windows.txt', optimum_windows, delimiter=',')
with open('predictions.txt', 'w') as f:
    for item in prediction_labels:
        f.write('%s\n' % item)
end = time.time()
print('total execution time : ' + str(end - start))


import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
import time 
import pickle

def preprocessImage(image):
    

    #convert opencv numpy array to PIL image
    
    #PADDING
        
    #get size of image
    x,y = image.size

    #get maximum one to extend other edge to same length to make image square
    square_edge = max(x,y)

    #create just black image with size of square_edge X square_edge
    padded_image = Image.new('RGB', (square_edge, square_edge), (0,0,0))


    #copy available image into black square image to get padded version of image
    #In Python 3, to get an integer result from a division you need to use // instead of /
    up_left_coordinate = ((square_edge - x) // 2, (square_edge - y) // 2)


    #Note: in this library, cartesian pixel coordinate system with (0,0) in the upper left corner is used
    # so, to centre old image on square padded image, corresponding upperleft coordinate would be obtained by the above formula

    padded_image.paste(image, up_left_coordinate)

    image = padded_image




    #RESCALING TO 224 X 224

    RESCALE_SIZE = 224


    #utilize resize function of pillow library
    rescaled_image = image.resize((RESCALE_SIZE,RESCALE_SIZE))
    image = rescaled_image



    #CONVERT IMAGES INTO NUMPY ARRAY BEFORE NORMALIZATION


    image = np.asarray(image).astype(np.float32)


    

    #DO NORMALIZATION
    image = np.true_divide(image,255)

    image[:,:,0] = np.subtract(image[:,:,0],0.485)
    image[:,:,1] = np.subtract(image[:,:,1],0.456)
    image[:,:,2] = np.subtract(image[:,:,2],0.406)

    image[:,:,0] = np.true_divide(image[:,:,0],0.229)
    image[:,:,1] = np.true_divide(image[:,:,1],0.224)
    image[:,:,2] = np.true_divide(image[:,:,2],0.225)

    processed_image = image



    return processed_image

#function ends ------------------------



#TESTING


import glob

import cv2 as cv
import numpy as np
import sys

import torch
from resnet import resnet50
from sklearn import preprocessing
from PIL import Image



model = resnet50(pretrained=True)
model.eval()

#READ TEST IMAGES
test_images = []

start = time.time()


#load models
svm_classifiers = []
model_pathes= ['model1.sav','model2.sav','model3.sav','model4.sav','model5.sav','model6.sav','model7.sav','model8.sav','model9.sav','model10.sav']
for model_path in model_pathes:

    loaded_model = pickle.load(open(model_path, 'rb'))
    svm_classifiers.append(loaded_model)




#read all images under the test/images folder
i = range(0,100)
for image_path in i:
    
    #read image
    image = cv.imread("test/images/"+str(image_path)+".JPEG") # load an image_path
    
   

    #convert image to rgb
    image = cv.cvtColor(image, cv.COLOR_BGR2RGB)

    #add image to list
    test_images.append(image)   



# 1.Extract candidate windows

modelPath = "model.yml"
windows = []
i = 1
#for each image extract window
for image in test_images:

    edge_detection = cv.ximgproc.createStructuredEdgeDetection(modelPath)
    rgb_im = cv.cvtColor(image, cv.COLOR_BGR2RGB)
    edges = edge_detection.detectEdges(np.float32(rgb_im) / 255.0)

    orimap = edge_detection.computeOrientation(edges)
    edges = edge_detection.edgesNms(edges, orimap)

    edge_boxes = cv.ximgproc.createEdgeBoxes()
    edge_boxes.setMaxBoxes(50)

    #get boxes values consist of x,y,width,height
    boxes = edge_boxes.getBoundingBoxes(edges, orimap)

    windows.append(boxes)
    



# 2.Classification and localization
optimum_windows = np.asarray([],dtype=np.int8)
predictions = np.asarray([],dtype=np.int8)


#for each image 
for i in range(0,len(windows)):

    test_image = test_images[i]

    candidate_windows = windows[i]

    


    windows_predictions = np.asarray([],dtype=np.int8)
    windows_probabilities= np.asarray([]) 
    a = []
    #for each window
    for j in range(0,len(candidate_windows)):

        #get coordinates and length values
        a = candidate_windows[j]
        x, y, w, h = candidate_windows[j]

        #crop image to window obtained by box edge method
        #Note: opencv images are stored in numpy array, top left point corresponds to 0,0 coordinate
        cropped_image = test_image[y:y+h,x:x+w,:]
        #convert opencv numpy array to PIL image
        cropped_image = Image.fromarray(cropped_image.astype('uint8'), 'RGB')

        cropped_image = preprocessImage(cropped_image)

        # we append an augmented dimension to indicate batch_size, which is one
        cropped_image = np.reshape(cropped_image, [1, 224, 224, 3])


        # model takes as input images of size [batch_size, 3, im_height, im_width]
        cropped_image = np.transpose(cropped_image, [0, 3, 1, 2])
        #(For the second arguement) i in the j-th place in the tuple means a’s i-th axis becomes a.transpose()’s j-th axis.
        #e.g. shape of the new array would be [1,3,224,224]


        # convert the Numpy image to torch.FloatTensor
        tensor_image = torch.from_numpy(cropped_image)



        # extract features
        feature_vector = model(tensor_image)


        # convert the features of type torch.FloatTensor to a Numpy array
        # so that you can either work with them within the sklearn environment
        # or save them as .mat files


        feature_vector.data.cpu().numpy()

        feature_vector = feature_vector.data.numpy()
        #Note: the version placed in the project document ends up with an error


        #before adding list, normalize feature vector
        feature_vector = preprocessing.normalize(feature_vector,norm='l2')

        feature_vector = np.squeeze(feature_vector)


    
        classifier_scores = np.asarray([])
        
        #apply each of binary svm classifier to window
        for classifier in svm_classifiers:
            
            #get window score for each binary classifier            
            classifier_scores = np.append(classifier_scores,classifier.predict_log_proba(feature_vector.reshape(1,-1))[0][1])
            
            #Note: (error)array.reshape(1, -1) if it contains a single sample
    
        window_prediction = np.argmax(classifier_scores)+1  
        window_probability = np.amax(classifier_scores)
        
        windows_predictions = np.append(windows_predictions,window_prediction)
        windows_probabilities = np.append(windows_probabilities,window_probability)

    image_prediction = windows_predictions[np.argmax(windows_probabilities)]
   
    optimum_window = candidate_windows[np.argmax(windows_probabilities)]
    
    print(optimum_window)

    optimum_window = np.reshape(optimum_window,4)

    optimum_windows = np.append(optimum_windows,[optimum_window])
    
    predictions = np.append(predictions,image_prediction) 


label_dictionary = {1:'n01615121', 2:'n02099601', 3:'n02123159',4:'n02129604', 5:'n02317335', 6:'n02391049', 7:'n02410509', 8:'n02422699', 9:'n02481823',10:'n02504458'}


prediction_labels = [label_dictionary[label] for label in predictions]

optimum_windows = optimum_windows.astype(int).reshape(len(test_images),4)

np.savetxt("windows.txt",optimum_windows,delimiter = ',')
with open('predictions.txt', 'w') as f:
    for item in prediction_labels:
        f.write("%s\n" % item)








end = time.time()

print("total execution time : " + str(end - start))