{"id":13418784,"url":"https://github.com/andrewssobral/simple_vehicle_counting","last_synced_at":"2025-04-05T04:14:39.940Z","repository":{"id":31258893,"uuid":"34820566","full_name":"andrewssobral/simple_vehicle_counting","owner":"andrewssobral","description":"Vehicle Detection, Tracking and Counting","archived":false,"fork":false,"pushed_at":"2020-03-07T22:05:56.000Z","size":21852,"stargazers_count":502,"open_issues_count":21,"forks_count":246,"subscribers_count":59,"default_branch":"master","last_synced_at":"2025-03-29T03:11:22.444Z","etag":null,"topics":["background-subtraction","opencv","vehicle-counting","vehicle-detection","vehicle-tracking"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andrewssobral.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-29T22:01:59.000Z","updated_at":"2025-03-25T08:13:54.000Z","dependencies_parsed_at":"2022-09-18T06:04:24.244Z","dependency_job_id":null,"html_url":"https://github.com/andrewssobral/simple_vehicle_counting","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrewssobral%2Fsimple_vehicle_counting","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrewssobral%2Fsimple_vehicle_counting/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrewssobral%2Fsimple_vehicle_counting/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrewssobral%2Fsimple_vehicle_counting/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andrewssobral","download_url":"https://codeload.github.com/andrewssobral/simple_vehicle_counting/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247284953,"owners_count":20913704,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["background-subtraction","opencv","vehicle-counting","vehicle-detection","vehicle-tracking"],"created_at":"2024-07-30T22:01:07.040Z","updated_at":"2025-04-05T04:14:39.915Z","avatar_url":"https://github.com/andrewssobral.png","language":"C++","funding_links":[],"categories":["TODO scan for Android support in followings","C++"],"sub_categories":[],"readme":"Vehicle Detection, Tracking and Counting\n========================================\n\nLast page update: **12/04/2017** (Added Python API \u0026 OpenCV 3.x support)\n\nLast version: **1.0.0** (see Release Notes for more info)\n\nHi everyone,\n\nThere are several ways to perform vehicle detection, tracking and counting.\nHere is a step-by-step of a simplest way to do this:\n\n1. First, you will need to detect the moving objects. An easy way to do vehicle detection is by using a Background Subtraction (BS) algorithm. You can try to use a background subtraction library like [BGSLibrary](https://github.com/andrewssobral/bgslibrary#bgslibrary).\n2. For vehicle tracking, you will need to use a tracking algorithm. A simplest way to do this is by using a blob tracker algorithm (see [cvBlob](https://code.google.com/p/cvblob/) or [OpenCVBlobsLib](http://opencvblobslib.github.io/opencvblobslib/)). So, send the foreground mask to **cvBlob** or **OpenCVBlobsLib**. For example, the **cvBlob** library provide some methods to get the **centroid**, the **track** and the **ID** of the moving objects. You can also set to draw a **bounding box**, the **centroid** and the **angle** of the tracked object.\n3. And then, check if the **centroid** of the moving object has crossed a **region of interest** (i.e. virtual line) in your video.\n4. Voilà! enjoy it :)\n\n\u003cp align=\"center\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/andrewssobral/simple_vehicle_counting/master/doc/images/vehicle_counting_screen.png\" /\u003e\u003c/p\u003e\n\n\nCitation\n--------\nIf you use this code for your publications, please cite it as:\n```\n@ONLINE{vdtc,\n    author = \"Andrews Sobral\",\n    title  = \"Vehicle Detection, Tracking and Counting\",\n    year   = \"2014\",\n    url    = \"https://github.com/andrewssobral/simple_vehicle_counting\"\n}\n```\n\n\nFor Windows users\n-----------------\n* There is no Visual Studio 2013 template project anymore. Please, use CMAKE instead.\n\n#### Compiling with OpenCV 3.x and Visual Studio 2015 from CMAKE\n\n**Dependencies:**\n* OpenCV 3.x (tested with OpenCV 3.2.0)\n* GIT (tested with git version 2.7.2.windows.1).\n* CMAKE for Windows (tested with cmake version 3.1.1).\n* Microsoft Visual Studio (tested with VS2015).\n\n*Note: the procedure is similar for OpenCV 2.4.x and Visual Studio 2013.*\n\nPlease follow the instructions below:\n\n1) Go to Windows console.\n\n2) Clone git repository:\n```\ngit clone --recursive https://github.com/andrewssobral/simple_vehicle_counting.git\n```\n\n3) Go to **simple_vehicle_counting/build** folder.\n\n4) Set your OpenCV PATH:\n```\nset OpenCV_DIR=C:\\OpenCV3.2.0\\build\n```\n\n5) Launch CMAKE:\n```\ncmake -DOpenCV_DIR=%OpenCV_DIR% -G \"Visual Studio 14 Win64\" ..\n```\n\n6) Include OpenCV binaries in the system path:\n```\nset PATH=%PATH%;%OpenCV_DIR%\\x64\\vc14\\bin\n```\n\n7) Open the **bgs.sln** file in your Visual Studio and switch to **'RELEASE'** mode\n\n8) Click on **'ALL_BUILD'** project and build!\n\n9) If everything goes well, copy **simple_vehicle_counting.exe** to **simple_vehicle_counting/** and run!\n\n\nFor Linux users\n-----------------\n* For Linux and Mac users, a CMakefile is provided to compile the source code.\n\n* * Check out the latest project source code and compile it:\n```\n~/git clone --recursive https://github.com/andrewssobral/simple_vehicle_counting.git\n~/cd simple_vehicle_counting\n~/simple_vehicle_counting/cd build\n~/simple_vehicle_counting/build/ cmake ..\n~/simple_vehicle_counting/build/ make\n```\n* * Run demo:\n```\n~/simple_vehicle_counting/run_simple_vehicle_counting.sh\n```\n\n\nDocker image\n----------------------------------------\n* Docker image is available at:\n* * **Ubuntu 16.04 + VNC + OpenCV 2.4.13 + Python 2.7 + Vehicle Detection, Tracking and Counting**\nhttps://hub.docker.com/r/andrewssobral/vehicle_detection_tracking_counting/\n\nExample code\n------------\n```C++\n#include \u003ciostream\u003e\n#include \u003copencv2/opencv.hpp\u003e\n\n#include \"package_bgs/PBAS/PixelBasedAdaptiveSegmenter.h\"\n#include \"package_tracking/BlobTracking.h\"\n#include \"package_analysis/VehicleCouting.h\"\n\nint main(int argc, char **argv)\n{\n  /* Open video file */\n  CvCapture *capture = 0;\n  capture = cvCaptureFromAVI(\"dataset/video.avi\");\n  if(!capture){\n    std::cerr \u003c\u003c \"Cannot open video!\" \u003c\u003c std::endl;\n    return 1;\n  }\n\n  /* Background Subtraction Algorithm */\n  IBGS *bgs;\n  bgs = new PixelBasedAdaptiveSegmenter;\n\n  /* Blob Tracking Algorithm */\n  cv::Mat img_blob;\n  BlobTracking* blobTracking;\n  blobTracking = new BlobTracking;\n\n  /* Vehicle Counting Algorithm */\n  VehicleCouting* vehicleCouting;\n  vehicleCouting = new VehicleCouting;\n\n  std::cout \u003c\u003c \"Press 'q' to quit...\" \u003c\u003c std::endl;\n  int key = 0;\n  IplImage *frame;\n  while(key != 'q')\n  {\n    frame = cvQueryFrame(capture);\n    if(!frame) break;\n\n    cv::Mat img_input = cv::cvarrToMat(frame);\n    cv::imshow(\"Input\", img_input);\n\n    // bgs-\u003eprocess(...) internally process and show the foreground mask image\n    cv::Mat img_mask;\n    bgs-\u003eprocess(img_input, img_mask);\n\n    if(!img_mask.empty())\n    {\n      // Perform blob tracking\n      blobTracking-\u003eprocess(img_input, img_mask, img_blob);\n\n      // Perform vehicle counting\n      vehicleCouting-\u003esetInput(img_blob);\n      vehicleCouting-\u003esetTracks(blobTracking-\u003egetTracks());\n      vehicleCouting-\u003eprocess();\n    }\n\n    key = cvWaitKey(1);\n  }\n\n  delete vehicleCouting;\n  delete blobTracking;\n  delete bgs;\n\n  cvDestroyAllWindows();\n  cvReleaseCapture(\u0026capture);\n\n  return 0;\n}\n```\n\n\nPython API\n----------------------------------------\nA [python demo](python/demo.py) shows how to call the Python API.\nIt is similar as the [C++ demo](Demo.cpp).\n\nTo use the Python API, you should copy [\"python\" directory](python) to overwrite the generated one.\n\n```\n~/simple_vehicle_counting/cd build\n~/simple_vehicle_counting/build/cmake ..\n~/simple_vehicle_counting/build/make -j 8\n~/simple_vehicle_counting/build/cp -r ../python/* python/\n~/simple_vehicle_counting/build/../run_python_demo.sh\n```\n\nIf you have previously built the project at the project root,\nmake sure there are no previously generated libraries in the [\"python\" directory](python) by ```make clean```.\n\n\nRelease Notes:\n--------------\n* 12/04/2017: Added OpenCV 3.x support. Removed vs2013 template project (use CMAKE instead).\n\n* 07/04/2017: Added Python API, thanks to [@kyu-sz](https://github.com/kyu-sz).\n\n* Version 1.0.0: First version.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrewssobral%2Fsimple_vehicle_counting","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandrewssobral%2Fsimple_vehicle_counting","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrewssobral%2Fsimple_vehicle_counting/lists"}