From 93205ae9821079070e8a09ac02bfb9ee8c415165 Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Sat, 30 Sep 2023 19:16:03 -0700 Subject: [PATCH] first commit of working example --- .envrc | 1 + .gitignore | 3 + Cargo.lock | 201 ++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 + flake.lock | 96 +++++++++++++++++ flake.nix | 122 ++++++++++++++++++++++ object_tracking/Cargo.toml | 10 ++ object_tracking/src/main.rs | 24 +++++ 8 files changed, 460 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 object_tracking/Cargo.toml create mode 100644 object_tracking/src/main.rs diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9624092 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +.direnv +result diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..bb12c21 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,201 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea5d730647d4fadd988536d06fecce94b7b4f2a7efdae548f1cf4b63205518ab" +dependencies = [ + "memchr", +] + +[[package]] +name = "anyhow" +version = "1.0.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "cc" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" +dependencies = [ + "jobserver", +] + +[[package]] +name = "clang" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84c044c781163c001b913cd018fc95a628c50d0d2dfea8bca77dad71edb16e37" +dependencies = [ + "clang-sys", + "libc", +] + +[[package]] +name = "clang-sys" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c688fc74432808e3eb684cae8830a86be1d66a2bd58e1f248ed0960a590baf6f" +dependencies = [ + "glob", + "libc", +] + +[[package]] +name = "dunce" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b" + +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + +[[package]] +name = "jobserver" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "936cfd212a0155903bcbc060e316fb6cc7cbf2e1907329391ebadc1fe0ce77c2" +dependencies = [ + "libc", +] + +[[package]] +name = "libc" +version = "0.2.148" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b" + +[[package]] +name = "memchr" +version = "2.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" + +[[package]] +name = "num-traits" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" +dependencies = [ + "autocfg", +] + +[[package]] +name = "object_tracking" +version = "0.1.0" +dependencies = [ + "anyhow", + "opencv", +] + +[[package]] +name = "once_cell" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" + +[[package]] +name = "opencv" +version = "0.84.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a097f79e4a6ec571f5765c5020f7dbe77d22b9b8dde01c647098fcd2be3895c" +dependencies = [ + "cc", + "dunce", + "jobserver", + "libc", + "num-traits", + "once_cell", + "opencv-binding-generator", + "pkg-config", + "semver", + "shlex", + "vcpkg", +] + +[[package]] +name = "opencv-binding-generator" +version = "0.72.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e674c5d4d124421a4980fa8533bafd15e07dec30acca5c1b38c055ba234a2cf" +dependencies = [ + "clang", + "clang-sys", + "dunce", + "once_cell", + "percent-encoding", + "regex", +] + +[[package]] +name = "percent-encoding" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" + +[[package]] +name = "pkg-config" +version = "0.3.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" + +[[package]] +name = "regex" +version = "1.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebee201405406dbf528b8b672104ae6d6d63e6d118cb10e4d51abbc7b58044ff" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59b23e92ee4318893fa3fe3e6fb365258efbfe6ac6ab30f090cdcbb7aa37efa9" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" + +[[package]] +name = "semver" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad977052201c6de01a8ef2aa3378c4bd23217a056337d1d6da40468d267a4fb0" + +[[package]] +name = "shlex" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7cee0529a6d40f580e7a5e6c495c8fbfe21b7b52795ed4bb5e62cdf92bc6380" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..a94e130 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,3 @@ +[workspace] +members = ["object_tracking"] +resolver = "2" diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..4694cda --- /dev/null +++ b/flake.lock @@ -0,0 +1,96 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1681202837, + "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "cfacdce06f30d2b68473a46042957675eebb3401", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1695830400, + "narHash": "sha256-gToZXQVr0G/1WriO83olnqrLSHF2Jb8BPcmCt497ro0=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "8a86b98f0ba1c405358f1b71ff8b5e1d317f5db2", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1681358109, + "narHash": "sha256-eKyxW4OohHQx9Urxi7TQlFBTDWII+F+x2hklDOQPB50=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "96ba1c52e54e74c3197f4d43026b3f3d92e83ff9", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1696039808, + "narHash": "sha256-7TbAr9LskWG6ISPhUdyp6zHboT7FsFrME5QsWKybPTA=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "a4c3c904ab29e04a20d3a6da6626d66030385773", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..02fbbe2 --- /dev/null +++ b/flake.nix @@ -0,0 +1,122 @@ +/* +TODO +1. Find and replace "opencv_tracking" with your package name for **ALL FILES IN REPOSITORY** +2. Add a flake description that describes the workspace on line 27 +3. Add a package description on line 70 +4. (optional) uncomment `nativeBuildInputs` and `buildInputs` on lines 43 and 44 if you need openssl +5. (optional) set your project homepage, license, and maintainers list on lines 48-51 +6. (optional) uncomment the NixOS module and update it for your needs +7. Delete this comment block +*/ + +/* +Some utility commands: +- `nix flake update --commit-lock-file` +- `nix flake lock update-input ` +- `nix build .#opencv_tracking` or `nix build .` +- `nix run .#opencv_tracking` or `nix run .` +*/ + +{ + description = ""; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + rust-overlay.url = "github:oxalica/rust-overlay"; + }; + + outputs = { self, nixpkgs, rust-overlay }: + let + overlays = [ (import rust-overlay) ]; + system = "x86_64-linux"; + pkgs = import nixpkgs { + inherit system overlays; + }; + rustSettings = with pkgs; { + src = ./.; + nativeBuildInputs = [ pkg-config ]; + buildInputs = [ + (opencv.override { + enableGtk2 = true; + enableFfmpeg = true; + }) + ]; + cargoHash = nixpkgs.lib.fakeHash; + }; + meta = with nixpkgs.lib; { + #homepage = "https://example.com"; + #license = [ licenses.gpl3 ]; + platforms = [ system ]; + #maintainers = with maintainers; [ ]; + }; + in { + devShells.${system}.default = with pkgs; mkShell { + shellHook = '' + export LIBCLANG_PATH="${llvmPackages.libclang.lib}/lib" + ''; + packages = [ + (pkgs.rust-bin.stable.latest.default.override { + extensions = [ "rust-src" ]; + }) + bacon + (opencv.override { + enableGtk2 = true; + enableFfmpeg = true; + }) + llvmPackages.clang + ]; + inputsFrom = with self.packages.${system}; [ opencv_tracking ]; + }; + packages.${system} = { + default = self.packages.${system}.opencv_tracking; + opencv_tracking = pkgs.rustPlatform.buildRustPackage (rustSettings // { + pname = "opencv_tracking"; + version = "0.1.0"; + buildAndTestSubdir = "opencv_tracking"; + cargoHash = "sha256-+TaGIiKf+Pz2bTABeG8aCZz0/ZTCKl5398+qbas4Nvo="; + meta = meta // { + description = ""; + }; + }); + }; + /* + nixosModules.default = { config, ... }: let + lib = nixpkgs.lib; + in { + options.services.opencv_tracking = { + enable = lib.mkEnableOption (lib.mdDoc "opencv_tracking service"); + package = lib.mkOption { + type = lib.types.package; + default = self.packages.${system}.opencv_tracking; + defaultText = "pkgs.opencv_tracking"; + description = lib.mdDoc '' + The opencv_tracking package that should be used. + ''; + }; + port = lib.mkOption { + type = lib.types.port; + default = 8000; + description = lib.mdDoc '' + The port at which to run. + ''; + }; + }; + config.systemd.services.opencv_tracking = let + cfg = config.services.opencv_tracking; + pkg = self.packages.${system}.opencv_tracking; + in lib.mkIf cfg.enable { + description = pkg.meta.description; + after = [ "network.target" ]; + wantedBy = [ "network.target" ]; + serviceConfig = { + ExecStart = '' + ${cfg.package}/bin/opencv_tracking --port ${builtins.toString cfg.port} + ''; + Restart = "always"; + DynamicUser = true; + }; + }; + }; + */ + }; +} diff --git a/object_tracking/Cargo.toml b/object_tracking/Cargo.toml new file mode 100644 index 0000000..1abb531 --- /dev/null +++ b/object_tracking/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "object_tracking" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.75" +opencv = "0.84.5" diff --git a/object_tracking/src/main.rs b/object_tracking/src/main.rs new file mode 100644 index 0000000..9dde802 --- /dev/null +++ b/object_tracking/src/main.rs @@ -0,0 +1,24 @@ +use anyhow::Result; // Automatically handle the error types +use opencv::{ + prelude::*, + videoio, + highgui +}; // Note, the namespace of OpenCV is changed (to better or worse). It is no longer one enormous. +fn main() -> Result<()> { // Note, this is anyhow::Result + // Open a GUI window + highgui::named_window("window", highgui::WINDOW_FULLSCREEN)?; + // Open the web-camera (assuming you have one) + let mut cam = videoio::VideoCapture::new(0, videoio::CAP_ANY)?; + let mut frame = Mat::default(); // This array will store the web-cam data + // Read the camera + // and display in the window + loop { + cam.read(&mut frame)?; + highgui::imshow("window", &frame)?; + let key = highgui::wait_key(1)?; + if key == 113 { // quit with q + break; + } + } + Ok(()) +}