Skip to content

Commit 7f4f15e

Browse files
a31859a31859
authored andcommitted
add start/stop processing frames an timeout
1 parent d99e98e commit 7f4f15e

File tree

4 files changed

+116
-28
lines changed

4 files changed

+116
-28
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,22 @@ Work in progress...
1515
The plugin aims to be used with iOS version >= 7.
1616

1717
## Usage
18-
The plugin offers two functions `isDetecting` and `setPattern`.
18+
The plugin offers the functions `startProcessing`, `isDetecting` and `setPattern`.
19+
20+
`startProcessing` - the plugin will process the video frames captured by the camera if the inputed argument is `true`, if the argument is `false` no frames will be processed. Calls on success if the argument is set and on error if no value set.
21+
**Note:** the plugins start with this option true.
22+
```javascript
23+
startProcessing(true or false, successCallback, errorCallback);
24+
```
25+
1926
`isDetecting` - the plugin will callback on success function if detecting the pattern or on error function if it's not.
2027
```javascript
2128
isDetecting(successCallback, errorCallback);
2229
```
30+
`setDetectionTimeout` - this function will set a timeout (**in seconds**) in which the processing of the frames will not occur. Calls on success if the argument is set and on error if no value set.
31+
```javascript
32+
setDetectionTimeout(timeout, successCallback, errorCallback);
33+
```
2334

2435
`setPattern` - sets the new pattern target to be detected. Calls on success if the pattern is set and on error if no pattern set. The input pattern must be a base64 image.
2536
```javascript
@@ -28,6 +39,8 @@ setPattern(base64image, successCallback, errorCallback);
2839

2940
## Usage example
3041
```javascript
42+
ImageDetectionPlugin.startProcessing(true, function(success){console.log(success);}, function(error){console.log(error);});
43+
3144
ImageDetectionPlugin.isDetecting(function(success){console.log(success);}, function(error){console.log(error);});
3245

3346
var img = new Image();
@@ -44,4 +57,6 @@ img.onload = function () {
4457
canvas = null;
4558
};
4659
img.src = "img/patterns/coke.jpg";
60+
61+
ImageDetectionPlugin.setDetectionTimeout(2, function(success){console.log(success);}, function(error){console.log(error);});
4762
```

src/ios/ImageDetectionPlugin.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
{
88
UIImageView *img;
99
CvVideoCamera *camera;
10-
CDVInvokedUrlCommand *_command;
1110
}
1211

1312
@property (nonatomic, retain) CvVideoCamera *camera;
@@ -19,4 +18,8 @@
1918

2019
- (void)setPattern:(CDVInvokedUrlCommand*)command;
2120

21+
- (void)startProcessing:(CDVInvokedUrlCommand*)command;
22+
23+
- (void)setDetectionTimeout:(CDVInvokedUrlCommand*)command;
24+
2225
@end

src/ios/ImageDetectionPlugin.mm

Lines changed: 90 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ @interface ImageDetectionPlugin()
1010
{
1111
Mat patt, desc1;
1212
vector<KeyPoint> kp1;
13-
bool debug, thread_over, called_success_detection, called_failed_detection;
13+
bool processFrames, debug, thread_over, called_success_detection, called_failed_detection;
1414
NSMutableArray *detection;
1515
NSString *callbackID;
16+
NSNumber *last_time;
17+
float timeout;
1618
}
1719

1820
@end
@@ -41,22 +43,22 @@ - (void)greet:(CDVInvokedUrlCommand*)command
4143
-(void)isDetecting:(CDVInvokedUrlCommand*)command
4244
{
4345
callbackID = command.callbackId;
44-
// [self.commandDelegate runInBackground:^{
45-
// CDVPluginResult* plugin_result = nil;
46-
// NSString* msg;
47-
//
48-
// if ([self getState]) {
49-
// msg = @"pattern detected";
50-
// plugin_result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:msg];
51-
// [plugin_result setKeepCallbackAsBool:YES];
52-
// } else {
53-
// msg = @"pattern not detected";
54-
// plugin_result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:msg];
55-
// [plugin_result setKeepCallbackAsBool:YES];
56-
// }
57-
//
58-
// [self.commandDelegate sendPluginResult:plugin_result callbackId:command.callbackId];
59-
// }];
46+
// [self.commandDelegate runInBackground:^{
47+
// CDVPluginResult* plugin_result = nil;
48+
// NSString* msg;
49+
//
50+
// if ([self getState]) {
51+
// msg = @"pattern detected";
52+
// plugin_result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:msg];
53+
// [plugin_result setKeepCallbackAsBool:YES];
54+
// } else {
55+
// msg = @"pattern not detected";
56+
// plugin_result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:msg];
57+
// [plugin_result setKeepCallbackAsBool:YES];
58+
// }
59+
//
60+
// [self.commandDelegate sendPluginResult:plugin_result callbackId:command.callbackId];
61+
// }];
6062
}
6163

6264
- (void)setPattern:(CDVInvokedUrlCommand*)command;
@@ -79,6 +81,51 @@ - (void)setPattern:(CDVInvokedUrlCommand*)command;
7981
}];
8082
}
8183

84+
- (void)startProcessing:(CDVInvokedUrlCommand*)command;
85+
{
86+
[self.commandDelegate runInBackground:^{
87+
CDVPluginResult* plugin_result = nil;
88+
NSNumber* argVal = [command.arguments objectAtIndex:0];
89+
NSString* msg;
90+
91+
if (argVal != nil) {
92+
if ([argVal boolValue] == YES) {
93+
processFrames = true;
94+
msg = @"Frame processing set to 'true'";
95+
} else {
96+
processFrames = false;
97+
msg = @"Frame processing set to 'false'";
98+
}
99+
plugin_result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:msg];
100+
} else {
101+
msg = @"No value";
102+
plugin_result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:msg];
103+
}
104+
105+
[self.commandDelegate sendPluginResult:plugin_result callbackId:command.callbackId];
106+
}];
107+
}
108+
109+
- (void)setDetectionTimeout:(CDVInvokedUrlCommand*)command;
110+
{
111+
[self.commandDelegate runInBackground:^{
112+
CDVPluginResult* plugin_result = nil;
113+
NSNumber* argVal = [command.arguments objectAtIndex:0];
114+
NSString* msg;
115+
116+
if (argVal != nil && argVal > 0) {
117+
timeout = [argVal floatValue];
118+
msg = [NSString stringWithFormat:@"Processing timeout set to %@", argVal];
119+
plugin_result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:msg];
120+
} else {
121+
msg = @"No value or timeout value negative.";
122+
plugin_result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:msg];
123+
}
124+
125+
[self.commandDelegate sendPluginResult:plugin_result callbackId:command.callbackId];
126+
}];
127+
}
128+
82129
-(void)setBase64Pattern:(NSString *)image_base64
83130
{
84131
if ([image_base64 rangeOfString:@"data:"].location == NSNotFound) {
@@ -128,11 +175,16 @@ - (void)pluginInitialize {
128175

129176
self.camera.delegate = self;
130177

178+
processFrames = true;
131179
debug = false;
132180
thread_over = true;
133181
called_success_detection = false;
134182
called_failed_detection = true;
135183

184+
timeout = 0.0;
185+
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
186+
last_time = [NSNumber numberWithDouble: timeStamp];
187+
136188
detection = [[NSMutableArray alloc] init];
137189

138190
[self.camera start];
@@ -143,16 +195,28 @@ - (void)pluginInitialize {
143195
#ifdef __cplusplus
144196
- (void)processImage:(Mat &)image;
145197
{
146-
// process each image in new thread
147-
if(!image.empty() && thread_over){
148-
thread_over = false;
149-
Mat image_copy = image.clone();
150-
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
151-
[self backgroundImageProcessing: image_copy];
152-
dispatch_sync(dispatch_get_main_queue(), ^{
153-
thread_over = true;
198+
//get current time and calculate time passed since last time update
199+
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
200+
NSNumber *current_time = [NSNumber numberWithDouble: timeStamp];
201+
float time_passed = [current_time floatValue] - [last_time floatValue];
202+
//NSLog(@"current_time - %@, last_time - %@", current_time, last_time); && time_passed > timeout
203+
204+
//process frames if option is true and timeout passed
205+
if (processFrames) {
206+
// process each image in new thread
207+
if(!image.empty() && thread_over){
208+
thread_over = false;
209+
Mat image_copy = image.clone();
210+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
211+
[self backgroundImageProcessing: image_copy];
212+
dispatch_sync(dispatch_get_main_queue(), ^{
213+
thread_over = true;
214+
});
154215
});
155-
});
216+
}
217+
//NSLog(@"time passed - %f, timeout - %f", time_passed, timeout);
218+
//update time
219+
last_time = current_time;
156220
}
157221
}
158222
#endif

www/ImageDetectionPlugin.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22
*
33
**/
44
var ImageDetectionPlugin = function () {
5+
this.startProcessing = function (bool, successCallback, errorCallback) {
6+
cordova.exec(successCallback, errorCallback, "ImageDetectionPlugin", "startProcessing", [bool]);
7+
};
58
this.setPattern = function (pattern, successCallback, errorCallback) {
69
cordova.exec(successCallback, errorCallback, "ImageDetectionPlugin", "setPattern", [pattern]);
710
};
811
this.isDetecting = function (successCallback, errorCallback) {
912
cordova.exec(successCallback, errorCallback, "ImageDetectionPlugin", "isDetecting", []);
1013
};
14+
this.setDetectionTimeout = function (timeout, successCallback, errorCallback) {
15+
cordova.exec(successCallback, errorCallback, "ImageDetectionPlugin", "setDetectionTimeout", [timeout]);
16+
};
1117
this.greet = function (name, successCallback, errorCallback) {
1218
cordova.exec(successCallback, errorCallback, "ImageDetectionPlugin", "greet", [name]);
1319
};

0 commit comments

Comments
 (0)