<jupyter_start><jupyter_text>Plate Number Image Classification<jupyter_code># Import the libraries needed
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
import random
# Variable Declarations to be used for image dataset creation
folders = ["negative_images", "plate_number"]
file_directory = "C:\\Users\\Chidibede\\Desktop\\HNG\\Dataset\\plate numbers images"
img_size = 70
image_data = []


# function to create the image training data
def create_image_data():
    for folder in folders:
        target = folders.index(folder)
        path = os.path.join(file_directory, folder)
        for file in os.listdir(path):
            try:
                complete_path = os.path.join(path, file)
                img = cv2.imread(complete_path, cv2.IMREAD_GRAYSCALE)
                img_resized = cv2.resize(img, (img_size, img_size))
                image_data.append([img_resized, target])
            except Exception as e:
                print(e)

# create the image data by calling the function
create_image_data()

# shuffle the data
import random
random.shuffle(image_data)

# Create the features and labels
X = []
y = []

for feature, label in image_data:
    X.append(feature)
    y.append(label)

# Convert to a numpy array and reshape the feature array
X = np.array(X)
X = np.array(X)
dataset_size = len(image_data)
X = X.reshape(dataset_size,-1)

# check the new shape
print("The shape of the features is ", X.shape)
# Create the testing data of 25 plate numbers and 25 non plate numbers
test_folders = ["test_negative_images", "test_plate_numbers"]
test_file_directory = "C:\\Users\\Chidibede\\Desktop\\HNG\\Dataset\\plate numbers images\\test_data"
img_size = 70
test_image_data = []


# function to create the image data
def create_test_image_data():
    for test_folder in test_folders:
        target = test_folders.index(test_folder)
        path = os.path.join(test_file_directory, test_folder)
        for file in os.listdir(path):
            try:
                complete_path = os.path.join(path, file)
                img = cv2.imread(complete_path, cv2.IMREAD_GRAYSCALE)
                img_resized = cv2.resize(img, (img_size, img_size))
                test_image_data.append([img_resized, target])
            except Exception as e:
                print(e)
create_test_image_data()
# Shuffle
random.shuffle(test_image_data)
# create the test features and labels
# Create the features and labels
X_test = []
y_test = []

for feature, label in test_image_data:
    X_test.append(feature)
    y_test.append(label)

# Convert to a numpy array and reshape the feature array
X_test = np.array(X_test)
dataset_size = len(test_image_data)
X_test = X_test.reshape(dataset_size,-1)
print("the shape of X_test is ", X_test.shape)
# Training the dataset using support vector machine

# import
from sklearn import svm

# instantiate
svm_model = svm.SVC(kernel='linear')

# fit
svm_model.fit(X, y)


# Classification accuracy
y_pred = svm_model.predict(X_test)
from sklearn import metrics
print(metrics.accuracy_score(y_test, y_pred))

# Choose a new model

# import
from sklearn.linear_model import LogisticRegressionCV

#  instantiate
logreg = LogisticRegressionCV(multi_class="auto", max_iter=300, cv=5)

# Train model
logreg.fit(X, y)
# Predict
y_pred = logreg.predict(X_test)

# Evaluate classification accuracy
print(metrics.accuracy_score(y_test, y_pred))<jupyter_output>0.72
{'fraction_non_alphanumeric': 0.09215915740198946, 'fraction_numerical': 0.007021650087770626, 'mean_word_length': 3.6265223274695533, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '310761', 'license': 'MIT', 'n_tokens_mistral': 1129, 'n_tokens_neox': 1089, 'n_words': 300, 'path': 'Plate_Number_image_Classification.ipynb', 'repo_name': 'chidibede/Plate-Number-Classification', 'size': '3418'}