Upload gen_ai_project1.py
Browse files- gen_ai_project1.py +60 -0
gen_ai_project1.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Gen AI Project1
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1Q27-bhi-hIw4U_QKiDXy3bwfLjPOI02o
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
#AI Powered Video Editing
|
| 11 |
+
|
| 12 |
+
#YOLOv8 (Ultralytics YOLOv8n)
|
| 13 |
+
|
| 14 |
+
!pip install datasets
|
| 15 |
+
|
| 16 |
+
!pip install ultralytics
|
| 17 |
+
|
| 18 |
+
from datasets import load_dataset
|
| 19 |
+
from moviepy.editor import ImageSequenceClip
|
| 20 |
+
from ultralytics import YOLO
|
| 21 |
+
import os
|
| 22 |
+
import cv2
|
| 23 |
+
from PIL import Image
|
| 24 |
+
|
| 25 |
+
# Load dataset
|
| 26 |
+
dataset = load_dataset("VarunB31990/Video-Editing-Dataset")
|
| 27 |
+
|
| 28 |
+
# Load YOLO model
|
| 29 |
+
model = YOLO("yolov8n.pt")
|
| 30 |
+
|
| 31 |
+
# Directory for images
|
| 32 |
+
image_dir = "images/"
|
| 33 |
+
os.makedirs(image_dir, exist_ok=True)
|
| 34 |
+
processed_dir = "processed_frames/"
|
| 35 |
+
os.makedirs(processed_dir, exist_ok=True)
|
| 36 |
+
# Process images and run YOLO
|
| 37 |
+
processed_paths = []
|
| 38 |
+
for i, item in enumerate(dataset["train"]):
|
| 39 |
+
if "original_image" in item:
|
| 40 |
+
image = item["original_image"]
|
| 41 |
+
image_path = os.path.join(image_dir, f"frame_{i}.jpg")
|
| 42 |
+
image.save(image_path)
|
| 43 |
+
# Run YOLO on the image
|
| 44 |
+
results = model(image_path)
|
| 45 |
+
for result in results:
|
| 46 |
+
im_array = result.plot() # Get YOLO detections
|
| 47 |
+
im = Image.fromarray(im_array)
|
| 48 |
+
detected_path = os.path.join(processed_dir, f"detected_{i}.jpg")
|
| 49 |
+
im.save(detected_path)
|
| 50 |
+
processed_paths.append(detected_path)
|
| 51 |
+
# Create video from processed images
|
| 52 |
+
if len(processed_paths)>1:
|
| 53 |
+
clip = ImageSequenceClip(processed_paths, fps=10)
|
| 54 |
+
clip.write_videofile("yolo_detection_video.mp4", codec="libx264", fps=10)
|
| 55 |
+
print("🎥 Video created: yolo_detection_video.mp4")
|
| 56 |
+
else:
|
| 57 |
+
print("⚠️ Not enough images to create a video.")
|
| 58 |
+
|
| 59 |
+
from IPython.display import display, Video
|
| 60 |
+
display(Video("yolo_detection_video.mp4", embed=True))
|