Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import gradio as gr
|
3 |
+
import google.generativeai as genai
|
4 |
+
import os
|
5 |
+
import PIL.Image
|
6 |
+
|
7 |
+
# Configure the API key for Google Generative AI
|
8 |
+
genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
|
9 |
+
|
10 |
+
# Define the Generative AI model
|
11 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
12 |
+
|
13 |
+
# Function to capture frames from a video
|
14 |
+
def frame_capture(video_path, num_frames=5):
|
15 |
+
vidObj = cv2.VideoCapture(video_path)
|
16 |
+
frames = []
|
17 |
+
total_frames = int(vidObj.get(cv2.CAP_PROP_FRAME_COUNT))
|
18 |
+
frame_step = max(1, total_frames // num_frames)
|
19 |
+
count = 0
|
20 |
+
|
21 |
+
while len(frames) < num_frames:
|
22 |
+
vidObj.set(cv2.CAP_PROP_POS_FRAMES, count)
|
23 |
+
success, image = vidObj.read()
|
24 |
+
if not success:
|
25 |
+
break
|
26 |
+
frames.append(image)
|
27 |
+
count += frame_step
|
28 |
+
|
29 |
+
vidObj.release()
|
30 |
+
return frames
|
31 |
+
|
32 |
+
# Function to generate text descriptions for frames
|
33 |
+
def generate_descriptions_for_frames(video_path):
|
34 |
+
frames = frame_capture(video_path)
|
35 |
+
images = [PIL.Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) for frame in frames]
|
36 |
+
|
37 |
+
prompt = "Describe what is happening in each of these frames."
|
38 |
+
images_with_prompt = [prompt] + images
|
39 |
+
|
40 |
+
responses = model.generate_content(images_with_prompt)
|
41 |
+
descriptions = [response.text for response in responses]
|
42 |
+
|
43 |
+
formatted_description = format_descriptions(descriptions)
|
44 |
+
return formatted_description
|
45 |
+
|
46 |
+
# Helper function to format descriptions
|
47 |
+
def format_descriptions(descriptions):
|
48 |
+
return ' '.join(descriptions).strip()
|
49 |
+
|
50 |
+
# Define Gradio interface
|
51 |
+
video_input = gr.Video(label="Upload or Record Video", source="upload", type="filepath")
|
52 |
+
output_text = gr.Textbox(label="Video Analysis")
|
53 |
+
|
54 |
+
# Create Gradio app
|
55 |
+
gr.Interface(fn=generate_descriptions_for_frames, inputs=video_input, outputs=output_text, title="Video Content Detection System").launch()
|