|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import cv2\n", |
| 10 | + "import time\n", |
| 11 | + "import numpy as np\n", |
| 12 | + "\n", |
| 13 | + "## Preparation for writing the ouput video\n", |
| 14 | + "fourcc = cv2.VideoWriter_fourcc(*'XVID')\n", |
| 15 | + "out = cv2.VideoWriter('output.avi',fourcc,20.0, (640,480))\n", |
| 16 | + "\n", |
| 17 | + "##reading from the webcam \n", |
| 18 | + "cap = cv2.VideoCapture(0)\n", |
| 19 | + "\n", |
| 20 | + "## Allow the system to sleep for 3 seconds before the webcam starts\n", |
| 21 | + "time.sleep(3)\n", |
| 22 | + "count = 0\n", |
| 23 | + "background = 0\n", |
| 24 | + "\n", |
| 25 | + "## Capture the background in range of 60\n", |
| 26 | + "for i in range(60):\n", |
| 27 | + " ret,background = cap.read()\n", |
| 28 | + "background = np.flip(background,axis=1)\n", |
| 29 | + "\n", |
| 30 | + "\n", |
| 31 | + "## Read every frame from the webcam, until the camera is open\n", |
| 32 | + "while(cap.isOpened()):\n", |
| 33 | + " ret, img = cap.read()\n", |
| 34 | + " if not ret:\n", |
| 35 | + " break\n", |
| 36 | + " count+=1\n", |
| 37 | + " img = np.flip(img,axis=1)\n", |
| 38 | + " \n", |
| 39 | + " ## Convert the color space from BGR to HSV\n", |
| 40 | + " hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)\n", |
| 41 | + "\n", |
| 42 | + " ## Generat masks to detect red color\n", |
| 43 | + " lower_red = np.array([0,120,50])\n", |
| 44 | + " upper_red = np.array([10,255,255])\n", |
| 45 | + " mask1 = cv2.inRange(hsv,lower_red,upper_red)\n", |
| 46 | + "\n", |
| 47 | + " lower_red = np.array([170,120,70])\n", |
| 48 | + " upper_red = np.array([180,255,255])\n", |
| 49 | + " mask2 = cv2.inRange(hsv,lower_red,upper_red)\n", |
| 50 | + "\n", |
| 51 | + " mask1 = mask1+mask2" |
| 52 | + ] |
| 53 | + } |
| 54 | + ], |
| 55 | + "metadata": { |
| 56 | + "kernelspec": { |
| 57 | + "display_name": "Python 3", |
| 58 | + "language": "python", |
| 59 | + "name": "python3" |
| 60 | + }, |
| 61 | + "language_info": { |
| 62 | + "codemirror_mode": { |
| 63 | + "name": "ipython", |
| 64 | + "version": 3 |
| 65 | + }, |
| 66 | + "file_extension": ".py", |
| 67 | + "mimetype": "text/x-python", |
| 68 | + "name": "python", |
| 69 | + "nbconvert_exporter": "python", |
| 70 | + "pygments_lexer": "ipython3", |
| 71 | + "version": "3.8.3" |
| 72 | + } |
| 73 | + }, |
| 74 | + "nbformat": 4, |
| 75 | + "nbformat_minor": 4 |
| 76 | +} |
0 commit comments