Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,15 @@
|
|
1 |
import streamlit as st
|
2 |
-
from PIL import Image
|
3 |
import io
|
4 |
-
import cv2
|
5 |
import numpy as np
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
# Function to perform image search (placeholder)
|
8 |
def search_image(image):
|
9 |
# Placeholder function for image search
|
@@ -20,8 +26,8 @@ def save_to_profile(results, user_profile):
|
|
20 |
user_profile["search_history"] = []
|
21 |
user_profile["search_history"].append(results)
|
22 |
|
23 |
-
# Function to compare two images
|
24 |
-
def
|
25 |
# Convert images to grayscale
|
26 |
img1 = cv2.cvtColor(np.array(image1), cv2.COLOR_RGB2GRAY)
|
27 |
img2 = cv2.cvtColor(np.array(image2), cv2.COLOR_RGB2GRAY)
|
@@ -44,6 +50,21 @@ def compare_images(image1, image2):
|
|
44 |
similarity = (len(matches) / min(len(kp1), len(kp2))) * 100
|
45 |
return similarity, matches
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
# Streamlit app
|
48 |
def main():
|
49 |
# Sidebar with navigation
|
@@ -106,7 +127,10 @@ def main():
|
|
106 |
if uploaded_file1 is not None and uploaded_file2 is not None:
|
107 |
if st.button("Compare"):
|
108 |
with st.spinner("Comparing images..."):
|
109 |
-
|
|
|
|
|
|
|
110 |
st.success(f"Similarity: {similarity:.2f}%")
|
111 |
|
112 |
elif page == "About Us":
|
@@ -146,4 +170,4 @@ def main():
|
|
146 |
st.sidebar.write(f"- {match['source']}: {match['url']}")
|
147 |
|
148 |
if __name__ == "__main__":
|
149 |
-
main()
|
|
|
1 |
import streamlit as st
|
2 |
+
from PIL import Image, ImageChops
|
3 |
import io
|
|
|
4 |
import numpy as np
|
5 |
|
6 |
+
# Check if OpenCV is installed
|
7 |
+
try:
|
8 |
+
import cv2
|
9 |
+
OPENCV_AVAILABLE = True
|
10 |
+
except ImportError:
|
11 |
+
OPENCV_AVAILABLE = False
|
12 |
+
|
13 |
# Function to perform image search (placeholder)
|
14 |
def search_image(image):
|
15 |
# Placeholder function for image search
|
|
|
26 |
user_profile["search_history"] = []
|
27 |
user_profile["search_history"].append(results)
|
28 |
|
29 |
+
# Function to compare two images using OpenCV (if available)
|
30 |
+
def compare_images_opencv(image1, image2):
|
31 |
# Convert images to grayscale
|
32 |
img1 = cv2.cvtColor(np.array(image1), cv2.COLOR_RGB2GRAY)
|
33 |
img2 = cv2.cvtColor(np.array(image2), cv2.COLOR_RGB2GRAY)
|
|
|
50 |
similarity = (len(matches) / min(len(kp1), len(kp2))) * 100
|
51 |
return similarity, matches
|
52 |
|
53 |
+
# Function to compare two images using pixel-based method (fallback)
|
54 |
+
def compare_images_pixel(image1, image2):
|
55 |
+
# Resize images to the same size
|
56 |
+
image1 = image1.resize((256, 256))
|
57 |
+
image2 = image2.resize((256, 256))
|
58 |
+
|
59 |
+
# Calculate the difference between the two images
|
60 |
+
diff = ImageChops.difference(image1, image2)
|
61 |
+
|
62 |
+
# Calculate the similarity percentage
|
63 |
+
diff_pixels = sum(diff.convert("L").point(lambda x: 255 if x > 0 else 0).getdata())
|
64 |
+
total_pixels = 256 * 256
|
65 |
+
similarity = 100 - (diff_pixels / total_pixels) * 100
|
66 |
+
return similarity
|
67 |
+
|
68 |
# Streamlit app
|
69 |
def main():
|
70 |
# Sidebar with navigation
|
|
|
127 |
if uploaded_file1 is not None and uploaded_file2 is not None:
|
128 |
if st.button("Compare"):
|
129 |
with st.spinner("Comparing images..."):
|
130 |
+
if OPENCV_AVAILABLE:
|
131 |
+
similarity, _ = compare_images_opencv(image1, image2)
|
132 |
+
else:
|
133 |
+
similarity = compare_images_pixel(image1, image2)
|
134 |
st.success(f"Similarity: {similarity:.2f}%")
|
135 |
|
136 |
elif page == "About Us":
|
|
|
170 |
st.sidebar.write(f"- {match['source']}: {match['url']}")
|
171 |
|
172 |
if __name__ == "__main__":
|
173 |
+
main()
|