meraj12 commited on
Commit
36c65ef
·
verified ·
1 Parent(s): 5b6224f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -31
app.py CHANGED
@@ -1,53 +1,51 @@
1
  import streamlit as st
2
- import requests
3
- import face_recognition
4
  import numpy as np
5
  from PIL import Image
 
 
6
  import os
7
- import io
8
 
9
  st.set_page_config(page_title="Face Finder on Internet", layout="centered")
10
  st.title("🕵️‍♂️ Check if Your Face Exists on the Internet")
11
  st.markdown("Upload your photo and we'll search for public matches using reverse image search and AI-based face comparison.")
12
 
13
- # Function to convert uploaded image to face encoding
14
- def get_face_encoding(image_file):
15
- try:
16
- image = face_recognition.load_image_file(image_file)
17
- encodings = face_recognition.face_encodings(image)
18
- if encodings:
19
- return encodings[0]
20
- else:
21
- return None
22
- except Exception as e:
23
- st.error(f"Error processing image: {e}")
24
- return None
25
 
26
- # Function to reverse search using Yandex (via redirect to their site)
27
- def generate_yandex_link(image_bytes):
28
- temp_file_path = "temp_image.jpg"
29
- with open(temp_file_path, "wb") as f:
30
- f.write(image_bytes)
31
- return "https://yandex.com/images/search?rpt=imageview&url="
 
 
32
 
33
  # UI: Upload image
34
  uploaded_file = st.file_uploader("Upload your face image (jpg/png)", type=["jpg", "jpeg", "png"])
35
 
36
  if uploaded_file:
37
  # Show uploaded image
38
- image = Image.open(uploaded_file)
39
  st.image(image, caption="Uploaded Image", use_column_width=True)
40
 
41
- # Extract face encoding
42
- encoding = get_face_encoding(uploaded_file)
43
 
44
- if encoding is not None:
45
- st.success("Face encoding extracted successfully ✅")
46
 
47
  # Convert image to bytes for reverse search
48
- img_byte_arr = io.BytesIO()
49
- image.save(img_byte_arr, format='JPEG')
50
- img_bytes = img_byte_arr.getvalue()
51
 
52
  # Provide links for manual reverse search
53
  st.subheader("🔍 Search Your Face on the Web")
@@ -55,7 +53,7 @@ if uploaded_file:
55
 
56
  with col1:
57
  st.markdown("[Search on Google](https://images.google.com)")
58
- st.caption("Upload the same image there manually")
59
 
60
  with col2:
61
  st.markdown("[Search on Yandex](https://yandex.com/images)")
@@ -70,4 +68,4 @@ if uploaded_file:
70
  st.warning("No face detected in the uploaded image. Try another photo.")
71
 
72
  st.markdown("---")
73
- st.markdown("Created with ❤️ using open-source face recognition models and public search tools.")
 
1
  import streamlit as st
 
 
2
  import numpy as np
3
  from PIL import Image
4
+ import insightface
5
+ import cv2
6
  import os
7
+ import tempfile
8
 
9
  st.set_page_config(page_title="Face Finder on Internet", layout="centered")
10
  st.title("🕵️‍♂️ Check if Your Face Exists on the Internet")
11
  st.markdown("Upload your photo and we'll search for public matches using reverse image search and AI-based face comparison.")
12
 
13
+ # Load InsightFace model
14
+ @st.cache_resource
15
+ def load_model():
16
+ model = insightface.app.FaceAnalysis(name='buffalo_l')
17
+ model.prepare(ctx_id=0)
18
+ return model
19
+
20
+ model = load_model()
 
 
 
 
21
 
22
+ # Function to extract face embeddings
23
+ def get_face_embedding(image):
24
+ image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
25
+ faces = model.get(image)
26
+ if faces:
27
+ return faces[0].embedding
28
+ else:
29
+ return None
30
 
31
  # UI: Upload image
32
  uploaded_file = st.file_uploader("Upload your face image (jpg/png)", type=["jpg", "jpeg", "png"])
33
 
34
  if uploaded_file:
35
  # Show uploaded image
36
+ image = Image.open(uploaded_file).convert("RGB")
37
  st.image(image, caption="Uploaded Image", use_column_width=True)
38
 
39
+ # Extract face embedding
40
+ embedding = get_face_embedding(image)
41
 
42
+ if embedding is not None:
43
+ st.success("Face features extracted successfully ✅")
44
 
45
  # Convert image to bytes for reverse search
46
+ img_byte_arr = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
47
+ image.save(img_byte_arr.name)
48
+ img_bytes = open(img_byte_arr.name, 'rb').read()
49
 
50
  # Provide links for manual reverse search
51
  st.subheader("🔍 Search Your Face on the Web")
 
53
 
54
  with col1:
55
  st.markdown("[Search on Google](https://images.google.com)")
56
+ st.caption("Upload the same image manually")
57
 
58
  with col2:
59
  st.markdown("[Search on Yandex](https://yandex.com/images)")
 
68
  st.warning("No face detected in the uploaded image. Try another photo.")
69
 
70
  st.markdown("---")
71
+ st.markdown("Created with ❤️ using open-source InsightFace and public search tools.")