Spaces:
Runtime error
Runtime error
SaladSlayer00
commited on
Commit
•
6b368fb
1
Parent(s):
e1b0ac8
Update app.py
Browse files
app.py
CHANGED
@@ -8,86 +8,101 @@ aws_secret_access_key = os.getenv('AWS_SECRET_ACCESS_KEY')
|
|
8 |
|
9 |
s3_client = boto3.client(
|
10 |
's3',
|
11 |
-
aws_access_key_id=
|
12 |
-
aws_secret_access_key=
|
13 |
region_name='eu-central-1'
|
14 |
)
|
15 |
|
16 |
def upload_to_s3(bucket_name, folder_name):
|
17 |
-
|
18 |
for filename in os.listdir(folder_name):
|
19 |
if filename.endswith('.png'):
|
20 |
file_path = os.path.join(folder_name, filename)
|
21 |
s3_client.upload_file(file_path, bucket_name, f"{folder_name}/{filename}")
|
|
|
|
|
22 |
|
23 |
-
def process_video(uploaded_video, name, surname, interval_ms):
|
24 |
try:
|
25 |
-
if
|
26 |
-
|
|
|
27 |
|
28 |
folder_name = f"{name}_{surname}"
|
29 |
os.makedirs(folder_name, exist_ok=True)
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
-
|
34 |
-
# Initialize face detector
|
35 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
36 |
-
|
37 |
-
# Open and process the video
|
38 |
vidcap = cv2.VideoCapture(temp_video_path)
|
39 |
if not vidcap.isOpened():
|
40 |
raise Exception("Failed to open video file.")
|
41 |
|
42 |
fps = vidcap.get(cv2.CAP_PROP_FPS)
|
43 |
-
frame_interval = int(fps * (interval_ms /
|
44 |
-
|
45 |
frame_count = 0
|
46 |
saved_image_count = 0
|
47 |
success, image = vidcap.read()
|
|
|
|
|
48 |
while success and saved_image_count < 86:
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
55 |
face = image[y:y+h, x:x+w]
|
56 |
face_resized = cv2.resize(face, (160, 160))
|
57 |
-
|
|
|
|
|
58 |
saved_image_count += 1
|
59 |
-
|
60 |
-
|
61 |
|
62 |
-
|
63 |
-
|
64 |
|
65 |
-
vidcap.release()
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
upload_to_s3(bucket_name, folder_name)
|
70 |
-
|
71 |
-
return f"Saved and uploaded {saved_image_count} face images"
|
72 |
|
|
|
|
|
73 |
|
74 |
-
return f"Saved {saved_image_count} face images
|
75 |
|
76 |
except Exception as e:
|
77 |
-
return f"An error occurred: {e}"
|
78 |
|
|
|
79 |
with gr.Blocks() as demo:
|
|
|
80 |
with gr.Row():
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
submit_button.click(
|
88 |
fn=process_video,
|
89 |
-
inputs=[video, name, surname, interval],
|
90 |
-
outputs=[gr.Text(label="Result")]
|
91 |
)
|
92 |
|
|
|
|
|
|
|
|
|
|
|
93 |
demo.launch()
|
|
|
8 |
|
9 |
s3_client = boto3.client(
|
10 |
's3',
|
11 |
+
aws_access_key_id=aws_access_key_id,
|
12 |
+
aws_secret_access_key=aws_secret_access_key,
|
13 |
region_name='eu-central-1'
|
14 |
)
|
15 |
|
16 |
def upload_to_s3(bucket_name, folder_name):
|
17 |
+
image_paths = []
|
18 |
for filename in os.listdir(folder_name):
|
19 |
if filename.endswith('.png'):
|
20 |
file_path = os.path.join(folder_name, filename)
|
21 |
s3_client.upload_file(file_path, bucket_name, f"{folder_name}/{filename}")
|
22 |
+
image_paths.append(file_path)
|
23 |
+
return image_paths
|
24 |
|
25 |
+
def process_video(uploaded_video, capture_video, name, surname, interval_ms):
|
26 |
try:
|
27 |
+
video_source = capture_video if capture_video else uploaded_video
|
28 |
+
if video_source is None:
|
29 |
+
return "No video file provided.", []
|
30 |
|
31 |
folder_name = f"{name}_{surname}"
|
32 |
os.makedirs(folder_name, exist_ok=True)
|
33 |
|
34 |
+
# Video processing logic
|
35 |
+
# Use video_source directly as it's a file path (string)
|
36 |
+
temp_video_path = video_source
|
|
|
37 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
|
|
|
|
38 |
vidcap = cv2.VideoCapture(temp_video_path)
|
39 |
if not vidcap.isOpened():
|
40 |
raise Exception("Failed to open video file.")
|
41 |
|
42 |
fps = vidcap.get(cv2.CAP_PROP_FPS)
|
43 |
+
frame_interval = int(fps * (interval_ms / 1000))
|
|
|
44 |
frame_count = 0
|
45 |
saved_image_count = 0
|
46 |
success, image = vidcap.read()
|
47 |
+
image_paths = []
|
48 |
+
|
49 |
while success and saved_image_count < 86:
|
50 |
+
if frame_count % frame_interval == 0:
|
51 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
52 |
+
faces = face_cascade.detectMultiScale(gray, 1.2, 4)
|
53 |
+
for (x, y, w, h) in faces:
|
54 |
+
# Additional checks for face region validation
|
55 |
+
aspect_ratio = w / h
|
56 |
+
if aspect_ratio > 0.75 and aspect_ratio < 1.33 and w * h > 4000: # Example thresholds
|
57 |
face = image[y:y+h, x:x+w]
|
58 |
face_resized = cv2.resize(face, (160, 160))
|
59 |
+
image_filename = os.path.join(folder_name, f"{name}_{surname}_{saved_image_count:04d}.png")
|
60 |
+
cv2.imwrite(image_filename, face_resized)
|
61 |
+
image_paths.append(image_filename)
|
62 |
saved_image_count += 1
|
63 |
+
if saved_image_count >= 86:
|
64 |
+
break
|
65 |
|
66 |
+
success, image = vidcap.read()
|
67 |
+
frame_count += 1
|
68 |
|
|
|
69 |
|
70 |
+
vidcap.release()
|
|
|
|
|
|
|
|
|
71 |
|
72 |
+
bucket_name = 'newimagesupload00'
|
73 |
+
uploaded_images = upload_to_s3(bucket_name, folder_name)
|
74 |
|
75 |
+
return f"Saved and uploaded {saved_image_count} face images", uploaded_images
|
76 |
|
77 |
except Exception as e:
|
78 |
+
return f"An error occurred: {e}", []
|
79 |
|
80 |
+
# Gradio Interface
|
81 |
with gr.Blocks() as demo:
|
82 |
+
gr.Markdown("### Video Face Detector and Uploader")
|
83 |
with gr.Row():
|
84 |
+
with gr.Column():
|
85 |
+
video = gr.File(label="Upload Your Video")
|
86 |
+
capture = gr.Video(label="Or Capture from Camera")
|
87 |
+
with gr.Column():
|
88 |
+
name = gr.Textbox(label="Name")
|
89 |
+
surname = gr.Textbox(label="Surname")
|
90 |
+
interval = gr.Number(label="Interval in milliseconds", value=100)
|
91 |
+
submit_button = gr.Button("Submit")
|
92 |
+
with gr.Column():
|
93 |
+
gallery = gallery = gr.Gallery(
|
94 |
+
label="Generated images", show_label=False, elem_id="gallery"
|
95 |
+
, columns=[3], rows=[1], object_fit="contain", height="auto")
|
96 |
|
97 |
submit_button.click(
|
98 |
fn=process_video,
|
99 |
+
inputs=[video, capture, name, surname, interval],
|
100 |
+
outputs=[gr.Text(label="Result"), gallery]
|
101 |
)
|
102 |
|
103 |
+
# CSS for styling (optional)
|
104 |
+
css = """
|
105 |
+
body { font-family: Arial, sans-serif; }
|
106 |
+
"""
|
107 |
+
|
108 |
demo.launch()
|