{ "cells": [ { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import cv2\n", "import os, json\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Capture a background image" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "settings_filename = 'settings.json'\n", "\n", "with open(settings_filename) as json_file:\n", " cfg = json.load(json_file)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Define BGR colors\n", "BGR_COLOR = {\n", " 'red': (0,0,255),\n", " 'green': (127,255,0),\n", " 'blue': (255,127,0),\n", " 'yellow': (0,127,255),\n", " 'black': (0,0,0),\n", " 'white': (255,255,255)\n", "}\n", "\n", "cap = cv2.VideoCapture(0) # if slow https://github.com/opencv/opencv/issues/17687\n", "cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))\n", "cap.set(cv2.CAP_PROP_FRAME_WIDTH, cfg['resolution_x'])\n", "cap.set(cv2.CAP_PROP_FRAME_HEIGHT, cfg['resolution_y'])\n", "cap.set(cv2.CAP_PROP_FPS, 20)\n", "cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')) \n", "\n", "while(True):\n", " ret, frame = cap.read()\n", "\n", " # Draw the arena area\n", " cv2.circle(frame, (cfg['arena_x'], cfg['arena_y']), cfg['arena_radius'], BGR_COLOR['red'], 2)\n", "\n", " # Mask the space outside the arena\n", " mask = np.zeros(shape=frame.shape, dtype=\"uint8\")\n", " cv2.circle(mask, (cfg['arena_x'], cfg['arena_y']), cfg['arena_radius'], BGR_COLOR['white'], -1)\n", "\n", " frame = cv2.bitwise_and(src1=frame, src2=mask)\n", " cv2.imshow('Press \"c\" to capture, \"q\" to quit', frame)\n", "\n", " k = cv2.waitKey(33)\n", " if k == ord('c'):\n", " cv2.imwrite(os.path.join('assets', 'background.png'), frame)\n", " break\n", " if k == ord('q'):\n", " break \n", "\n", "# When the background image is captured, release the capture\n", "cap.release()\n", "cv2.destroyAllWindows()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }