Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -29,33 +29,44 @@ def frame_capture(video_path, num_frames=5):
|
|
| 29 |
vidObj.release()
|
| 30 |
return frames
|
| 31 |
|
| 32 |
-
# Function to generate text descriptions for frames
|
| 33 |
-
def
|
| 34 |
frames = frame_capture(video_path)
|
| 35 |
images = [PIL.Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) for frame in frames]
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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 Video", autoplay=True)
|
|
|
|
| 52 |
user_input = gr.Textbox(label="Ask something specific about the video", placeholder="E.g., Are there any cars in this video?")
|
| 53 |
-
output_text = gr.Textbox(label="What's in this video")
|
| 54 |
|
| 55 |
# Create Gradio app
|
| 56 |
-
gr.
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
| 29 |
vidObj.release()
|
| 30 |
return frames
|
| 31 |
|
| 32 |
+
# Function to generate text descriptions for frames or answer a specific question
|
| 33 |
+
def analyze_video(video_path, user_question):
|
| 34 |
frames = frame_capture(video_path)
|
| 35 |
images = [PIL.Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) for frame in frames]
|
| 36 |
|
| 37 |
+
if user_question.strip():
|
| 38 |
+
prompt = f"Based on these video frames, {user_question}"
|
| 39 |
+
else:
|
| 40 |
+
prompt = "Describe what is happening in each of these frames in this video sequentially."
|
| 41 |
+
|
| 42 |
images_with_prompt = [prompt] + images
|
| 43 |
|
| 44 |
responses = model.generate_content(images_with_prompt)
|
| 45 |
descriptions = [response.text for response in responses]
|
| 46 |
|
| 47 |
+
return descriptions[-1] if user_question.strip() else format_descriptions(descriptions)
|
|
|
|
| 48 |
|
| 49 |
# Helper function to format descriptions
|
| 50 |
def format_descriptions(descriptions):
|
| 51 |
return ' '.join(descriptions).strip()
|
| 52 |
|
| 53 |
+
# Function to handle chat interaction
|
| 54 |
+
def chat_interaction(video_path, chatbot, user_message):
|
| 55 |
+
response = analyze_video(video_path, user_message)
|
| 56 |
+
chatbot.append((user_message, response))
|
| 57 |
+
return "", chatbot
|
| 58 |
+
|
| 59 |
# Define Gradio interface
|
| 60 |
video_input = gr.Video(label="Upload Video", autoplay=True)
|
| 61 |
+
chatbot = gr.Chatbot(label="Video Analysis Chatbot")
|
| 62 |
user_input = gr.Textbox(label="Ask something specific about the video", placeholder="E.g., Are there any cars in this video?")
|
|
|
|
| 63 |
|
| 64 |
# Create Gradio app
|
| 65 |
+
with gr.Blocks() as demo:
|
| 66 |
+
with gr.Column():
|
| 67 |
+
video_input.render()
|
| 68 |
+
chatbot.render()
|
| 69 |
+
user_input.render()
|
| 70 |
+
user_input.submit(fn=chat_interaction, inputs=[video_input, chatbot, user_input], outputs=[user_input, chatbot])
|
| 71 |
+
|
| 72 |
+
demo.launch()
|