Last active
May 12, 2018 21:00
-
-
Save ricardodeazambuja/12ab5c012d47677575fc203d07d81c2d to your computer and use it in GitHub Desktop.
Super simple example to test OpenCV inside a Jupyter notebook running through a docker container - https://ricardodeazambuja.com/deep_learning/2018/05/04/tuning_tensorflow_docker/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy\n", | |
"import cv2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"cv2.startWindowThread()\n", | |
"cv2.namedWindow(\"Image window\",cv2.WINDOW_NORMAL) # cv2.WINDOW_NORMAL lets us resize the window" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Let's test it with some old school analog tv static (aka noise)\n", | |
"while True:\n", | |
" cv2.imshow(\"Image window\", numpy.random.rand(600,600,4))\n", | |
" key = cv2.waitKey(1) & 0xFF\n", | |
" # Pressing 'q' stops the loop\n", | |
" if key == ord(\"q\"):\n", | |
" break" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Now, we need to get to the webcam\n", | |
"capture = cv2.VideoCapture(0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Grabs frames from webcam and sends to the window created\n", | |
"while True:\n", | |
" grabbed, frame = capture.read()\n", | |
" cv2.imshow(\"Image window\", frame)\n", | |
" key = cv2.waitKey(1) & 0xFF\n", | |
" # Pressing 'q' stops the loop\n", | |
" if key == ord(\"q\"):\n", | |
" break" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"capture.release()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"cv2.destroyAllWindows()" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.5.2" | |
}, | |
"widgets": { | |
"state": {}, | |
"version": "1.1.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment