Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Define the folder path where images will be saved
|
6 |
+
dataset_path = "/home/user/app/dataset" # Adjust path as needed
|
7 |
+
|
8 |
+
# Ensure the directory exists
|
9 |
+
if not os.path.exists(dataset_path):
|
10 |
+
os.makedirs(dataset_path)
|
11 |
+
|
12 |
+
# Function to capture and save images
|
13 |
+
def capture_image(image, name):
|
14 |
+
person_folder = os.path.join(dataset_path, name)
|
15 |
+
os.makedirs(person_folder, exist_ok=True) # Create a folder for each person if not exists
|
16 |
+
|
17 |
+
image_count = len(os.listdir(person_folder))
|
18 |
+
image_path = os.path.join(person_folder, f"{image_count + 1}.jpg")
|
19 |
+
cv2.imwrite(image_path, cv2.cvtColor(image, cv2.COLOR_RGB2BGR)) # Save image in BGR format for OpenCV
|
20 |
+
|
21 |
+
return f"Image saved for {name} at {image_path}"
|
22 |
+
|
23 |
+
# Gradio interface for capturing images
|
24 |
+
iface = gr.Interface(
|
25 |
+
fn=capture_image,
|
26 |
+
inputs=["webcam", "text"],
|
27 |
+
outputs="text",
|
28 |
+
description="Enter your name and capture your image for the dataset."
|
29 |
+
)
|
30 |
+
|
31 |
+
iface.launch()
|