Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app3.py +126 -0
- requirements.txt +0 -0
app3.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
##STREAMLINK CODE
|
| 2 |
+
import cv2
|
| 3 |
+
import streamlink
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import time
|
| 6 |
+
import tempfile
|
| 7 |
+
import base64
|
| 8 |
+
import os
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
from openai import OpenAI
|
| 11 |
+
import assemblyai as aai
|
| 12 |
+
|
| 13 |
+
# Load environment variables
|
| 14 |
+
load_dotenv()
|
| 15 |
+
aai.settings.api_key = os.getenv("ASSEMBLYAI_API_KEY")
|
| 16 |
+
OpenAI.api_key = os.getenv("OPENAI_API_KEY")
|
| 17 |
+
client = OpenAI()
|
| 18 |
+
|
| 19 |
+
def extract_recent_frames(video_url, output_folder, duration=10, frames_per_second=1):
|
| 20 |
+
streams = streamlink.streams(video_url)
|
| 21 |
+
|
| 22 |
+
if not streams:
|
| 23 |
+
st.error("Error: Unable to retrieve streams. Make sure the YouTube video URL is valid.")
|
| 24 |
+
return
|
| 25 |
+
|
| 26 |
+
stream_url = streams['best'].url
|
| 27 |
+
|
| 28 |
+
cap = cv2.VideoCapture(stream_url)
|
| 29 |
+
|
| 30 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 31 |
+
total_frames = int(fps * duration)
|
| 32 |
+
frame_interval = int(fps / frames_per_second)
|
| 33 |
+
|
| 34 |
+
frame_count = 0
|
| 35 |
+
start_time = time.time()
|
| 36 |
+
|
| 37 |
+
extracted_frames = []
|
| 38 |
+
|
| 39 |
+
while cap.isOpened():
|
| 40 |
+
ret, frame = cap.read()
|
| 41 |
+
if not ret:
|
| 42 |
+
st.error("Error: Couldn't read frame.")
|
| 43 |
+
break
|
| 44 |
+
|
| 45 |
+
elapsed_time = time.time() - start_time
|
| 46 |
+
if frame_count % frame_interval == 0 and elapsed_time <= duration:
|
| 47 |
+
# Convert frame to base64
|
| 48 |
+
_, buffer = cv2.imencode(".jpg", frame)
|
| 49 |
+
base64_frame = base64.b64encode(buffer).decode("utf-8")
|
| 50 |
+
extracted_frames.append(base64_frame)
|
| 51 |
+
|
| 52 |
+
frame_count += 1
|
| 53 |
+
|
| 54 |
+
if elapsed_time > duration:
|
| 55 |
+
break
|
| 56 |
+
|
| 57 |
+
cap.release()
|
| 58 |
+
|
| 59 |
+
return extracted_frames
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def main():
|
| 64 |
+
st.title("Insightly Live Video Analysis")
|
| 65 |
+
|
| 66 |
+
youtube_video_url = st.text_input("Enter YouTube Video URL:")
|
| 67 |
+
duration = st.slider("Select Duration (seconds):", min_value=1, max_value=60, value=10)
|
| 68 |
+
frames_per_second = st.slider("Select Frames per Second:", min_value=1, max_value=10, value=1)
|
| 69 |
+
|
| 70 |
+
if st.button("Extract Frames"):
|
| 71 |
+
st.info("Extracting frames. Please wait...")
|
| 72 |
+
extracted_frames = extract_recent_frames(youtube_video_url, "temp_frames", duration, frames_per_second)
|
| 73 |
+
|
| 74 |
+
if extracted_frames:
|
| 75 |
+
st.success("Frames extracted successfully!")
|
| 76 |
+
|
| 77 |
+
# Display frames in a grid format with frame description on click
|
| 78 |
+
display_frame_grid(extracted_frames)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
else:
|
| 83 |
+
st.error("Failed to extract frames.")
|
| 84 |
+
|
| 85 |
+
#####################33
|
| 86 |
+
def generate_description(base64_frames):
|
| 87 |
+
try:
|
| 88 |
+
prompt_messages = [
|
| 89 |
+
{
|
| 90 |
+
"role": "user",
|
| 91 |
+
"content": [
|
| 92 |
+
"1. Generate a description for this sequence of video frames in about 90 words. Return the following: 1. List of objects in the video 2. Any restrictive content or sensitive content and if so which frame.",
|
| 93 |
+
*map(lambda x: {"image": x, "resize": 428}, base64_frames[0::30]),
|
| 94 |
+
],
|
| 95 |
+
},
|
| 96 |
+
]
|
| 97 |
+
response = client.chat.completions.create(
|
| 98 |
+
model="gpt-4-vision-preview",
|
| 99 |
+
messages=prompt_messages,
|
| 100 |
+
max_tokens=3000,
|
| 101 |
+
)
|
| 102 |
+
return response.choices[0].message.content
|
| 103 |
+
except Exception as e:
|
| 104 |
+
print(f"Error in generate_description: {e}")
|
| 105 |
+
return None
|
| 106 |
+
|
| 107 |
+
#########################################3333
|
| 108 |
+
|
| 109 |
+
def display_frame_grid(extracted_frames):
|
| 110 |
+
cols_per_row = 3
|
| 111 |
+
n_frames = len(extracted_frames)
|
| 112 |
+
for idx in range(0, n_frames, cols_per_row):
|
| 113 |
+
cols = st.columns(cols_per_row)
|
| 114 |
+
for col_index in range(cols_per_row):
|
| 115 |
+
frame_idx = idx + col_index
|
| 116 |
+
if frame_idx < n_frames:
|
| 117 |
+
with cols[col_index]:
|
| 118 |
+
# Decode base64 and display the frame
|
| 119 |
+
decoded_frame = base64.b64decode(extracted_frames[frame_idx])
|
| 120 |
+
st.image(decoded_frame, channels="BGR", caption=f'Frame {frame_idx + 1}', use_column_width=True, output_format="JPEG")
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
if __name__ == "__main__":
|
| 126 |
+
main()
|
requirements.txt
ADDED
|
Binary file (3.33 kB). View file
|
|
|