Skip to content
Open

Main #381

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
390 changes: 390 additions & 0 deletions Extrating_images_with_labels_from_videos.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,390 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"gpuType": "T4",
"authorship_tag": "ABX9TyNZpcELM44Sax8rDaoqF7I0",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/AmirAziz1221/computer-vision-course/blob/main/Extrating_images_with_labels_from_videos.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "woyyurXohzHE",
"outputId": "062f9497-41b0-4d7c-f849-88cc2ffecf4c"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"
]
}
],
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')"
]
},
{
"cell_type": "code",
"source": [
"import cv2\n",
"import os\n",
"\n",
"# Paths\n",
"video_path = '/content/drive/MyDrive/Colab Notebooks/flood shared with Amir/flood videos/Flood In Booni Upper Chitral Pakistan_part_1.avi'\n",
"output_images_dir = '/content/drive/MyDrive/Water Segmentation/frames/train/images'\n",
"output_labels_dir = '/content/drive/MyDrive/Water Segmentation/frames/train/labels'\n",
"\n",
"# Create output directories if they don't exist\n",
"os.makedirs(output_images_dir, exist_ok=True) # Create images output directory if it doesn't exist\n",
"os.makedirs(output_labels_dir, exist_ok=True) # Create labels output directory if it doesn't exist\n",
"\n",
"# Open video\n",
"cap = cv2.VideoCapture(video_path)\n",
"frame_rate = cap.get(cv2.CAP_PROP_FPS) # Frames per second\n",
"frame_count = 0\n",
"save_frequency = int(frame_rate) # Modify to control frame save interval\n",
"\n",
"# Example bounding boxes: (x_min, y_min, x_max, y_max, class_id)\n",
"# You need to replace this with actual logic to get bounding boxes if necessary\n",
"frame_boxes = {\n",
" 0: [(10, 10, 50, 50, 1)], # frame_number: [(x_min, y_min, x_max, y_max, class), ...]\n",
" 1: [(20, 20, 60, 60, 1)],\n",
" # Add more frames with their bounding boxes as needed\n",
"}\n",
"\n",
"while cap.isOpened():\n",
" ret, frame = cap.read()\n",
" if not ret:\n",
" break\n",
"\n",
" # Check if we should save this frame\n",
" if frame_count % save_frequency == 0:\n",
" frame_filename = os.path.join(output_images_dir, f'frame_{frame_count}.jpg')\n",
"\n",
" # Get bounding boxes for the current frame (or set empty if none)\n",
" boxes = frame_boxes.get(frame_count, [])\n",
"\n",
" # Draw bounding boxes on the frame\n",
" for (x_min, y_min, x_max, y_max, cls) in boxes:\n",
" cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2) # Draw rectangle\n",
" cv2.putText(frame, str(cls), (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) # Class label\n",
"\n",
" # Save the labeled frame as an image\n",
" cv2.imwrite(frame_filename, frame)\n",
"\n",
" # Save bounding boxes for this frame in a text file\n",
" label_filename = os.path.join(output_labels_dir, f'frame_{frame_count}.txt')\n",
" with open(label_filename, 'w') as label_file:\n",
" for (x_min, y_min, x_max, y_max, cls) in boxes:\n",
" label_file.write(f\"{cls} {x_min} {y_min} {x_max} {y_max}\\n\")\n",
"\n",
" frame_count += 1\n",
"\n",
"cap.release()"
],
"metadata": {
"id": "TX8NmITCh9e-"
},
"execution_count": 23,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import cv2\n",
"import os\n",
"\n",
"# Paths\n",
"video_path = '/content/drive/MyDrive/Water Segmentation/combine video.mp4'\n",
"output_images_dir = '/content/drive/MyDrive/Water Segmentation/frames/train/images'\n",
"output_labels_dir = '/content/drive/MyDrive/Water Segmentation/frames/train/labels'\n",
"\n",
"# Create output directories if they don't exist\n",
"os.makedirs(output_images_dir, exist_ok=True) # Create images output directory if it doesn't exist\n",
"os.makedirs(output_labels_dir, exist_ok=True) # Create labels output directory if it doesn't exist\n",
"\n",
"# Open video\n",
"cap = cv2.VideoCapture(video_path)\n",
"frame_rate = cap.get(cv2.CAP_PROP_FPS) # Frames per second\n",
"frame_count = 0\n",
"save_frequency = int(frame_rate) # Modify to control frame save interval\n",
"\n",
"# Example bounding boxes: (x_min, y_min, x_max, y_max, class_id)\n",
"# You need to replace this with actual logic to get bounding boxes if necessary\n",
"frame_boxes = {\n",
" 0: [(10, 10, 50, 50, 1)], # frame_number: [(x_min, y_min, x_max, y_max, class), ...]\n",
" 1: [(20, 20, 60, 60, 1)],\n",
" # Add more frames with their bounding boxes as needed\n",
"}\n",
"\n",
"while cap.isOpened():\n",
" ret, frame = cap.read()\n",
" if not ret:\n",
" break\n",
"\n",
" # Check if we should save this frame\n",
" if frame_count % save_frequency == 0:\n",
" frame_filename = os.path.join(output_images_dir, f'frame_{frame_count}.jpg')\n",
"\n",
" # Get bounding boxes for the current frame (or set empty if none)\n",
" boxes = frame_boxes.get(frame_count, [])\n",
"\n",
" # Draw bounding boxes on the frame\n",
" for (x_min, y_min, x_max, y_max, cls) in boxes:\n",
" cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2) # Draw rectangle\n",
" cv2.putText(frame, str(cls), (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) # Class label\n",
"\n",
" # Save the labeled frame as an image\n",
" cv2.imwrite(frame_filename, frame)\n",
"\n",
" # Save bounding boxes for this frame in a text file\n",
" label_filename = os.path.join(output_labels_dir, f'frame_{frame_count}.txt')\n",
" with open(label_filename, 'w') as label_file:\n",
" for (x_min, y_min, x_max, y_max, cls) in boxes:\n",
" label_file.write(f\"{cls} {x_min} {y_min} {x_max} {y_max}\\n\")\n",
"\n",
" frame_count += 1\n",
"\n",
"cap.release()\n"
],
"metadata": {
"id": "mHBfHpf5lnZI"
},
"execution_count": 22,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import cv2\n",
"import os\n",
"\n",
"# Paths\n",
"video_path = '/content/drive/MyDrive/Water Segmentation/combine video.mp4'\n",
"output_images_dir = '/content/drive/MyDrive/Water Segmentation/frames/train/images'\n",
"output_labels_dir = '/content/drive/MyDrive/Water Segmentation/frames/train/labels'\n",
"\n",
"# Create output directories if they don't exist\n",
"os.makedirs(output_images_dir, exist_ok=True) # Create images output directory if it doesn't exist\n",
"os.makedirs(output_labels_dir, exist_ok=True) # Create labels output directory if it doesn't exist\n",
"\n",
"# Open video\n",
"cap = cv2.VideoCapture(video_path)\n",
"frame_rate = cap.get(cv2.CAP_PROP_FPS) # Frames per second\n",
"frame_count = 0\n",
"save_frequency = int(frame_rate) # Modify to control frame save interval\n",
"\n",
"# Example bounding boxes: (x_min, y_min, x_max, y_max, class_id)\n",
"# You need to replace this with actual logic to get bounding boxes if necessary\n",
"frame_boxes = {\n",
" 0: [(10, 10, 50, 50, 1)], # frame_number: [(x_min, y_min, x_max, y_max, class), ...]\n",
" 1: [(20, 20, 60, 60, 1)],\n",
" # Add more frames with their bounding boxes as needed\n",
"}\n",
"\n",
"while cap.isOpened():\n",
" ret, frame = cap.read()\n",
" if not ret:\n",
" break\n",
"\n",
" # Check if we should save this frame\n",
" if frame_count % save_frequency == 0:\n",
" frame_filename = os.path.join(output_images_dir, f'frame_{frame_count}.jpg')\n",
"\n",
" # Get bounding boxes for the current frame (or set empty if none)\n",
" boxes = frame_boxes.get(frame_count, [])\n",
"\n",
" # Draw bounding boxes on the frame\n",
" for (x_min, y_min, x_max, y_max, cls) in boxes:\n",
" cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2) # Draw rectangle\n",
" cv2.putText(frame, str(cls), (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) # Class label\n",
"\n",
" # Save the labeled frame as an image\n",
" cv2.imwrite(frame_filename, frame)\n",
"\n",
" # Save bounding boxes for this frame in a text file\n",
" label_filename = os.path.join(output_labels_dir, f'frame_{frame_count}.txt')\n",
" with open(label_filename, 'w') as label_file:\n",
" for (x_min, y_min, x_max, y_max, cls) in boxes:\n",
" label_file.write(f\"{cls} {x_min} {y_min} {x_max} {y_max}\\n\")\n",
"\n",
" frame_count += 1\n",
"\n",
"cap.release()\n"
],
"metadata": {
"id": "7pvM2ZpFkdkU"
},
"execution_count": 8,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import cv2\n",
"import os\n",
"\n",
"# Paths\n",
"video_path = '/content/drive/MyDrive/Colab Notebooks/flood shared with Amir/flood videos/Floods submerge villages as Typhoon Carina brings heavy rains in Philippines - ABS-CBN News_part_0.avi'\n",
"output_images_dir = '/content/drive/MyDrive/Water Segmentation/frames/train/images'\n",
"output_labels_dir = '/content/drive/MyDrive/Water Segmentation/frames/train/labels'\n",
"\n",
"# Create output directories if they don't exist\n",
"os.makedirs(output_images_dir, exist_ok=True) # Create images output directory if it doesn't exist\n",
"os.makedirs(output_labels_dir, exist_ok=True) # Create labels output directory if it doesn't exist\n",
"\n",
"# Open video\n",
"cap = cv2.VideoCapture(video_path)\n",
"frame_rate = cap.get(cv2.CAP_PROP_FPS) # Frames per second\n",
"frame_count = 0\n",
"save_frequency = int(frame_rate) # Modify to control frame save interval\n",
"\n",
"# Example bounding boxes: (x_min, y_min, x_max, y_max, class_id)\n",
"# You need to replace this with actual logic to get bounding boxes if necessary\n",
"frame_boxes = {\n",
" 0: [(10, 10, 50, 50, 1)], # frame_number: [(x_min, y_min, x_max, y_max, class), ...]\n",
" 1: [(20, 20, 60, 60, 1)],\n",
" # Add more frames with their bounding boxes as needed\n",
"}\n",
"\n",
"while cap.isOpened():\n",
" ret, frame = cap.read()\n",
" if not ret:\n",
" break\n",
"\n",
" # Check if we should save this frame\n",
" if frame_count % save_frequency == 0:\n",
" frame_filename = os.path.join(output_images_dir, f'frame_{frame_count}.jpg')\n",
"\n",
" # Get bounding boxes for the current frame (or set empty if none)\n",
" boxes = frame_boxes.get(frame_count, [])\n",
"\n",
" # Draw bounding boxes on the frame\n",
" for (x_min, y_min, x_max, y_max, cls) in boxes:\n",
" cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2) # Draw rectangle\n",
" cv2.putText(frame, str(cls), (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) # Class label\n",
"\n",
" # Save the labeled frame as an image\n",
" cv2.imwrite(frame_filename, frame)\n",
"\n",
" # Save bounding boxes for this frame in a text file\n",
" label_filename = os.path.join(output_labels_dir, f'frame_{frame_count}.txt')\n",
" with open(label_filename, 'w') as label_file:\n",
" for (x_min, y_min, x_max, y_max, cls) in boxes:\n",
" label_file.write(f\"{cls} {x_min} {y_min} {x_max} {y_max}\\n\")\n",
"\n",
" frame_count += 1\n",
"\n",
"cap.release()\n"
],
"metadata": {
"id": "AyUdW-Oik_vW"
},
"execution_count": 24,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import cv2\n",
"import os\n",
"\n",
"# Paths\n",
"video_path = '/content/drive/MyDrive/Colab Notebooks/flood shared with Amir/normal flow/Free stock footage_ Wide shallow river in the green forest_part_0.avi'\n",
"output_images_dir = '/content/drive/MyDrive/Water Segmentation/frames/train/images'\n",
"output_labels_dir = '/content/drive/MyDrive/Water Segmentation/frames/train/labels'\n",
"\n",
"# Create output directories if they don't exist\n",
"os.makedirs(output_images_dir, exist_ok=True) # Create images output directory if it doesn't exist\n",
"os.makedirs(output_labels_dir, exist_ok=True) # Create labels output directory if it doesn't exist\n",
"\n",
"# Open video\n",
"cap = cv2.VideoCapture(video_path)\n",
"frame_rate = cap.get(cv2.CAP_PROP_FPS) # Frames per second\n",
"frame_count = 0\n",
"save_frequency = int(frame_rate) # Modify to control frame save interval\n",
"\n",
"# Example bounding boxes: (x_min, y_min, x_max, y_max, class_id)\n",
"# You need to replace this with actual logic to get bounding boxes if necessary\n",
"frame_boxes = {\n",
" 0: [(10, 10, 50, 50, 1)], # frame_number: [(x_min, y_min, x_max, y_max, class), ...]\n",
" 1: [(20, 20, 60, 60, 1)],\n",
" # Add more frames with their bounding boxes as needed\n",
"}\n",
"\n",
"while cap.isOpened():\n",
" ret, frame = cap.read()\n",
" if not ret:\n",
" break\n",
"\n",
" # Check if we should save this frame\n",
" if frame_count % save_frequency == 0:\n",
" frame_filename = os.path.join(output_images_dir, f'frame_{frame_count}.jpg')\n",
"\n",
" # Get bounding boxes for the current frame (or set empty if none)\n",
" boxes = frame_boxes.get(frame_count, [])\n",
"\n",
" # Draw bounding boxes on the frame\n",
" for (x_min, y_min, x_max, y_max, cls) in boxes:\n",
" cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2) # Draw rectangle\n",
" cv2.putText(frame, str(cls), (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) # Class label\n",
"\n",
" # Save the labeled frame as an image\n",
" cv2.imwrite(frame_filename, frame)\n",
"\n",
" # Save bounding boxes for this frame in a text file\n",
" label_filename = os.path.join(output_labels_dir, f'frame_{frame_count}.txt')\n",
" with open(label_filename, 'w') as label_file:\n",
" for (x_min, y_min, x_max, y_max, cls) in boxes:\n",
" label_file.write(f\"{cls} {x_min} {y_min} {x_max} {y_max}\\n\")\n",
"\n",
" frame_count += 1\n",
"\n",
"cap.release()\n"
],
"metadata": {
"id": "mHs1iKoKofb7"
},
"execution_count": 31,
"outputs": []
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "kVSPzr_gojTy"
},
"execution_count": null,
"outputs": []
}
]
}
Loading