{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# The simplest opencv widget integration\n", "\n", "Create an updating html table that is displayed while the video is captured." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ " import cv2\n", " from pandas import Series, DataFrame\n", " from ipywidgets import HTML" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ " table = HTML()\n", "\n", " def show_webcam(mirror=False):\n", " global df\n", " cam = cv2.VideoCapture(0)\n", " while True:\n", " ret_val, img = cam.read()\n", " if mirror: \n", " img = cv2.flip(img, 1)\n", " df = DataFrame(img[None, None, 3][0, 0, :, :], columns=list('rgb'))\n", " table.value = df.describe().astype(str).to_html()\n", " cv2.imshow('my webcam', img)\n", " if cv2.waitKey(1) == 27: \n", " break # esc to quit\n", " cv2.destroyAllWindows()\n", "\n", "\n", " def main():\n", " try:\n", " __import__(\"IPython\").display.display(table)\n", " show_webcam(mirror=True)\n", " except KeyboardInterrupt: ..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Run the program." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " if __name__ == '__main__':\n", " main()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.5" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }