LovnishVerma's picture
Update app.py
132c799 verified
import os
import cv2
import numpy as np
import streamlit as st
from datetime import datetime
from huggingface_hub import HfApi
# Constants
KNOWN_FACES_DIR = "known_faces"
IMG_SIZE = (200, 200)
# Initialize Hugging Face API
api = HfApi()
# Helper Function to upload image to Hugging Face
def upload_to_huggingface(image_path, repo_id="LovnishVerma/face__emotion_detection"):
try:
api.upload_file(
path_or_fileobj=image_path,
path_in_repo=os.path.basename(image_path), # Name of the image in the repo
repo_id=repo_id,
repo_type="dataset" # You can also set it as "model" if uploading to a model repo
)
st.success(f"Photo uploaded to Hugging Face repository: {repo_id}")
except Exception as e:
st.error(f"Error uploading photo: {e}")
# Streamlit App
st.title("Webcam Photo Capture and Upload to Hugging Face")
st.sidebar.title("Options")
option = st.sidebar.selectbox("Choose an action", ["Home", "Capture Photo"])
if option == "Home":
st.write("Capture a photo using your webcam and upload it to Hugging Face.")
elif option == "Capture Photo":
# Ask the user to capture a photo using webcam
photo = st.camera_input("Capture a photo")
if photo is not None:
# Convert the uploaded photo to an image (using PIL or OpenCV)
img = cv2.imdecode(np.frombuffer(photo.getvalue(), np.uint8), cv2.IMREAD_COLOR)
if img is not None:
# Save the photo to a temporary file
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
photo_path = f"temp_photo_{timestamp}.jpg"
cv2.imwrite(photo_path, img)
# Display the photo
st.image(img, caption="Captured Photo", channels="BGR")
# Ask the user if they want to upload the photo
if st.button("Upload Photo to Hugging Face"):
# Replace with your Hugging Face repository
upload_to_huggingface(photo_path, repo_id="your-username/your-repo")
# Optionally, delete the temporary photo file after upload
os.remove(photo_path)