{"id":17229486,"url":"https://github.com/legoless/legocv","last_synced_at":"2025-07-30T07:42:33.809Z","repository":{"id":56919695,"uuid":"78846571","full_name":"Legoless/LegoCV","owner":"Legoless","description":"Native OpenCV Swift Framework","archived":false,"fork":false,"pushed_at":"2019-05-21T19:48:50.000Z","size":352,"stargazers_count":194,"open_issues_count":15,"forks_count":27,"subscribers_count":11,"default_branch":"master","last_synced_at":"2024-12-10T10:12:01.871Z","etag":null,"topics":["framework","opencv","swift"],"latest_commit_sha":null,"homepage":"https://github.com/legoless/legocv","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Legoless.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-13T12:15:55.000Z","updated_at":"2024-11-28T18:17:10.000Z","dependencies_parsed_at":"2022-08-21T04:20:15.301Z","dependency_job_id":null,"html_url":"https://github.com/Legoless/LegoCV","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/Legoless%2FLegoCV","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Legoless%2FLegoCV/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Legoless%2FLegoCV/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Legoless%2FLegoCV/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Legoless","download_url":"https://codeload.github.com/Legoless/LegoCV/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230520390,"owners_count":18238948,"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":["framework","opencv","swift"],"created_at":"2024-10-15T04:48:34.985Z","updated_at":"2024-12-20T01:14:07.342Z","avatar_url":"https://github.com/Legoless.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LegoCV\n\nLegoCV is native OpenCV framework built for Swift and Objective-C projects. It eliminates the need to use Objective-C++ and write bringing code, and allows for full compatibility with native Swift projects. The only dependency is the native [OpenCV framework](http://opencv.org) for **iOS** (*and later macOS and tvOS*).\n\n## Vision\n\nSwift is one of the fastest evolving languages, but there is currently no way to use C++ frameworks directly, as it was possible be with Objective-C/C++.\n\nThis project's purpose is to create a simple, easy to use native Swift framework for OpenCV. The project adds Swift and Objective-C convenience methods, but translates to OpenCV API entirely.\n\n*The idea is to simply wrap OpenCV native C++ classes into lightweight Objective-C classes, which are then natively bridged to Swift, providing a thin layer on top of native OpenCV. [Realm](https://realm.io) and [EmguCV](http://www.emgu.com) in C# use similar framework structure. Possibility for fully native cross-platform Swift version of OpenCV exists in the future.*\n\n## Example\n\nThe following examples display the difference with using LegoCV in Swift or Objective-C compared to vanilla OpenCV in C++.\nThe example is extracted from Face detection sample code, included with LegoCV. On iOS it uses `OCVVideoCamera` wrapper class to get image stream from camera (wraps OpenCV's `CvVideoCamera`, to keep backward compatibility).\n\n#### Swift (LegoCV):\n```swift\nlet faceDetector = OCVCascadeClassifier();\nfaceDetector.load(path: \"haarcascade_frontalface_alt2.xml\")\n\nfunc process(image: OCVMat) {\n    let scale = 2.0\n\n    let minSize = OCVSize(width: 30, height: 30)\n    \n    let size = CGSize(width: 140.0, height: 140.00).ocvSize\n        \n    let gray = OCVMat()\n    let smallImage = OCVMat(rows: Int(round(Double(image.rows) / scale)), cols: Int(round(Double(image.cols) / scale)), type: .cv8U, channels: 1)\n    \n    //\n    // OpenCV Default Syntax requires to predefine both input and output\n    //\n    OCVOperation.convertColor(from: image, to: gray, with: .BGR2GRAY)\n    \n    //\n    // LegoCV syntactic sugar allows you to perform operations directly on the input, only defining output.\n    //\n    image.convertColor(to: gray, with: .BGR2GRAY)\n    let grayImg = image.convertColor(with: .BGR2GRAY)\n    \n    OCVOperation.convertColor(from: image, to: gray, with: .BGR2GRAY)\n    OCVOperation.resize(from: gray, to: smallImage, size: smallImage.size, fx: 0, fy: 0, interpolation: .linear)\n    OCVOperation.equalizeHistogram(from: smallImage, to: smallImage)\n    \n    //\n    // Faces are returned as OCVRect instances, so they are mapped in Swift, as they are structs.\n    //\n    var faces : [OCVRect] = faceDetector.detectMultiscale(with: smallImage, scaleFactor: 1.1, minNeighbours: 2, flags: 0, minSize: minSize).map { $0.rect }\n    \n    //\n    // More LegoCV objective syntactic sugar\n    //\n    let result : OCVCascadeClassifierResult = faceDetector.detectMultiscale(on: smallImage, with: OCVCascadeClassifierOptions.default)\n    faces = result.objects\n    \n}\n```\n\n#### Objective-C (LegoCV with Objective-C):\n```objectivec\n- (void)setupClassifier {\n    self.faceDetector = [[OCVCascadeClassifier alloc] init];\n    [self.faceDetector loadPath:@\"haarcascade_frontalface_alt2.xml\"];\n}\n\n- (void)processImage:(OCVMat *)image {\n    double scale = 2.0;\n\n    OCVSize minSize;\n    minSize.width = 30;\n    minSize.height = 30;\n        \n    OCVMat* gray = [[OCVMat alloc] init];\n    OCVMat* smallImage = [[OCVMat alloc] initWithRows:round(image.rows / scale) cols:round(image.cols / scale) type: OCVDepthTypeCv8U, channels: 1)\n    \n    [OCVOperation convertColorFromSource:image toDestination:gray with:OCVColorConversionTypeBGR2GRAY];\n    [OCVOperation resizeFromSource:gray toDestination:smallImage size:smallImage.size fx:0 fy:0 interpolation:OCVInterpolationTypeLinear];\n    [OCVOperation equalizeHistogramFromSource:smallImage toDestination:smallImage];\n    \n    //\n    // Faces are returned as OCVRectValue instances, which wrap OCVRect structs.\n    //\n    NSArray\u003cOCVRectValue *\u003e* faces = [self.faceDetector detectMultiscaleWith:smallImage scaleFactor:1.1 minNeighbours:2 flags: 0 minSize:minSize];\n    \n    //\n    // Call the face detector classifier\n    //\n    OCVCascadeClassifierResult* result = [self.faceDetector detectMultiScaleOnImage:smallImage withOptions:[OCVCascadeClassifierOptions defaultOptions]];\n}\n```\n\n#### C++ (OpenCV):\n```cpp\nusing namespace cv;\n\nvoid setup () {\n    _faceDetector = new CascadeClassifier();\n    _faceDetector-\u003eload(\"haarcascade_frontalface_alt2.xml\");\n}\n\nvoid processImage(cv::Mat img) {\n    double scale = 2.0;\n    Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );\n    \n    cvtColor( img, gray, COLOR_BGR2GRAY );\n    resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );\n    equalizeHist( smallImg, smallImg );\n\n    cv::Size minSize(30,30);\n\n    vector\u003ccv::Rect\u003e faceRects;\n\n    // Faces are returned in provided faceRects vector\n    _faceDetector-\u003edetectMultiScale(smallImg, faceRects, 1.1, 2, 0, minSize);\n}\n```\n\nMore examples, including Swift playgrounds can be found in the sample project.\n\n## Documentation\n\nAs this is a project in progress, documentation will be added to [Wiki]() as development progresses.\n\n## Performance\n\nThere is a smaller performance impact compared to pure native C++ code of OpenCV, due to Objective-C messaging system. If you need a high performance code, it is still recommended to write the algorithm in C++ and add bridges to LegoCV or Objective-C.\n\n## Installation\n\n### Prerequisites\n\nFor LegoCV you need `cmake`. Install it with `brew install cmake` and make sure you have Xcode Command Line tools installed. Trigger with `xcode-select --install` to check. Also make sure you use latest Xcode version and not Beta for master branch.\n\n### CocoaPods\n\nLegoCV can be installed with [CocoaPods](https://cocoapods.org) or [Carthage](https://github.com/Carthage/Carthage). It's only dependency is OpenCV framework, which can be downloaded from their website.\n\n```ruby\npod 'LegoCV'\n\n# Use only specific modules\npod 'LegoCV/Core'\npod 'LegoCV/VideoIO'\n```\n\n*LegoCV supports iOS 8 and higher.*\n\n### Example Project\n\n1. First clone the project:\n\n`git clone git@github.com:legoless/legocv.git`\n`cd legocv`\n\n2. Initialize submodules\n\n```\ngit submodule init\ngit submodule update\n```\n\n3. Build `opencv2.framework` from git repository.\n\n```\n/usr/bin/python opencv/platforms/ios/build_framework.py ios --dynamic\n```\n\n4. Open `LegoCV.xcodeproj` and build.\n\n\n# License\n\n[BSD license](https://github.com/legoless/legocv/blob/master/LICENSE), respect [OpenCV](https://github.com/opencv/opencv/blob/master/LICENSE) license as well.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flegoless%2Flegocv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flegoless%2Flegocv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flegoless%2Flegocv/lists"}