Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add notebook examples #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
140 changes: 138 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,138 @@
simple_camera
__pycache__
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ To test the camera:
```
# Simple Test
# Ctrl^C to exit
# Jetson Nano A01
$ gst-launch-1.0 nvarguscamerasrc ! nvoverlaysink

# sensor_id selects the camera: 0 or 1 on Jetson Nano B01
$ gst-launch-1.0 nvarguscamerasrc sensor_id=0 ! nvoverlaysink

Expand Down
180 changes: 180 additions & 0 deletions face_detect.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import IPython.display\n",
"import PIL.Image\n",
"import time\n",
"from io import BytesIO\n",
"import ipywidgets as widgets"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# gstreamer_pipeline returns a GStreamer pipeline for capturing from the CSI camera\n",
"# Defaults to 1280x720 @ 30fps\n",
"# Flip the image by setting the flip_method (most common values: 0 and 2)\n",
"# display_width and display_height determine the size of the window on the screen\n",
"\n",
"\n",
"def gstreamer_pipeline(\n",
" capture_width=3280,\n",
" capture_height=2464,\n",
" display_width=820,\n",
" display_height=616,\n",
" framerate=21,\n",
" flip_method=0,\n",
"):\n",
" return (\n",
" \"nvarguscamerasrc ! \"\n",
" \"video/x-raw(memory:NVMM), \"\n",
" \"width=(int)%d, height=(int)%d, \"\n",
" \"format=(string)NV12, framerate=(fraction)%d/1 ! \"\n",
" \"nvvidconv flip-method=%d ! \"\n",
" \"video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! \"\n",
" \"videoconvert ! \"\n",
" \"video/x-raw, format=(string)BGR ! appsink\"\n",
" % (\n",
" capture_width,\n",
" capture_height,\n",
" framerate,\n",
" flip_method,\n",
" display_width,\n",
" display_height,\n",
" )\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Use 'jpeg' instead of 'png' (~5 times faster)\n",
"def show_array(a, display_id=None, fmt='jpeg'):\n",
" f = BytesIO()\n",
" PIL.Image.fromarray(a).save(f, fmt)\n",
" obj = IPython.display.Image(data=f.getvalue())\n",
" if display_id is not None:\n",
" IPython.display.update_display(obj, display_id=display_id)\n",
" return display_id\n",
" else:\n",
" return IPython.display.display(obj, display_id=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def face_detect():\n",
" # To flip the image, modify the flip_method parameter (0 and 2 are the most common)\n",
" print(gstreamer_pipeline(flip_method=2))\n",
" face_cascade = cv2.CascadeClassifier(\n",
" \"/usr/share/opencv4/haarcascades/haarcascade_frontalface_default.xml\"\n",
" )\n",
" eye_cascade = cv2.CascadeClassifier(\n",
" \"/usr/share/opencv4/haarcascades/haarcascade_eye.xml\"\n",
" )\n",
" # Video capturing from OpenCV\n",
" video_capture = cv2.VideoCapture(gstreamer_pipeline(flip_method=2), cv2.CAP_GSTREAMER)\n",
" display_id = None\n",
" fps_output = widgets.Output()\n",
" IPython.display.display(fps_output)\n",
" if video_capture.isOpened():\n",
" try:\n",
" while True:\n",
" t1 = time.time()\n",
" \n",
" return_value, frame = video_capture.read()\n",
" \n",
" if not return_value:\n",
" print(f\"return_value: {return_value}\")\n",
" break\n",
" \n",
" # Convert the image from OpenCV BGR format to matplotlib RGB format\n",
" # to display the image\n",
" gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n",
" faces = face_cascade.detectMultiScale(gray, 1.3, 5)\n",
"\n",
" for (x, y, w, h) in faces:\n",
" cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)\n",
" roi_gray = gray[y : y + h, x : x + w]\n",
" roi_color = frame[y : y + h, x : x + w]\n",
" eyes = eye_cascade.detectMultiScale(roi_gray)\n",
" for (ex, ey, ew, eh) in eyes:\n",
" cv2.rectangle(\n",
" roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2\n",
" )\n",
"\n",
" if display_id is not None:\n",
" show_array(frame, display_id)\n",
" else:\n",
" display_handle = show_array(frame)\n",
" display_id = display_handle.display_id\n",
" \n",
" t2 = time.time()\n",
"\n",
" #ref: https://github.com/jupyter-widgets/ipywidgets/issues/1744#issuecomment-335179855\n",
" with fps_output:\n",
" print(f\"display_id: {display_id}\")\n",
" print(f\"{(1/(t2-t1)):.4f} FPS\")\n",
" # Display the frame info until new frame is available\n",
" IPython.display.clear_output(wait=True)\n",
" \n",
" except KeyboardInterrupt as e:\n",
" print(f\"KeyboardInterrupt\")\n",
" except Exception as e:\n",
" print(f\"Exception: {e}\")\n",
" finally:\n",
" # Release the Video Device\n",
" video_capture.release()\n",
" # Message to be displayed after releasing the device\n",
" print(\"Released Video Resource\")\n",
" else:\n",
" print(\"Unable to open camera\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"face_detect()"
]
}
],
"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.6.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading