File size: 1,554 Bytes
bbcc5b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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)