| from PIL import Image |
| import streamlit as st |
| import cv2 |
| import numpy as np |
| import os |
|
|
| |
| st.title("Face Detection and Image Capture") |
| Name = st.text_input('Enter Your Name:') |
|
|
| |
| face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') |
|
|
| |
| cap = cv2.VideoCapture(0) |
| cap.set(3, 640) |
| cap.set(4, 480) |
|
|
| |
| save_dir = "images" |
|
|
| |
| def detect_faces(image): |
| gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
| faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5) |
| for (x, y, w, h) in faces: |
| cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2) |
| return image |
|
|
| |
| image_file = st.camera_input("Take a picture") |
|
|
| |
| if image_file: |
| try: |
| |
| pil_image = Image.open(image_file) |
|
|
| |
| image_np = np.array(pil_image) |
|
|
| |
| image_with_faces = detect_faces(image_np) |
|
|
| |
| rgb_image = cv2.cvtColor(image_with_faces, cv2.COLOR_BGR2RGB) |
| |
| st.image(rgb_image, channels="RGB", use_column_width=True) |
| |
| |
| image_filename = f"{Name}.jpg" |
| image_path = os.path.join(save_dir + image_filename) |
| |
| |
| cv2.imwrite(image_path, cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)) |
| |
| st.success(f"Image saved as {image_path}") |
| except Exception as e: |
| st.error(f"Error saving image: {str(e)}") |
|
|
| |
| cap.release() |