File size: 1,856 Bytes
7547a78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
"""Gen AI Project1

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1Q27-bhi-hIw4U_QKiDXy3bwfLjPOI02o
"""

#AI Powered Video Editing

#YOLOv8 (Ultralytics YOLOv8n)

!pip install datasets

!pip install ultralytics

from datasets import load_dataset
from moviepy.editor import ImageSequenceClip
from ultralytics import YOLO
import os
import cv2
from PIL import Image

# Load dataset
dataset = load_dataset("VarunB31990/Video-Editing-Dataset")

# Load YOLO model
model = YOLO("yolov8n.pt")

# Directory for images
image_dir = "images/"
os.makedirs(image_dir, exist_ok=True)
processed_dir = "processed_frames/"
os.makedirs(processed_dir, exist_ok=True)
# Process images and run YOLO
processed_paths = []
for i, item in enumerate(dataset["train"]):
    if "original_image" in item:
                image = item["original_image"]
                image_path = os.path.join(image_dir, f"frame_{i}.jpg")
                image.save(image_path)
                # Run YOLO on the image
                results = model(image_path)
                for result in results:
                    im_array = result.plot()  # Get YOLO detections
                    im = Image.fromarray(im_array)
                    detected_path = os.path.join(processed_dir, f"detected_{i}.jpg")
                    im.save(detected_path)
                    processed_paths.append(detected_path)
# Create video from processed images
if len(processed_paths)>1:
   clip = ImageSequenceClip(processed_paths, fps=10)
   clip.write_videofile("yolo_detection_video.mp4", codec="libx264", fps=10)
   print("🎥 Video created: yolo_detection_video.mp4")
else:
   print("⚠️ Not enough images to create a video.")

from IPython.display import display, Video
display(Video("yolo_detection_video.mp4", embed=True))