Spaces:
Running
Running
Upload image_classifier.py
Browse files- image_classifier.py +139 -0
image_classifier.py
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import cv2
|
3 |
+
from tensorflow.keras.applications import ResNet50
|
4 |
+
from tensorflow.keras.applications.resnet50 import preprocess_input
|
5 |
+
from tensorflow.keras.preprocessing import image
|
6 |
+
from skimage.metrics import structural_similarity as ssim
|
7 |
+
import os
|
8 |
+
import argparse
|
9 |
+
|
10 |
+
class ImageCharacterClassifier:
|
11 |
+
def __init__(self, similarity_threshold=0.7):
|
12 |
+
# Initialize ResNet50 model without top classification layer
|
13 |
+
self.model = ResNet50(weights='imagenet', include_top=False, pooling='avg')
|
14 |
+
self.similarity_threshold = similarity_threshold
|
15 |
+
|
16 |
+
def load_and_preprocess_image(self, image_path, target_size=(224, 224)):
|
17 |
+
# Load and preprocess image for ResNet50
|
18 |
+
img = image.load_img(image_path, target_size=target_size)
|
19 |
+
img_array = image.img_to_array(img)
|
20 |
+
img_array = np.expand_dims(img_array, axis=0)
|
21 |
+
img_array = preprocess_input(img_array)
|
22 |
+
return img_array
|
23 |
+
|
24 |
+
def extract_features(self, image_path):
|
25 |
+
# Extract deep features using ResNet50
|
26 |
+
preprocessed_img = self.load_and_preprocess_image(image_path)
|
27 |
+
features = self.model.predict(preprocessed_img)
|
28 |
+
return features
|
29 |
+
|
30 |
+
def calculate_ssim(self, img1_path, img2_path):
|
31 |
+
# Calculate SSIM between two images
|
32 |
+
img1 = cv2.imread(img1_path)
|
33 |
+
img2 = cv2.imread(img2_path)
|
34 |
+
|
35 |
+
# Convert to grayscale if images are in color
|
36 |
+
if len(img1.shape) == 3:
|
37 |
+
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
|
38 |
+
if len(img2.shape) == 3:
|
39 |
+
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
|
40 |
+
|
41 |
+
# Resize images to same dimensions
|
42 |
+
img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0]))
|
43 |
+
|
44 |
+
score = ssim(img1, img2)
|
45 |
+
return score
|
46 |
+
|
47 |
+
def classify_images(self, reference_image_path, image_folder_path):
|
48 |
+
# Extract features from reference image
|
49 |
+
reference_features = self.extract_features(reference_image_path)
|
50 |
+
|
51 |
+
results = []
|
52 |
+
|
53 |
+
# Process each image in the folder
|
54 |
+
for image_name in os.listdir(image_folder_path):
|
55 |
+
if image_name.lower().endswith(('.png', '.jpg', '.jpeg')):
|
56 |
+
image_path = os.path.join(image_folder_path, image_name)
|
57 |
+
|
58 |
+
try:
|
59 |
+
# Calculate SSIM
|
60 |
+
ssim_score = self.calculate_ssim(reference_image_path, image_path)
|
61 |
+
|
62 |
+
# Extract features and calculate similarity
|
63 |
+
image_features = self.extract_features(image_path)
|
64 |
+
|
65 |
+
# Calculate cosine similarity
|
66 |
+
feature_similarity = np.dot(reference_features.flatten(),
|
67 |
+
image_features.flatten()) / (
|
68 |
+
np.linalg.norm(reference_features) * np.linalg.norm(image_features))
|
69 |
+
|
70 |
+
# Give more weight to feature similarity
|
71 |
+
combined_similarity = (0.3 * ssim_score + 0.7 * feature_similarity)
|
72 |
+
|
73 |
+
# Classify based on similarity threshold
|
74 |
+
is_similar = combined_similarity >= self.similarity_threshold
|
75 |
+
|
76 |
+
results.append({
|
77 |
+
'image_name': image_name,
|
78 |
+
'ssim_score': ssim_score,
|
79 |
+
'feature_similarity': feature_similarity,
|
80 |
+
'combined_similarity': combined_similarity,
|
81 |
+
'is_similar': is_similar
|
82 |
+
})
|
83 |
+
|
84 |
+
except Exception as e:
|
85 |
+
print(f"Error processing {image_name}: {str(e)}")
|
86 |
+
continue
|
87 |
+
|
88 |
+
return results
|
89 |
+
|
90 |
+
def main():
|
91 |
+
# Create argument parser
|
92 |
+
parser = argparse.ArgumentParser(description='Image Character Classification')
|
93 |
+
parser.add_argument('--reference', '-r',
|
94 |
+
type=str,
|
95 |
+
required=True,
|
96 |
+
help='Path to reference image')
|
97 |
+
parser.add_argument('--folder', '-f',
|
98 |
+
type=str,
|
99 |
+
required=True,
|
100 |
+
help='Path to folder containing images to compare')
|
101 |
+
parser.add_argument('--threshold', '-t',
|
102 |
+
type=float,
|
103 |
+
default=0.5, # Lowered the default threshold
|
104 |
+
help='Similarity threshold (default: 0.5)')
|
105 |
+
|
106 |
+
# Parse arguments
|
107 |
+
args = parser.parse_args()
|
108 |
+
|
109 |
+
# Initialize classifier
|
110 |
+
classifier = ImageCharacterClassifier(similarity_threshold=args.threshold)
|
111 |
+
|
112 |
+
# Check if paths exist
|
113 |
+
if not os.path.exists(args.reference):
|
114 |
+
print(f"Error: Reference image not found at {args.reference}")
|
115 |
+
return
|
116 |
+
|
117 |
+
if not os.path.exists(args.folder):
|
118 |
+
print(f"Error: Image folder not found at {args.folder}")
|
119 |
+
return
|
120 |
+
|
121 |
+
# Perform classification
|
122 |
+
results = classifier.classify_images(args.reference, args.folder)
|
123 |
+
|
124 |
+
# Sort results by similarity score
|
125 |
+
results.sort(key=lambda x: x['combined_similarity'], reverse=True)
|
126 |
+
|
127 |
+
# Print results
|
128 |
+
print("\nResults sorted by similarity (highest to lowest):")
|
129 |
+
print("-" * 50)
|
130 |
+
for result in results:
|
131 |
+
print(f"\nImage: {result['image_name']}")
|
132 |
+
print(f"SSIM Score: {result['ssim_score']:.3f}")
|
133 |
+
print(f"Feature Similarity: {result['feature_similarity']:.3f}")
|
134 |
+
print(f"Combined Similarity: {result['combined_similarity']:.3f}")
|
135 |
+
print(f"Is Similar: {result['is_similar']}")
|
136 |
+
print("-" * 30)
|
137 |
+
|
138 |
+
if __name__ == "__main__":
|
139 |
+
main()
|