Skip to content

Commit eeed402

Browse files
committed
Test qr scanner
1 parent 5ea9b7a commit eeed402

File tree

8 files changed

+56
-25
lines changed

8 files changed

+56
-25
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export(ocv_stylize)
3131
export(ocv_version)
3232
export(ocv_video)
3333
export(ocv_write)
34+
export(qr_scanner)
3435
importFrom(Rcpp,sourceCpp)
3536
importFrom(magrittr,"%>%")
3637
useDynLib(opencv)

R/RcppExports.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ cvmat_display <- function(ptr) {
8181
invisible(.Call('_opencv_cvmat_display', PACKAGE = 'opencv', ptr))
8282
}
8383

84-
livestream <- function(filter) {
85-
invisible(.Call('_opencv_livestream', PACKAGE = 'opencv', filter))
84+
livestream <- function(filter, stop_on_result = FALSE) {
85+
.Call('_opencv_livestream', PACKAGE = 'opencv', filter, stop_on_result)
8686
}
8787

8888
data_prefix <- function() {

R/opencv.R

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,13 @@ ocv_display <- function(image){
160160
#' @export
161161
#' @rdname opencv
162162
#' @param filter an R function that takes and returns an opecv image
163-
ocv_video <- function(filter){
163+
ocv_video <- function(filter, stop_on_result = FALSE){
164164
if(!is.function(filter))
165165
stop("Filter must be a function")
166166
message("Starting video window (could be behind this window)")
167-
livestream(function(image){
168-
if(!inherits(image, 'opencv-image'))
169-
stop("Image must be opencv-image")
170-
out <- filter(image)
171-
if(!inherits(out, 'opencv-image'))
172-
stop("Output must be opencv-image")
173-
return(out)
174-
})
167+
livestream(filter, stop_on_result)
175168
}
176169

177-
178170
#' @export
179171
#' @rdname opencv
180172
ocv_grayscale <- function(image){

R/qr.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,15 @@ ocv_qrtext <- function(image){
1818
ocv_qrmask <- function(image){
1919
cvmat_qrmask(image)
2020
}
21+
22+
#' @export
23+
#' @rdname qrcode
24+
qr_scanner <- function(){
25+
ocv_video(function(im){
26+
out <- ocv_qrmask(im)
27+
value <- attr(out, 'value')
28+
if(length(value)){
29+
list(out = out, value = value)
30+
}
31+
}, stop_on_result = TRUE)
32+
}

man/qrcode.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/RcppExports.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,15 @@ BEGIN_RCPP
246246
END_RCPP
247247
}
248248
// livestream
249-
void livestream(Rcpp::Function filter);
250-
RcppExport SEXP _opencv_livestream(SEXP filterSEXP) {
249+
Rcpp::RObject livestream(Rcpp::Function filter, bool stop_on_result);
250+
RcppExport SEXP _opencv_livestream(SEXP filterSEXP, SEXP stop_on_resultSEXP) {
251251
BEGIN_RCPP
252+
Rcpp::RObject rcpp_result_gen;
252253
Rcpp::RNGScope rcpp_rngScope_gen;
253254
Rcpp::traits::input_parameter< Rcpp::Function >::type filter(filterSEXP);
254-
livestream(filter);
255-
return R_NilValue;
255+
Rcpp::traits::input_parameter< bool >::type stop_on_result(stop_on_resultSEXP);
256+
rcpp_result_gen = Rcpp::wrap(livestream(filter, stop_on_result));
257+
return rcpp_result_gen;
256258
END_RCPP
257259
}
258260
// data_prefix
@@ -507,7 +509,7 @@ static const R_CallMethodDef CallEntries[] = {
507509
{"_opencv_cvmat_copyto", (DL_FUNC) &_opencv_cvmat_copyto, 3},
508510
{"_opencv_cvmat_info", (DL_FUNC) &_opencv_cvmat_info, 1},
509511
{"_opencv_cvmat_display", (DL_FUNC) &_opencv_cvmat_display, 1},
510-
{"_opencv_livestream", (DL_FUNC) &_opencv_livestream, 1},
512+
{"_opencv_livestream", (DL_FUNC) &_opencv_livestream, 2},
511513
{"_opencv_data_prefix", (DL_FUNC) &_opencv_data_prefix, 0},
512514
{"_opencv_set_num_threads", (DL_FUNC) &_opencv_set_num_threads, 1},
513515
{"_opencv_cvmat_grayscale", (DL_FUNC) &_opencv_cvmat_grayscale, 1},

src/base.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,25 +175,46 @@ void cvmat_display(XPtrMat ptr){
175175
}
176176

177177
// [[Rcpp::export]]
178-
void livestream(Rcpp::Function filter){
178+
Rcpp::RObject livestream(Rcpp::Function filter, bool stop_on_result = false){
179179
VideoCapture cap(0);
180180
if(!cap.isOpened())
181181
throw std::runtime_error("Failed to open Camera");
182182
Mat image;
183183
namedWindow("mywindow", 1);
184+
Rcpp::RObject out = R_NilValue;
184185
try {
185186
for(int i = 0;;i++) {
186187
cap >> image;
187-
XPtrMat out(filter(cvmat_xptr(image)));
188-
imshow("mywindow", get_mat(out));
188+
Rcpp::RObject val = filter(cvmat_xptr(image));
189+
if(stop_on_result){
190+
if(val != R_NilValue){
191+
out = val;
192+
break;
193+
}
194+
imshow("mywindow", image);
195+
} else {
196+
if(val.inherits("opencv-image")){
197+
imshow("mywindow", get_mat(XPtrMat(val)));
198+
} else {
199+
REprintf("\rFilter function did not return opencv-image object. Showing input image. (%d)", i);
200+
imshow("mywindow", image);
201+
}
202+
}
189203
if(waitKey(30) >= 0 || cv::getWindowProperty("mywindow", 0) < 0)
190204
break;
191205
Rcpp::checkUserInterrupt();
192206
}
193-
} catch(Rcpp::internal::InterruptedException e) { }
194-
cap.release();
195-
cv::destroyWindow("mywindow");
196-
cv::waitKey(1);
207+
} catch(Rcpp::internal::InterruptedException e) {
208+
cap.release();
209+
cv::destroyWindow("mywindow");
210+
cv::waitKey(1);
211+
} catch(std::exception e) {
212+
cap.release();
213+
cv::destroyWindow("mywindow");
214+
cv::waitKey(1);
215+
throw e;
216+
}
217+
return out;
197218
}
198219

199220
// [[Rcpp::export]]

src/qrdetect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ XPtrMat cvmat_qrmask(XPtrMat ptr){
2929
1.0,
3030
CV_RGB(118, 185, 0), //font color
3131
2);
32-
32+
ptr.attr("value") = Rcpp::CharacterVector::create(data);
3333
}
3434
return ptr;
3535
}

0 commit comments

Comments
 (0)