import streamlit as st from PIL import Image, ImageDraw import face_recognition import numpy as np # Load known face encodings and names ruthchild_image = face_recognition.load_image_file("picturesofknownpeople/ruthchild.jpg") ruthchild_face_encoding = face_recognition.face_encodings(ruthchild_image)[0] harryking_image = face_recognition.load_image_file("picturesofknownpeople/harryking.jpg") harryking_face_encoding = face_recognition.face_encodings(harryking_image)[0] barackobama_image = face_recognition.load_image_file("picturesofknownpeople/barack_obama.jpg") barackobama_face_encoding = face_recognition.face_encodings(barackobama_image)[0] michelleobama_image = face_recognition.load_image_file("picturesofknownpeople/michelle_obama.jpg") michelleobama_face_encoding = face_recognition.face_encodings(michelleobama_image)[0] biden_image = face_recognition.load_image_file("picturesofknownpeople/biden.jpg") biden_face_encoding = face_recognition.face_encodings(biden_image)[0] known_face_encodings = [ ruthchild_face_encoding, harryking_face_encoding, barackobama_face_encoding, michelleobama_face_encoding, biden_face_encoding ] known_face_names = [ "ruthchild", "harryking", "barackobama", "michelleobama", "biden" ] def main(): st.set_page_config(page_title="Insightly", page_icon="https://i.ibb.co/bX6GdqG/insightly-wbg.png") st.sidebar.image("https://i.ibb.co/bX6GdqG/insightly-wbg.png", use_column_width=True) st.title("Face Recognition with Streamlit") uploaded_image = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"]) if uploaded_image is not None: unknown_image = face_recognition.load_image_file(uploaded_image) # Find faces in the uploaded image face_locations = face_recognition.face_locations(unknown_image) face_encodings = face_recognition.face_encodings(unknown_image, face_locations) # Create a Pillow ImageDraw Draw instance to draw on the image pil_image = Image.fromarray(unknown_image) draw = ImageDraw.Draw(pil_image) for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): matches = face_recognition.compare_faces(known_face_encodings, face_encoding) name = "Unknown" face_distances = face_recognition.face_distance(known_face_encodings, face_encoding) best_match_index = np.argmin(face_distances) if matches[best_match_index]: name = known_face_names[best_match_index] # Draw a box and label on the image draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255)) text_width, text_height = draw.textsize(name) draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(0, 0, 255), outline=(0, 0, 255)) draw.text((left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 255)) st.image(pil_image, caption="Processed Image", use_column_width=True) if __name__ == "__main__": main()