Skip to content

Instantly share code, notes, and snippets.

@mikbuch
Last active January 4, 2022 10:10
Show Gist options
  • Save mikbuch/73401b8fa7289113417b35f1144cb179 to your computer and use it in GitHub Desktop.
Save mikbuch/73401b8fa7289113417b35f1144cb179 to your computer and use it in GitHub Desktop.
Materials for my answer the StackOverflow question: https://stackoverflow.com/q/70445787/8877692. A direct link to my answer: https://stackoverflow.com/a/70446399/8877692.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# OpenCV video capture example\n",
"\n",
"Note: you should run this example on your desktop.\n",
"\n",
"Created as part of the answer for [the Stack Overflow question by coreselene](https://stackoverflow.com/q/70445787/8877692). The path definition as in the original question.\n",
"\n",
"Source of the MP4 files: https://file-examples.com/index.php/sample-video-files/sample-mp4-files/\n",
"\n",
"How to capture (load) files with OpenCV: https://learnopencv.com/read-write-and-display-a-video-using-opencv-cpp-python/\n",
"\n",
"See also: https://learnopencv.com/reading-and-writing-videos-using-opencv/"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import os\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#SETUP PATHS\n",
"POS_PATH = os.path.join('data', 'positive')\n",
"NEG_PATH = os.path.join('data', 'negative')\n",
"ANC_PATH = os.path.join('data','anchor')\n",
"\n",
"\n",
"#MAKE DIRECTORIES\n",
"os.makedirs(POS_PATH)\n",
"os.makedirs(NEG_PATH)\n",
"os.makedirs(ANC_PATH)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Fill your directories with the files. In this example we will use a file `file_example_MP4_480_1_5MG.mp4` (path: `data/positive/file_example_MP4_480_1_5MG.mp4`). Source of this file is: https://file-examples.com/index.php/sample-video-files/sample-mp4-files/"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, if you will use the following command:\n",
"\n",
"```\n",
"os.listdir('positive')\n",
"\n",
"```\n",
"\n",
"you will get `FileNotFoundError`:\n",
"\n",
"```\n",
"---------------------------------------------------------------------------\n",
"FileNotFoundError Traceback (most recent call last)\n",
"<ipython-input-6-f6aeca1b018e> in <module>\n",
"----> 1 os.listdir('positive')\n",
"\n",
"FileNotFoundError: [Errno 2] No such file or directory: 'positive'\n",
"```\n",
"\n",
"The reason for that is that the actual location of this directory is:\n",
"\n",
"```\n",
"'data/positive'\n",
"\n",
"```\n",
"\n",
"See the proper command below:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['file_example_MP4_640_3MG.mp4', 'file_example_MP4_480_1_5MG.mp4']"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"os.listdir('data/positive')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Frames per second : 30.0 FPS\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"2021-12-22 09:44:13.013 Python[10065:138219] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to /var/folders/8j/8tw8x4sx49384vtn35xcpwn00000gr/T/org.python.python.savedState\n"
]
}
],
"source": [
"# Create a VideoCapture object and read from input file\n",
"# If the input is the camera, pass 0 instead of the video file name\n",
"cap = cv2.VideoCapture('data/positive/file_example_MP4_480_1_5MG.mp4')\n",
"\n",
"fps = cap.get(5)\n",
"print('Frames per second : ', fps,'FPS')\n",
"\n",
"\n",
"# Check if camera opened successfully\n",
"if (cap.isOpened()== False): \n",
" print(\"Error opening video stream or file\")\n",
"\n",
"# Read until video is completed\n",
"while(cap.isOpened()):\n",
" # Capture frame-by-frame\n",
" ret, frame = cap.read()\n",
" if ret == True:\n",
"\n",
" # Display the resulting frame\n",
" cv2.imshow('Frame',frame)\n",
"\n",
" # Press Q on keyboard to exit\n",
" if cv2.waitKey(25) & 0xFF == ord('q'):\n",
" break\n",
"\n",
" # Break the loop\n",
" else: \n",
" break\n",
"\n",
"# When everything done, release the video capture object\n",
"cap.release()\n",
"\n",
"# Closes all the frames\n",
"cv2.destroyAllWindows()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
jupyter = "*"
notebook = "*"
opencv-python = "*"
[dev-packages]
[requires]
python_version = "3.9"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment