This commit is contained in:
2021-11-17 14:57:56 -06:00
commit f17b64232d
177 changed files with 6639 additions and 0 deletions

39
native_opencv/ios/.gitignore vendored Normal file
View File

@@ -0,0 +1,39 @@
.idea/
.vagrant/
.sconsign.dblite
.svn/
.DS_Store
*.swp
profile
DerivedData/
build/
GeneratedPluginRegistrant.h
GeneratedPluginRegistrant.m
.generated/
*.pbxuser
*.mode1v3
*.mode2v3
*.perspectivev3
!default.pbxuser
!default.mode1v3
!default.mode2v3
!default.perspectivev3
xcuserdata
*.moved-aside
*.pyc
*sync/
Icon?
.tags*
/Flutter/Generated.xcconfig
/Flutter/flutter_export_environment.sh
opencv2.framework

View File

View File

@@ -0,0 +1,4 @@
#import <Flutter/Flutter.h>
@interface NativeOpencvPlugin : NSObject<FlutterPlugin>
@end

View File

@@ -0,0 +1,15 @@
#import "NativeOpencvPlugin.h"
#if __has_include(<native_opencv/native_opencv-Swift.h>)
#import <native_opencv/native_opencv-Swift.h>
#else
// Support project import fallback if the generated compatibility header
// is not copied when this plugin is created as a library.
// https://forums.swift.org/t/swift-static-libraries-dont-copy-generated-objective-c-header/19816
#import "native_opencv-Swift.h"
#endif
@implementation NativeOpencvPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
[SwiftNativeOpencvPlugin registerWithRegistrar:registrar];
}
@end

View File

@@ -0,0 +1,14 @@
import Flutter
import UIKit
public class SwiftNativeOpencvPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "native_opencv", binaryMessenger: registrar.messenger())
let instance = SwiftNativeOpencvPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
result("iOS " + UIDevice.current.systemVersion)
}
}

View File

@@ -0,0 +1,124 @@
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <chrono>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32)
#define IS_WIN32
#endif
#ifdef __ANDROID__
#include <android/log.h>
#endif
#ifdef IS_WIN32
#include <windows.h>
#endif
#if defined(__GNUC__)
// Attributes to prevent 'unused' function from being removed and to make it visible
#define FUNCTION_ATTRIBUTE __attribute__((visibility("default"))) __attribute__((used))
#elif defined(_MSC_VER)
// Marking a function for export
#define FUNCTION_ATTRIBUTE __declspec(dllexport)
#endif
using namespace cv;
using namespace std;
long long int get_now()
{
return chrono::duration_cast<std::chrono::milliseconds>(
chrono::system_clock::now().time_since_epoch())
.count();
}
void platform_log(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
#ifdef __ANDROID__
__android_log_vprint(ANDROID_LOG_VERBOSE, "ndk", fmt, args);
#elif defined(IS_WIN32)
char *buf = new char[4096];
std::fill_n(buf, 4096, '\0');
_vsprintf_p(buf, 4096, fmt, args);
OutputDebugStringA(buf);
delete[] buf;
#else
vprintf(fmt, args);
#endif
va_end(args);
}
// Avoiding name mangling
extern "C"
{
FUNCTION_ATTRIBUTE
const char *version()
{
return CV_VERSION;
}
FUNCTION_ATTRIBUTE
void process_image(char *inputImagePath, char *outputImagePath, double frame_rate)
{
platform_log("Starting the process");
long long start = get_now();
//Video Capture
VideoCapture cap = VideoCapture(inputImagePath);
platform_log("DURRRR %f",frame_rate);
const double scale = 1.0 / 16.0;
//Working Image Containers
Mat src;
Mat dst;
//Background sub
Mat fgMask;
auto subtractor = createBackgroundSubtractorMOG2(150, 100);
Size k_size = Size(1, 1);
//Centroid Variables
double posx = 0.0;
double posy = 0.0;
const double alpha = 0.995;
Moments mom;
double cX;
double cY;
//Drawing
const Scalar color = Scalar(0, 0, 255);
//Loop Management
while (true)
{
cap >> src;
if (src.empty())
{
break;
}
resize(src, src, Size(), scale, scale);
blur(src, dst, k_size);
subtractor->apply(src, fgMask);
mom = moments(fgMask);
if (mom.m00 != 0)
{
cX = mom.m10 / mom.m00;
cY = mom.m01 / mom.m00;
posx = posx * alpha + cX * (1.0 - alpha);
posy = posy * alpha + cY * (1.0 - alpha);
}
platform_log("Positions %f %f %f", posx, posy, mom.m00/(src.size[0] * src.size[1]* 255));
}
int evalInMillis = static_cast<int>(get_now() - start);
platform_log("Processing done in %dms\n", evalInMillis);
}
}

View File

@@ -0,0 +1,38 @@
#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html.
# Run `pod lib lint native_opencv.podspec' to validate before publishing.
#
Pod::Spec.new do |s|
s.name = 'native_opencv'
s.version = '0.0.1'
s.summary = 'A new flutter plugin project.'
s.description = <<-DESC
A new flutter plugin project.
DESC
s.homepage = 'http://example.com'
s.license = { :file => '../LICENSE' }
s.author = { 'Your Company' => 'email@example.com' }
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.dependency 'Flutter'
s.platform = :ios, '8.0'
# Flutter.framework does not contain a i386 slice.
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386 arm64' }
s.swift_version = '5.0'
# telling CocoaPods not to remove framework
s.preserve_paths = 'opencv2.framework'
# telling linker to include opencv2 framework
s.xcconfig = { 'OTHER_LDFLAGS' => '-framework opencv2' }
# including OpenCV framework
s.vendored_frameworks = 'opencv2.framework'
# including native framework
s.frameworks = 'AVFoundation'
# including C++ library
s.library = 'c++'
end