Skip to content

Commit 1dcd130

Browse files
authored
Add files via upload
1 parent 970a901 commit 1dcd130

12 files changed

+5703
-0
lines changed

00-Color-Mappings.ipynb

+187
Large diffs are not rendered by default.

00-Connecting-to-Camera.ipynb

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"<a href=\"https://www.pieriandata.com\"><img src=\"../DATA/Logo.jpg\"></a>\n",
8+
"\n",
9+
"<em text-align:center>Copyright Pierian Data Inc.</em>"
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"# OpenCV Connecting to a USB Camera or a Laptop Camera\n",
17+
"\n",
18+
"OpenCV can automatically connect to your laptop's built in camera or your USB camera if you've installed that specific USB camera drivers. Please keep in mind, its almost impossible for us to help troubleshoot this sort of physical connection on our end, so if you are not able to connect, please check out the troubleshooting tips here:\n",
19+
"\n",
20+
"* https://github.com/opencv/opencv/issues/8471\n",
21+
"\n",
22+
"### Notebook Users may need to restart the kernel after closing the camera window!"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 5,
28+
"metadata": {},
29+
"outputs": [],
30+
"source": [
31+
"## PUT THIS ALL IN ONE CELL!\n",
32+
"\n",
33+
"import cv2\n",
34+
"\n",
35+
"\n",
36+
"# Connects to your computer's default camera\n",
37+
"cap = cv2.VideoCapture(0)\n",
38+
"\n",
39+
"\n",
40+
"# Automatically grab width and height from video feed\n",
41+
"# (returns float which we need to convert to integer for later on!)\n",
42+
"width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))\n",
43+
"height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))\n",
44+
"\n",
45+
"while True:\n",
46+
" \n",
47+
" # Capture frame-by-frame\n",
48+
" ret, frame = cap.read()\n",
49+
"\n",
50+
" # Our operations on the frame come here\n",
51+
" gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n",
52+
"\n",
53+
" # Display the resulting frame\n",
54+
" cv2.imshow('frame',gray)\n",
55+
" \n",
56+
" # This command let's us quit with the \"q\" button on a keyboard.\n",
57+
" # Simply pressing X on the window won't work!\n",
58+
" if cv2.waitKey(1) & 0xFF == ord('q'):\n",
59+
" break\n",
60+
"\n",
61+
"# When everything done, release the capture and destroy the windows\n",
62+
"cap.release()\n",
63+
"cv2.destroyAllWindows()"
64+
]
65+
},
66+
{
67+
"cell_type": "markdown",
68+
"metadata": {},
69+
"source": [
70+
"## Writing a Video Stream to File"
71+
]
72+
},
73+
{
74+
"cell_type": "markdown",
75+
"metadata": {},
76+
"source": [
77+
"### Notebook Users: Make sure its all in the same cell!"
78+
]
79+
},
80+
{
81+
"cell_type": "markdown",
82+
"metadata": {},
83+
"source": [
84+
"FourCC is a 4-byte code used to specify the video codec. The list of available codes can be found in fourcc.org. It is platform dependent. \n",
85+
"\n",
86+
"MORE INFO ON CODECS: https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html#saving-a-video"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 3,
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"import cv2\n",
96+
"\n",
97+
"cap = cv2.VideoCapture(0)\n",
98+
"\n",
99+
"# Automatically grab width and height from video feed\n",
100+
"# (returns float which we need to convert to integer for later on!)\n",
101+
"width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))\n",
102+
"height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))\n",
103+
"\n",
104+
"\n",
105+
"# MACOS AND LINUX: *'XVID' (MacOS users may want to try VIDX as well just in case)\n",
106+
"# WINDOWS *'VIDX'\n",
107+
"writer = cv2.VideoWriter('../DATA/student_capture.mp4', cv2.VideoWriter_fourcc(*'XVID'),25, (width, height))\n",
108+
"\n",
109+
"\n",
110+
"## This loop keeps recording until you hit Q or escape the window\n",
111+
"## You may want to instead use some sort of timer, like from time import sleep and then just record for 5 seconds.\n",
112+
"\n",
113+
"while True:\n",
114+
" # Capture frame-by-frame\n",
115+
" ret, frame = cap.read()\n",
116+
"\n",
117+
" \n",
118+
" # Write the video\n",
119+
" writer.write(frame)\n",
120+
"\n",
121+
" # Display the resulting frame\n",
122+
" cv2.imshow('frame',frame)\n",
123+
" \n",
124+
" # This command let's us quit with the \"q\" button on a keyboard.\n",
125+
" # Simply pressing X on the window won't work!\n",
126+
" if cv2.waitKey(1) & 0xFF == ord('q'):\n",
127+
" break\n",
128+
"\n",
129+
" \n",
130+
"cap.release()\n",
131+
"writer.release()\n",
132+
"cv2.destroyAllWindows()"
133+
]
134+
},
135+
{
136+
"cell_type": "markdown",
137+
"metadata": {},
138+
"source": [
139+
"Make sure to check StackOverflow if you are encountering issues, as this is particularly tricky to help debug through online QA support."
140+
]
141+
}
142+
],
143+
"metadata": {
144+
"kernelspec": {
145+
"display_name": "Python 3",
146+
"language": "python",
147+
"name": "python3"
148+
},
149+
"language_info": {
150+
"codemirror_mode": {
151+
"name": "ipython",
152+
"version": 3
153+
},
154+
"file_extension": ".py",
155+
"mimetype": "text/x-python",
156+
"name": "python",
157+
"nbconvert_exporter": "python",
158+
"pygments_lexer": "ipython3",
159+
"version": "3.6.6"
160+
}
161+
},
162+
"nbformat": 4,
163+
"nbformat_minor": 2
164+
}

