Introduction
Image Markers complements the convenience of Radio Markers and the accuracy of Scan Markers.
Compare the similarity between image markers can be performed in many ways, for example:
Image Markers complements the convenience of Radio Markers and the accuracy of Scan Markers.
Compare the similarity between image markers can be performed in many ways, for example:
A Structural Similarity Index (SSIM) API can be created easily by running python in Flask.
pip install flask numpy scikit-image opencv-python
from flask import Flask, request, jsonify
import cv2
import numpy as np
from skimage.metrics import structural_similarity as ssim
from werkzeug.utils import secure_filename
import os
app = Flask(__name__)
# Temporary directory to store uploaded images
UPLOAD_FOLDER = 'uploads'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def load_image(image_path):
"""Load image from file in color (RGB)."""
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert BGR (OpenCV) to RGB
return image
def compare_images(image1, image2):
"""Compare two color images using SSIM and return the similarity score."""
# Resize images to the same size if necessary
if image1.shape != image2.shape:
image2 = cv2.resize(image2, (image1.shape[1], image1.shape[0]))
# Compute SSIM for each channel (R, G, B) separately
ssim_scores = []
for i in range(3): # Loop over the 3 color channels
score, _ = ssim(image1[:, :, i], image2[:, :, i], full=True)
ssim_scores.append(score)
# Average the SSIM score across all channels
return np.mean(ssim_scores)
@app.route('/compare', methods=['POST'])
def compare_images_api():
"""API endpoint to compare two images."""
if 'image1' not in request.files or 'image2' not in request.files:
return jsonify({"error": "Please upload two images with keys 'image1' and 'image2'"}), 400
image1_file = request.files['image1']
image2_file = request.files['image2']
# Save uploaded images temporarily
image1_path = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(image1_file.filename))
image2_path = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(image2_file.filename))
image1_file.save(image1_path)
image2_file.save(image2_path)
# Load images
image1 = load_image(image1_path)
image2 = load_image(image2_path)
# Compare images and calculate the similarity score
similarity_score = compare_images(image1, image2)
# Cleanup: Remove the temporary files
os.remove(image1_path)
os.remove(image2_path)
return jsonify({"similarity_score": similarity_score})
if __name__ == '__main__':
app.run(debug=True)
Command
curl -X POST -F "image1=@path_to_first_image.jpg" -F "image2=@path_to_second_image.jpg" http://localhost:5000/compare
Output
{
"similarity_score": 0.8674
}
Example of using ResNet50 to extract features.