Analyzing Movie Posters with Machine Learning: A Comprehensive Guide for Image Classification

Waleed Mousa
4 min readMar 19, 2023

--

In this tutorial, we will be using computer vision techniques to analyze and classify movie posters based on their visual features. By the end of this tutorial, you will be able to preprocess images, extract visual features, train and evaluate a machine learning model, and use it to classify new movie posters.

Agenda:

  1. Data Collection
  2. Preprocessing
  3. Feature Extraction
  4. Model Selection
  5. Model Training and Evaluation
  6. Prediction

Let’s get started!

Data Collection:

The first step is to collect a dataset of movie posters. For this tutorial, we will be using the “Movie Poster Dataset” available on Kaggle. You can download the dataset from the following link:

https://www.kaggle.com/neha1703/movie-genre-from-its-poster

The dataset contains around 27,000 movie posters along with their genres. Each poster is in JPG format and has a resolution of 500x750 pixels.

Preprocessing:

Once you have downloaded the dataset, the next step is to preprocess the images to standardize their size and color. We will be using Python’s Pillow library for this purpose.

First, install the Pillow library by running the following command:

pip install Pillow

Next, create a new Python script and import the necessary libraries:

from PIL import Image
import os

Now, create a new directory called “processed_images” to store the preprocessed images:

if not os.path.exists('processed_images'):
os.makedirs('processed_images')

Next, loop through all the images in the dataset, resize them to a standard size of 224x224 pixels, and save them in the “processed_images” directory:

for filename in os.listdir('movie_posters'):
try:
with Image.open(os.path.join('movie_posters', filename)) as img:
img = img.resize((224, 224), resample=Image.BICUBIC)
img.save(os.path.join('processed_images', filename))
except:
pass

This code resizes each image to 224x224 pixels using bicubic interpolation and saves it in the “processed_images” directory. The try-except block is used to handle any errors that might occur during image processing.

Feature Extraction:

The next step is to extract visual features from the posters. We will be using a pre-trained Convolutional Neural Network (CNN) called VGG16 to extract features from the images.

First, install the Keras library by running the following command:

pip install keras

Next, create a new Python script and import the necessary libraries:

from keras.preprocessing import image
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
import numpy as np
import os

Now, load the pre-trained VGG16 model:

model = VGG16(weights='imagenet', include_top=False)

Next, loop through all the images in the “processed_images” directory, extract features using the VGG16 model, and save them in a Numpy array:

features = []

for filename in os.listdir('processed_images'):
img_path = os.path.join('processed_images', filename)
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
feature = model.predict(x)
features.append(feature.flatten())

features = np.array(features)
np.save('features.npy', features)

This code extracts features from each image using the VGG16 model and saves them in a Numpy array called “features.npy”. The features.npy file will be used later to train and evaluate the machine learning model.

Model Selection:

The next step is to select a suitable machine learning model to classify the movie posters based on their visual features.

For this tutorial, we will be using the Support Vector Machine (SVM) algorithm, which is commonly used for image classification tasks.

First, import the necessary libraries:

from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
import numpy as np

Next, load the features.npy file:

features = np.load('features.npy')

Now, load the genre labels from the CSV file:

labels = np.genfromtxt('MovieGenre.csv', delimiter=',', usecols=range(1, 25), skip_header=1)

This code loads the genre labels from the “MovieGenre.csv” file, which contains information about each movie’s genre.

Next, split the dataset into training and testing sets:

X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, random_state=42)

This code splits the dataset into 80% training and 20% testing sets.

Model Training and Evaluation:

The next step is to train and evaluate the SVM model using the training and testing sets.

First, create an instance of the SVM model:

clf = SVC(kernel='linear', C=1, gamma='scale', probability=True)

Next, fit the model to the training data:

clf.fit(X_train, y_train)

Now, use the trained model to make predictions on the testing data:

y_pred = clf.predict(X_test)

Finally, evaluate the accuracy of the model:

accuracy = accuracy_score(y_test, y_pred)
print('Accuracy:', accuracy)

Prediction:

The final step is to use the trained model to predict the genre of new movie posters.

First, load the new poster image:

img = image.load_img('new_poster.jpg', target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

Next, use the SVM model to predict the genre of the new poster:

y_pred = clf.predict(x)
print('Predicted Genre:', y_pred)

That’s It!

In this tutorial, we have learned how to use computer vision techniques to analyze and classify movie posters based on their visual features.

We have used a pre-trained CNN to extract features from the posters, trained an SVM model on the extracted features, and used the model to predict the genre of new movie posters.

With these techniques, you can now analyze and classify movie posters for various applications, such as movie recommendation systems, genre prediction, and box office performance analysis.

More Content:

Machine Learning & AI

40 stories

--

--