Spaces:
Sleeping
Sleeping
import cv2 | |
import numpy as np | |
from utils.imageEmbedding.index import get_image_embedding | |
from utils.imageToText.index import extract_text | |
def get_video_embedding(video_url): | |
try: | |
cap = cv2.VideoCapture(video_url) | |
fps = cap.get(cv2.CAP_PROP_FPS) | |
interval = int(fps) # Capture a frame every second | |
frame_count = 0 | |
video_embeddings = [] | |
while(cap.isOpened()): | |
ret, frame = cap.read() | |
if ret: | |
if frame_count % interval == 0: | |
# Convert frame to binary format | |
ret, buffer = cv2.imencode('.jpg', frame) | |
if not ret: | |
continue | |
# Convert frame binary data to bytes | |
frame_bytes = buffer.tobytes() | |
# Get image embedding for the frame | |
extracted_text = extract_text(frame_bytes) | |
image_embedding = get_image_embedding(frame_bytes) | |
image_embedding_list = image_embedding.tolist() | |
video_embeddings.append({"image_embedding": image_embedding_list ,"extracted_text":extracted_text}) | |
frame_count += 1 | |
else: | |
break | |
cap.release() | |
return video_embeddings | |
except Exception as e: | |
print(e) | |
# Example usage: | |
# video_url = "https://utfs.io/f/ef6c037f-fa61-471a-8956-562bc2d62531-fzxs1i.mp4" | |
# video_embeddings = get_video_embedding(video_url) | |
# print("Video Embeddings:", video_embeddings) |