01-Blending-and-Pasting-Images.ipynb

+1,117
Large diffs are not rendered by default.

01-Using-Video-Files.ipynb

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"<a href=\"https://www.pieriandata.com\"><img src=\"../DATA/Logo.jpg\"></a>\n",
8+
"\n",
9+
"<em text-align:center>Copyright Pierian Data Inc.</em>"
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"# OpenCV with Video Files\n",
17+
"\n",
18+
"Let's now open the recorded video from the last lecture, the file is called \"video_capture.mp4\", although you can use this code to open any major video format."
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 2,
24+
"metadata": {},
25+
"outputs": [],
26+
"source": [
27+
"# Run everything in one cell!"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 1,
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"import cv2\n",
37+
"import time\n",
38+
"# Same command function as streaming, its just now we pass in the file path, nice!\n",
39+
"cap = cv2.VideoCapture('../DATA/video_capture.mp4')\n",
40+
"\n",
41+
"# FRAMES PER SECOND FOR VIDEO\n",
42+
"fps = 25\n",
43+
"\n",
44+
"# Always a good idea to check if the video was acutally there\n",
45+
"# If you get an error at thsi step, triple check your file path!!\n",
46+
"if cap.isOpened()== False: \n",
47+
" print(\"Error opening the video file. Please double check your file path for typos. Or move the movie file to the same location as this script/notebook\")\n",
48+
" \n",
49+
"\n",
50+
"# While the video is opened\n",
51+
"while cap.isOpened():\n",
52+
" \n",
53+
" \n",
54+
" \n",
55+
" # Read the video file.\n",
56+
" ret, frame = cap.read()\n",
57+
" \n",
58+
" # If we got frames, show them.\n",
59+
" if ret == True:\n",
60+
" \n",
61+
" \n",
62+
" \n",
63+
"\n",
64+
" # Display the frame at same frame rate of recording\n",
65+
" # Watch lecture video for full explanation\n",
66+
" time.sleep(1/fps)\n",
67+
" cv2.imshow('frame',frame)\n",
68+
" \n",
69+
" # Press q to quit\n",
70+
" if cv2.waitKey(25) & 0xFF == ord('q'):\n",
71+
" \n",
72+
" break\n",
73+
" \n",
74+
" # Or automatically break this whole loop if the video is over.\n",
75+
" else:\n",
76+
" break\n",
77+
" \n",
78+
"cap.release()\n",
79+
"# Closes all the frames\n",
80+
"cv2.destroyAllWindows()"
81+
]
82+
}
83+
],
84+
"metadata": {
85+
"kernelspec": {
86+
"display_name": "Python 3",
87+
"language": "python",
88+
"name": "python3"
89+
},
90+
"language_info": {
91+
"codemirror_mode": {
92+
"name": "ipython",
93+
"version": 3
94+
},
95+
"file_extension": ".py",
96+
"mimetype": "text/x-python",
97+
"name": "python",
98+
"nbconvert_exporter": "python",
99+
"pygments_lexer": "ipython3",
100+
"version": "3.6.6"
101+
}
102+
},
103+
"nbformat": 4,
104+
"nbformat_minor": 2
105+
}

0 commit comments

Comments
 (0)