I'm looking for a method to Spot the difference between two images using AI.
It's my university project, that my professor asked me to create a program to detect and spot the differences in two pairs of images using AI.
I deployed it using the Siamese Network, to calculate the difference, and if the difference was greater than the threshold then, I used the following code to show the differences :
input_images = np.array([[img1, img2]])
difference_image = np.abs(input_images[0, 0] - input_images[0, 1])
plt.imshow(difference_image)
But my prof didn't accept it he gave me a hint to split images into smaller shapes using Conv2D and then compare those shapes and if there is a difference, highlight that using the bounding box.
Can anyone help to deploy this code?
My previous code is :
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras import layers
img1 = plt.imread('1-1.jpg')
img2 = plt.imread('1-2.jpg')
input_shape = img1.shape # Assuming images are of the same shape
# Function to create
# def create_siamese_model(input_shape):
input_image_1 = layers.Input(shape=input_shape, name='input_image_1')
input_image_2 = layers.Input(shape=input_shape, name='input_image_2')
# Base network
base_network = keras.Sequential([
layers.Conv2D(40, (3, 3), activation='relu', input_shape=input_shape),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Flatten(),
layers.Dense(256, activation='relu')
])
# Encoded representations of input images
encoded_image_1 = base_network(input_image_1)
encoded_image_2 = base_network(input_image_2)
# L1 distance layer
l1_distance = layers.Lambda(lambda tensors: keras.backend.abs(tensors[0] - tensors[1]))([encoded_image_1, encoded_image_2])
# Output layer
output_layer = layers.Dense(15, activation='sigmoid')(l1_distance)
model = keras.Model(inputs=[input_image_1, input_image_2], outputs=output_layer)
input_images = np.array([[img1, img2]])
predictions = model.predict([input_images[:, 0], input_images[:, 1]])
threshold=0.5
if predictions[0, 0] > threshold:
# Highlight differences if the prediction is above the threshold
difference_image = np.abs(input_images[0, 0] - input_images[0, 1])
difference_image
plt.imshow(difference_image)
plt.show()
I found a way that uses CNN network to find differences between two images code:
this code use CNN network to Calculate the distance between two image array