Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
import io
|
|
|
|
|
4 |
|
5 |
# Function to perform image search (placeholder)
|
6 |
def search_image(image):
|
@@ -18,11 +20,35 @@ def save_to_profile(results, user_profile):
|
|
18 |
user_profile["search_history"] = []
|
19 |
user_profile["search_history"].append(results)
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
# Streamlit app
|
22 |
def main():
|
23 |
# Sidebar with navigation
|
24 |
st.sidebar.title("Navigation")
|
25 |
-
page = st.sidebar.radio("Go to", ["Home", "About Us", "Help"])
|
26 |
|
27 |
if page == "Home":
|
28 |
st.title("Image Cross-Check App")
|
@@ -60,6 +86,29 @@ def main():
|
|
60 |
else:
|
61 |
st.warning("No matches found.")
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
elif page == "About Us":
|
64 |
st.title("About Us")
|
65 |
st.write("""
|
@@ -78,6 +127,9 @@ def main():
|
|
78 |
3. **View Results**: If matches are found, they will be displayed on the screen.
|
79 |
4. **Save Results**: The results are automatically saved to your profile for future reference.
|
80 |
|
|
|
|
|
|
|
81 |
### Troubleshooting:
|
82 |
- If the app is not working, ensure you have a stable internet connection.
|
83 |
- Supported image formats are JPG, JPEG, and PNG.
|
|
|
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):
|
|
|
20 |
user_profile["search_history"] = []
|
21 |
user_profile["search_history"].append(results)
|
22 |
|
23 |
+
# Function to compare two images and calculate similarity percentage
|
24 |
+
def compare_images(image1, image2):
|
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)
|
28 |
+
|
29 |
+
# Initialize ORB detector
|
30 |
+
orb = cv2.ORB_create()
|
31 |
+
|
32 |
+
# Detect keypoints and descriptors
|
33 |
+
kp1, des1 = orb.detectAndCompute(img1, None)
|
34 |
+
kp2, des2 = orb.detectAndCompute(img2, None)
|
35 |
+
|
36 |
+
# Use BFMatcher to match descriptors
|
37 |
+
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
|
38 |
+
matches = bf.match(des1, des2)
|
39 |
+
|
40 |
+
# Sort matches by distance (lower is better)
|
41 |
+
matches = sorted(matches, key=lambda x: x.distance)
|
42 |
+
|
43 |
+
# Calculate similarity percentage
|
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
|
50 |
st.sidebar.title("Navigation")
|
51 |
+
page = st.sidebar.radio("Go to", ["Home", "Compare Photos", "About Us", "Help"])
|
52 |
|
53 |
if page == "Home":
|
54 |
st.title("Image Cross-Check App")
|
|
|
86 |
else:
|
87 |
st.warning("No matches found.")
|
88 |
|
89 |
+
elif page == "Compare Photos":
|
90 |
+
st.title("Compare Photos")
|
91 |
+
st.write("Upload two images to compare their similarity.")
|
92 |
+
|
93 |
+
# Upload first image
|
94 |
+
uploaded_file1 = st.file_uploader("Choose the first image...", type=["jpg", "jpeg", "png"])
|
95 |
+
if uploaded_file1 is not None:
|
96 |
+
image1 = Image.open(uploaded_file1)
|
97 |
+
st.image(image1, caption='First Image.', use_container_width=True)
|
98 |
+
|
99 |
+
# Upload second image
|
100 |
+
uploaded_file2 = st.file_uploader("Choose the second image...", type=["jpg", "jpeg", "png"])
|
101 |
+
if uploaded_file2 is not None:
|
102 |
+
image2 = Image.open(uploaded_file2)
|
103 |
+
st.image(image2, caption='Second Image.', use_container_width=True)
|
104 |
+
|
105 |
+
# Compare images
|
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 |
+
similarity, matches = compare_images(image1, image2)
|
110 |
+
st.success(f"Similarity: {similarity:.2f}%")
|
111 |
+
|
112 |
elif page == "About Us":
|
113 |
st.title("About Us")
|
114 |
st.write("""
|
|
|
127 |
3. **View Results**: If matches are found, they will be displayed on the screen.
|
128 |
4. **Save Results**: The results are automatically saved to your profile for future reference.
|
129 |
|
130 |
+
### Compare Photos:
|
131 |
+
- Upload two images and click "Compare" to calculate their similarity percentage.
|
132 |
+
|
133 |
### Troubleshooting:
|
134 |
- If the app is not working, ensure you have a stable internet connection.
|
135 |
- Supported image formats are JPG, JPEG, and PNG.
|