import streamlit as st from PIL import Image, ImageChops import io import numpy as np # Check if OpenCV is installed try: import cv2 OPENCV_AVAILABLE = True except ImportError: OPENCV_AVAILABLE = False # Function to perform image search (placeholder) def search_image(image): # Placeholder function for image search return { "matches": [ {"source": "Social Media A", "url": "https://example.com/match1"}, {"source": "Public Dataset B", "url": "https://example.com/match2"}, ] } # Function to save results to user profile def save_to_profile(results, user_profile): if "search_history" not in user_profile: user_profile["search_history"] = [] user_profile["search_history"].append(results) # Function to compare two images using OpenCV (if available) def compare_images_opencv(image1, image2): # Convert images to grayscale img1 = cv2.cvtColor(np.array(image1), cv2.COLOR_RGB2GRAY) img2 = cv2.cvtColor(np.array(image2), cv2.COLOR_RGB2GRAY) # Initialize ORB detector orb = cv2.ORB_create() # Detect keypoints and descriptors kp1, des1 = orb.detectAndCompute(img1, None) kp2, des2 = orb.detectAndCompute(img2, None) # Use BFMatcher to match descriptors bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) matches = bf.match(des1, des2) # Sort matches by distance (lower is better) matches = sorted(matches, key=lambda x: x.distance) # Calculate similarity percentage similarity = (len(matches) / min(len(kp1), len(kp2))) * 100 return similarity, matches # Function to compare two images using pixel-based method (fallback) def compare_images_pixel(image1, image2): # Resize images to the same size image1 = image1.resize((256, 256)) image2 = image2.resize((256, 256)) # Calculate the difference between the two images diff = ImageChops.difference(image1, image2) # Calculate the similarity percentage diff_pixels = sum(diff.convert("L").point(lambda x: 255 if x > 0 else 0).getdata()) total_pixels = 256 * 256 similarity = 100 - (diff_pixels / total_pixels) * 100 return similarity # Streamlit app def main(): # Sidebar with navigation st.sidebar.title("Navigation") page = st.sidebar.radio("Go to", ["Home", "Compare Photos", "About Us", "Help"]) if page == "Home": st.title("Image Cross-Check App") st.write("Upload an image to cross-check it against social media and public datasets.") # File uploader uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) if uploaded_file is not None: # Display the uploaded image image = Image.open(uploaded_file) st.image(image, caption='Uploaded Image.', use_container_width=True) # Convert image to bytes img_byte_arr = io.BytesIO() image.save(img_byte_arr, format='PNG') img_byte_arr = img_byte_arr.getvalue() # Perform image search if st.button("Search"): with st.spinner("Searching for matches..."): results = search_image(img_byte_arr) if results["matches"]: st.success("Found matches!") for match in results["matches"]: st.write(f"Source: {match['source']}") st.write(f"URL: {match['url']}") # Save results to user profile if "user_profile" not in st.session_state: st.session_state.user_profile = {} save_to_profile(results, st.session_state.user_profile) st.success("Results saved to your profile!") else: st.warning("No matches found.") elif page == "Compare Photos": st.title("Compare Photos") st.write("Upload two images to compare their similarity.") # Upload first image uploaded_file1 = st.file_uploader("Choose the first image...", type=["jpg", "jpeg", "png"]) if uploaded_file1 is not None: image1 = Image.open(uploaded_file1) st.image(image1, caption='First Image.', use_container_width=True) # Upload second image uploaded_file2 = st.file_uploader("Choose the second image...", type=["jpg", "jpeg", "png"]) if uploaded_file2 is not None: image2 = Image.open(uploaded_file2) st.image(image2, caption='Second Image.', use_container_width=True) # Compare images if uploaded_file1 is not None and uploaded_file2 is not None: if st.button("Compare"): with st.spinner("Comparing images..."): if OPENCV_AVAILABLE: similarity, _ = compare_images_opencv(image1, image2) else: similarity = compare_images_pixel(image1, image2) st.success(f"Similarity: {similarity:.2f}%") elif page == "About Us": st.title("About Us") st.write(""" Welcome to **Image Cross-Check App**! We are a team of developers passionate about creating tools that help you explore the digital world. Our app allows you to upload an image and cross-check it against social media platforms and public datasets. Feel free to reach out to us for feedback or suggestions! """) elif page == "Help": st.title("Help") st.write(""" ### How to Use the App: 1. **Upload an Image**: Click on the "Choose an image" button to upload a JPG, JPEG, or PNG file. 2. **Search**: Click the "Search" button to cross-check the image against social media and public datasets. 3. **View Results**: If matches are found, they will be displayed on the screen. 4. **Save Results**: The results are automatically saved to your profile for future reference. ### Compare Photos: - Upload two images and click "Compare" to calculate their similarity percentage. ### Troubleshooting: - If the app is not working, ensure you have a stable internet connection. - Supported image formats are JPG, JPEG, and PNG. - For further assistance, contact us at support@imagecrosscheck.com. """) # Display user profile and search history if "user_profile" in st.session_state and st.session_state.user_profile.get("search_history"): st.sidebar.title("Your Profile") st.sidebar.write("### Search History") for i, search in enumerate(st.session_state.user_profile["search_history"]): st.sidebar.write(f"**Search {i+1}**") for match in search["matches"]: st.sidebar.write(f"- {match['source']}: {match['url']}") if __name__ == "__main__": main()