Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
video_path = ""
|
| 5 |
+
|
| 6 |
+
def upload_video(uploaded_video):
|
| 7 |
+
global video_path
|
| 8 |
+
video_path = uploaded_video
|
| 9 |
+
return f"Video uploaded: {uploaded_video}"
|
| 10 |
+
|
| 11 |
+
def process_video():
|
| 12 |
+
if not video_path:
|
| 13 |
+
return "No video uploaded. Please upload a video first."
|
| 14 |
+
|
| 15 |
+
cap = cv2.VideoCapture(video_path)
|
| 16 |
+
if not cap.isOpened():
|
| 17 |
+
return "Error: Unable to open video"
|
| 18 |
+
|
| 19 |
+
processed_frame = None
|
| 20 |
+
while True:
|
| 21 |
+
ret, frame = cap.read()
|
| 22 |
+
if not ret:
|
| 23 |
+
break
|
| 24 |
+
|
| 25 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 26 |
+
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 27 |
+
faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
| 28 |
+
|
| 29 |
+
for (x, y, w, h) in faces:
|
| 30 |
+
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
|
| 31 |
+
|
| 32 |
+
processed_frame = frame # Save the last frame with detection
|
| 33 |
+
|
| 34 |
+
cap.release()
|
| 35 |
+
if processed_frame is not None:
|
| 36 |
+
return processed_frame
|
| 37 |
+
else:
|
| 38 |
+
return "No faces detected or video is empty."
|
| 39 |
+
|
| 40 |
+
def end_processing():
|
| 41 |
+
global video_path
|
| 42 |
+
video_path = ""
|
| 43 |
+
return "Processing ended. You can upload a new video now."
|
| 44 |
+
|
| 45 |
+
iface = gr.Blocks()
|
| 46 |
+
|
| 47 |
+
with iface:
|
| 48 |
+
gr.Markdown("## Video Face Detection Tool")
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
video_input = gr.Video(label="Upload Video")
|
| 52 |
+
upload_button = gr.Button("Upload")
|
| 53 |
+
upload_output = gr.Textbox(label="Upload Status")
|
| 54 |
+
|
| 55 |
+
upload_button.click(upload_video, inputs=video_input, outputs=upload_output)
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
process_button = gr.Button("Process Video")
|
| 59 |
+
end_button = gr.Button("End and New Video")
|
| 60 |
+
process_output = gr.Image(label="Processed Video")
|
| 61 |
+
end_output = gr.Textbox(label="Process Status")
|
| 62 |
+
|
| 63 |
+
process_button.click(process_video, outputs=process_output)
|
| 64 |
+
end_button.click(end_processing, outputs=end_output)
|
| 65 |
+
|
| 66 |
+
iface.launch(share=True) # `share=True` allows Hugging Face integration
|