File size: 2,142 Bytes
f171e09
0c1e78f
 
f171e09
5caf045
132c799
c4479c0
 
132c799
c4479c0
 
132c799
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4479c0
 
132c799
c4479c0
132c799
c4479c0
 
132c799
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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)