RajaThor commited on
Commit
3709805
·
verified ·
1 Parent(s): 0f0619b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -33
app.py CHANGED
@@ -154,6 +154,7 @@ def recognize_face(image_path):
154
  return "Please upload an image."
155
 
156
  try:
 
157
  unknown_encodings = load_and_encode(image_path)
158
  if not unknown_encodings:
159
  return "No face found in the provided image."
@@ -165,49 +166,34 @@ def recognize_face(image_path):
165
  known_encoding = np.array(data["encoding"])
166
  if face_recognition.compare_faces([known_encoding], unknown_encoding)[0]:
167
  info = data["info"]
 
168
  email = info.get("email", "Email not provided")
169
- insta_handle = info["instagram_handle"]
170
-
171
- # Instagram API request
172
- url = "https://instagram-scraper-api2.p.rapidapi.com/v1/info"
173
- querystring = {"username_or_id_or_url": insta_handle}
174
- headers = {
175
- "x-rapidapi-key": "bc2b15f577msh345ac5a5b422c7ep106f73jsn580b1c25e0d9",
176
- "x-rapidapi-host": "instagram-scraper-api2.p.rapidapi.com"
177
- }
178
-
179
- try:
180
- response = requests.get(url, headers=headers, params=querystring)
181
- response.raise_for_status() # Raise an exception for HTTP errors
182
- data = response.json().get('data', {})
183
- print("API Response Data:", data) # Debug print
184
- full_name = data.get('full_name', 'N/A')
185
- biography = data.get('biography', 'N/A')
186
- follower_count = data.get('follower_count', 'N/A')
187
- following_count = data.get('following_count', 'N/A')
188
- except requests.exceptions.HTTPError as http_err:
189
- print(f"HTTP error occurred: {http_err}")
190
- full_name, biography, follower_count, following_count = 'N/A', 'N/A', 'N/A', 'N/A'
191
- except Exception as err:
192
- print(f"Other error occurred: {err}")
193
- full_name, biography, follower_count, following_count = 'N/A', 'N/A', 'N/A', 'N/A'
194
-
195
- face_matches.append((name, insta_handle, email, full_name, biography, follower_count, following_count))
196
 
197
  if face_matches:
198
  matches.extend(face_matches)
199
  else:
200
- matches.append(("Unknown", "Unknown", "Unknown", 'N/A', 'N/A', 'N/A', 'N/A'))
201
 
202
  if matches:
203
  results = []
204
- for name, insta_handle, email, full_name, biography, follower_count, following_count in matches:
205
  insta_link = f"https://www.instagram.com/{insta_handle}/"
206
  insta_link_html = f'<a href="{insta_link}" target="_blank"><font color="red">{insta_handle}</font></a>'
207
- results.append(
208
- f"- It's a picture of {name}! Insta handle: {insta_link_html}, Email: {email}, "
209
- f"Full Name: {full_name}, Biography: {biography}, Followers: {follower_count}, Following: {following_count}"
210
- )
211
  log_action(st.session_state.auth_state["user"].email, "Recognized face")
212
  return "\n".join(results)
213
  else:
@@ -215,6 +201,27 @@ def recognize_face(image_path):
215
  except Exception as e:
216
  return f"Failed to recognize face: {str(e)}"
217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
 
219
  # Update recognize_face_optimal function to handle multiple face encodings
220
  def recognize_face_optimal(image_path):
 
154
  return "Please upload an image."
155
 
156
  try:
157
+ # Assuming load_and_encode function loads and encodes the image
158
  unknown_encodings = load_and_encode(image_path)
159
  if not unknown_encodings:
160
  return "No face found in the provided image."
 
166
  known_encoding = np.array(data["encoding"])
167
  if face_recognition.compare_faces([known_encoding], unknown_encoding)[0]:
168
  info = data["info"]
169
+ instagram_handle = info.get("instagram_handle")
170
  email = info.get("email", "Email not provided")
171
+
172
+ # Fetch additional Instagram data
173
+ if instagram_handle:
174
+ insta_data = fetch_instagram_data(instagram_handle)
175
+ if insta_data:
176
+ edge_followed_by = insta_data.get('edge_followed_by', {}).get('count')
177
+ edge_follow = insta_data.get('edge_follow', {}).get('count')
178
+ full_name = insta_data.get('full_name')
179
+ biography = insta_data.get('biography')
180
+ face_matches.append((name, instagram_handle, email, edge_followed_by, edge_follow, full_name, biography))
181
+ else:
182
+ face_matches.append((name, instagram_handle, email, "N/A", "N/A", "Unknown", "Unknown"))
183
+ else:
184
+ face_matches.append((name, "Unknown", email, "N/A", "N/A", "Unknown", "Unknown"))
 
 
 
 
 
 
 
 
 
 
 
 
 
185
 
186
  if face_matches:
187
  matches.extend(face_matches)
188
  else:
189
+ matches.append(("Unknown", "Unknown", "Unknown", "N/A", "N/A", "Unknown", "Unknown"))
190
 
191
  if matches:
192
  results = []
193
+ for name, insta_handle, email, edge_followed_by, edge_follow, full_name, biography in matches:
194
  insta_link = f"https://www.instagram.com/{insta_handle}/"
195
  insta_link_html = f'<a href="{insta_link}" target="_blank"><font color="red">{insta_handle}</font></a>'
196
+ results.append(f"- It's a picture of {name}! Insta handle: {insta_link_html}, Email: {email}, Followers: {edge_followed_by}, Following: {edge_follow}, Full Name: {full_name}, Biography: {biography}")
 
 
 
197
  log_action(st.session_state.auth_state["user"].email, "Recognized face")
198
  return "\n".join(results)
199
  else:
 
201
  except Exception as e:
202
  return f"Failed to recognize face: {str(e)}"
203
 
204
+ def fetch_instagram_data(instagram_handle):
205
+ url = f"https://instagram-scraper-20231.p.rapidapi.com/userinfo/{instagram_handle}"
206
+ headers = {
207
+ "x-rapidapi-key": "bc2b15f577msh345ac5a5b422c7ep106f73jsn580b1c25e0d9",
208
+ "x-rapidapi-host": "instagram-scraper-20231.p.rapidapi.com"
209
+ }
210
+
211
+ try:
212
+ response = requests.get(url, headers=headers)
213
+ if response.status_code == 200:
214
+ data = response.json()
215
+ if data.get('status') == 'success':
216
+ return data.get('data', {})
217
+ else:
218
+ return None
219
+ else:
220
+ return None
221
+ except Exception as e:
222
+ print(f"Error fetching Instagram data: {str(e)}")
223
+ return None
224
+
225
 
226
  # Update recognize_face_optimal function to handle multiple face encodings
227
  def recognize_face_optimal(image_path):