<jupyter_start><jupyter_text>The dataset is referenced as **Flickr Logos 27 Dataset**, which could be accessed using this url http://image.ntua.gr/iva/datasets/flickr_logos/       The training data consists of **4536 images of logos belonging to 27 companies (168 for each)** along with the annotation of the location of the logo in the image. The test data (which will be referred to as **Query data** moving forward)however, only have labels and NOT the annotations of the location of the logos.<jupyter_code># Importing libraries
import os
import re
import cv2
import joblib
import numpy as np
import pandas as pd
from PIL import ImageOps
from sklearn import svm
import joblib
import matplotlib.pyplot as plt
from PIL import Image, ImageFilter
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, accuracy_score

### Reading annotations and labels for training data

train_img_label_file = os.path.join(os.getcwd(),
                                    "flickr_logos_27_dataset_training_set_annotation.txt")

train_images = pd.read_csv(train_img_label_file,
                           sep=' ', 
                           usecols = [i for i in range(7)],
                           header=None,  
                           names=["file_name",'label','subset','x1','y1','x2','y2'])
train_images.head()
idx = 465
pth = 'data/'+train_images.loc[idx,'file_name']
image = Image.open(pth)
plt.imshow(image)
plt.axis("off")
plt.title("Train image of apple logo")
plt.show()<jupyter_output><empty_output><jupyter_text>**** The target variable is **'label'**, which is the brand corresponding the particular image containing the logo. And the predictors are the 3 channel RGB values in a image.Yes, the data is being pre-processed before being used in the training of a classifier. Following transformation would be done on the data :       1. Cropping the image using the annotations (in case of trainig data)2. Converting from RGB channel to grayscale (for dimensionality reduction)3. Equalizing image histogram.4. Randomly rotation the image 5. Calculating Histogram of Oriented Gradient (HOG) of the image6. Coversion of the image to a numpy array.7. Normalizing and scaling the numpy array values using *Standard Scaler*<jupyter_code># Testing image processing methods 
# Cropping using annotations and changing it to greyscale along with equalizing
idx = 465
pth = 'data/'+train_images.loc[idx,'file_name']
image = Image.open(pth)
image = image.crop([train_images.loc[idx,'x1'],train_images.loc[idx,'y1'],train_images.loc[idx,'x2'],train_images.loc[idx,'y2']])
image = ImageOps.grayscale(image)
image = ImageOps.equalize(image)
image = image.rotate(45)

plt.imshow(image)
plt.axis('off')
plt.title("Sample of train image after processing")
plt.show()<jupyter_output><empty_output><jupyter_text>***  Data Pre-processing<jupyter_code>#Cropping using annotations,changing it to greyscale along with equalizing and resizing

def image_processor(label_df,crop=True):
    '''
    function to read and pre process training images with labels
    Returns a numpy array of images
    '''
    angle = [0,45,90,135]
    train_img_arr = []
    num_images = len(label_df)
    for i in range(num_images):
        pth = 'data/'+label_df.loc[i,'file_name']
        image = Image.open(pth)
        if crop == True :
            x1 = train_images.loc[i,'x1']
            y1 = train_images.loc[i,'y1']
            x2 = train_images.loc[i,'x2']
            y2 = train_images.loc[i,'y2']
            image = image.crop([x1,y1,x2,y2])
        image = ImageOps.grayscale(image)
        image = ImageOps.equalize(image)
        
        train_img_arr.append(np.asarray(image.resize([64,64]),dtype="int32")) 
    return np.array(train_img_arr)
train_data = image_processor(train_images)
y = train_images['label']
train_data.shape
# resized processed image
plt.imshow(train_data[465])
plt.axis('off')   
plt.title("\nProcessed image\n", size=15)
plt.show()<jupyter_output><empty_output><jupyter_text>Applying HOG (Histogram of Oriented gradient)<jupyter_code>from skimage import color
from skimage.feature import hog
def HOG(img_arr,ppc=16,cpb=4):
    hog_images = []
    hog_features = []
    for image in img_arr:
        fd,hog_image = hog(image, orientations=8, pixels_per_cell=(ppc,ppc),
                           cells_per_block=(cpb, cpb),block_norm= 'L2',visualize=True)
        hog_images.append(hog_image)
        hog_features.append(fd)
        
    print("\nHOG Completed on {} images, with {} features\n"
          .format(np.shape(hog_features)[0],np.shape(hog_features)[1]))   
    
    return hog_images, hog_features
hog_images, hog_features = HOG(train_data)
np.shape(hog_features[0])
# analyzing images after HOG 

plt.imshow(hog_images[460])
plt.axis('off')   
plt.title("\nProcessed image after HOG\n", size=15)
plt.show()
feature_vec_train = np.array(hog_features)
print(np.shape(feature_vec_train))

sc = StandardScaler()
feature_vec_train = sc.fit_transform(feature_vec_train)<jupyter_output>(4536, 128)<jupyter_text>Training the classifier<jupyter_code>#Training the classifier on the actual training dataset with 4536 annotated logo images
clf = svm.SVC()
clf.fit(feature_vec_train, y)
joblib.dump(clf, 'logo_classifier.joblib')
joblib.dump(sc, 'std_scaler.bin', compress=True)
clf = joblib.load('logo_classifier.joblib')
# Pre-process the test images before classifcication
# call the image_processor
# call HOG
# predict
# return label<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1051590868236563, 'fraction_numerical': 0.016897357540895202, 'mean_word_length': 4.209737827715355, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '660202', 'license': 'Apache-2.0', 'n_tokens_mistral': 1853, 'n_tokens_neox': 1738, 'n_words': 481, 'path': 'Logo_detection.ipynb', 'repo_name': 'akshaypunwatkar/Containers_w_docker_and_cloud', 'size': '5563'}