swarm-lab / Rvision

Basic computer vision library for R
https://swarm-lab.github.io/Rvision/
GNU General Public License v3.0
141 stars 34 forks source link

Lists camera indexes #61

Open CorradoLanera opened 1 year ago

CorradoLanera commented 1 year ago

If I try to start a stream with an index unrelated to a camera, the whole R session crashes.

Is it possible to have a function that lists all possible available indexes?

E.g.:

idxs <- available_indexes()
streams <- lapply(idxs, Rvision::stream)

Or, how can I check if an index will not crash my session; e.g.

idx <- 1234

stream <- if (will_not_crash(idx)) {
  Rvision::stream(idx) 
} else {
  NULL
}

Thank you! Corrado.

sjmgarnier commented 1 year ago

@CorradoLanera The answer is no, it's not possible to have a function that does this consistently across OS and APIs. OpenCV itself - on which Rvision is based - doesn't even provide a function to do that. There are workarounds to guess the number of available cameras but they are system and API specific, so I will not implement them and I'll let the users design their own function if they really need one.

But generally speaking, if your problem is simply to keep your code running if a given index doesn't point towards a working camera, then it can be easily solved using a tryCatch call. For instance:

st <- tryCatch(stream(9), error = function(e) FALSE)
if (isStream(st)) {
    # DO SOMETHING
} else {
    # DO SOMETHING ELSE
}

This will return in st either FALSE if opening the stream leads to an error or the stream itself if it can be opened. Then you can decide what to do depending on what's stored in st. Note that you can have tryCatch return anything on error, not just FALSE (you could return NULL for instance).

CorradoLanera commented 1 year ago

Thank you, @sjmgarnier; that was exactly my approach. The issue is that I have nothing to "catch" because the whole session crashed. So, I start supposing there is a different issue, i.e., mainly mine. I tried an extreme minimal example :-) Obtaining the same result:

immagine

As you can see, my system's problem seems to be not in the implementation but something corrupted somewhere at a low level. So, I'll try to reinstall {Rvision} and its dependencies. I'll come back as soon as I complete the restoration.

Thank you again for your time and patience. Corrado.

EDIT: Please, note that anyway, the issue happens for "wrong" indexes only: immagine

CorradoLanera commented 1 year ago

Updated OpenCV and Rvision: nothing changed.

immagine

Do you have any suggestions to try to debug/understand that? :-)

sjmgarnier commented 1 year ago

@CorradoLanera Ok, yes, this is a different problem than what I was thinking about. I'll see if I can reproduce this. Can you tell me what your full config is? Thanks.

CorradoLanera commented 1 year ago

@sjmgarnier here below some system information within my running environment. There are other things that can help you?

options(golem.app.prod = FALSE)
golem::detach_all_attached()
golem::document_and_reload(
  pkg = "~/ubesp/iobed.shiny/",
  export_all = TRUE,
  helpers = TRUE,
  attach_testthat = TRUE
)
#> ℹ Loading iobed.shiny
#> Warning: replacing previous import 'shiny::runExample' by 'shinyjs::runExample'
#> when loading 'iobed.shiny'
#> ℹ Loading iobed.shiny
#> Warning: replacing previous import 'shiny::runExample' by 'shinyjs::runExample'
#> when loading 'iobed.shiny'

devtools::session_info(
  info = "all",
  dependencies = TRUE,
  include_base = TRUE
)
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.0 (2022-04-22 ucrt)
#>  os       Windows 10 x64 (build 22621)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United States.utf8
#>  ctype    English_United States.utf8
#>  tz       Europe/Berlin
#>  date     2022-09-07
#>  pandoc   2.18 @ C:/Program Files/RStudio/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  !  package        * version date (UTC) lib source
#>     attempt          0.3.1   2020-05-03 [1] RSPM (R 4.2.0)
#>     base           * 4.2.0   2022-04-22 [?] local
#>     brio             1.1.3   2021-11-30 [1] RSPM (R 4.2.0)
#>     cachem           1.0.6   2021-08-19 [1] RSPM (R 4.2.0)
#>     callr            3.7.2   2022-08-22 [1] RSPM (R 4.2.0)
#>     cli              3.3.0   2022-04-25 [1] CRAN (R 4.2.0)
#>     codetools        0.2-18  2020-11-04 [2] CRAN (R 4.2.0)
#>     commonmark       1.8.0   2022-03-09 [1] RSPM (R 4.2.0)
#>  P  compiler         4.2.0   2022-04-22 [2] local
#>     config           0.3.1   2020-12-17 [1] RSPM (R 4.2.0)
#>     crayon           1.5.1   2022-03-26 [1] RSPM (R 4.2.0)
#>  P  datasets       * 4.2.0   2022-04-22 [2] local
#>     desc             1.4.1   2022-03-06 [1] RSPM (R 4.2.0)
#>     devtools         2.4.3   2021-11-30 [1] RSPM (R 4.2.0)
#>     digest           0.6.29  2021-12-01 [1] RSPM (R 4.2.0)
#>     DT               0.23    2022-05-10 [1] RSPM (R 4.2.0)
#>     ellipsis         0.3.2   2021-04-29 [1] RSPM (R 4.2.0)
#>     evaluate         0.16    2022-08-09 [1] CRAN (R 4.2.1)
#>     fansi            1.0.3   2022-03-24 [1] RSPM (R 4.2.0)
#>     fastmap          1.1.0   2021-01-25 [1] RSPM (R 4.2.0)
#>     fs               1.5.2   2021-12-08 [1] RSPM (R 4.2.0)
#>     future           1.25.0  2022-04-24 [1] RSPM (R 4.2.0)
#>     globals          0.15.0  2022-05-09 [1] RSPM (R 4.2.0)
#>     glue             1.6.2   2022-02-24 [1] RSPM (R 4.2.0)
#>     golem            0.3.2   2022-03-04 [1] RSPM (R 4.2.0)
#>  P  graphics       * 4.2.0   2022-04-22 [2] local
#>  P  grDevices      * 4.2.0   2022-04-22 [2] local
#>     here             1.0.1   2020-12-13 [1] RSPM (R 4.2.0)
#>     highr            0.9     2021-04-16 [1] RSPM (R 4.2.0)
#>     hms              1.1.1   2021-09-26 [1] RSPM (R 4.2.0)
#>     htmltools        0.5.3   2022-07-18 [1] CRAN (R 4.2.1)
#>     htmlwidgets      1.5.4   2021-09-08 [1] RSPM (R 4.2.0)
#>     httpuv           1.6.5   2022-01-05 [1] RSPM (R 4.2.0)
#>     iobed.bed        0.2.1   2022-09-05 [1] local
#>  VP iobed.shiny    * 0.2.4   2022-09-05 [?] load_all() (on disk 0.2.3.9000)
#>     iobed.video      0.1.0   2022-09-05 [1] local
#>     iobed.watch      0.2.0   2022-09-05 [1] local
#>     knitr            1.40    2022-08-24 [1] CRAN (R 4.2.1)
#>     later            1.3.0   2021-08-18 [1] RSPM (R 4.2.0)
#>     lifecycle        1.0.1   2021-09-24 [1] RSPM (R 4.2.0)
#>     listenv          0.8.0   2019-12-05 [1] RSPM (R 4.2.0)
#>     magrittr         2.0.3   2022-03-30 [1] RSPM (R 4.2.0)
#>     memoise          2.0.1   2021-11-26 [1] RSPM (R 4.2.0)
#>  P  methods        * 4.2.0   2022-04-22 [2] local
#>     mime             0.12    2021-09-28 [1] RSPM (R 4.2.0)
#>  P  parallel         4.2.0   2022-04-22 [2] local
#>     parallelly       1.31.1  2022-04-22 [1] RSPM (R 4.2.0)
#>     pbapply          1.5-0   2021-09-16 [1] RSPM (R 4.2.0)
#>     pillar           1.7.0   2022-02-01 [1] RSPM (R 4.2.0)
#>     pkgbuild         1.3.1   2021-12-20 [1] RSPM (R 4.2.0)
#>     pkgconfig        2.0.3   2019-09-22 [1] RSPM (R 4.2.0)
#>     pkgload          1.2.4   2021-11-30 [1] RSPM (R 4.2.0)
#>     prettyunits      1.1.1   2020-01-24 [1] RSPM (R 4.2.0)
#>     processx         3.7.0   2022-07-07 [1] CRAN (R 4.2.1)
#>     promises         1.2.0.1 2021-02-11 [1] RSPM (R 4.2.0)
#>     ps               1.7.1   2022-06-18 [1] CRAN (R 4.2.1)
#>     purrr            0.3.4   2020-04-17 [1] RSPM (R 4.2.0)
#>     R.cache          0.15.0  2021-04-30 [1] RSPM (R 4.2.0)
#>     R.methodsS3      1.8.1   2020-08-26 [1] RSPM (R 4.2.0)
#>     R.oo             1.24.0  2020-08-26 [1] RSPM (R 4.2.0)
#>     R.utils          2.11.0  2021-09-26 [1] RSPM (R 4.2.0)
#>     R6               2.5.1   2021-08-19 [1] RSPM (R 4.2.0)
#>     Rcpp             1.0.9   2022-07-08 [1] RSPM (R 4.2.0)
#>     readr            2.1.2   2022-01-30 [1] RSPM (R 4.2.0)
#>     remotes          2.4.2   2021-11-30 [1] RSPM (R 4.2.0)
#>     reprex           2.0.1   2021-08-05 [1] RSPM (R 4.2.0)
#>     rlang            1.0.5   2022-08-31 [1] CRAN (R 4.2.1)
#>     rmarkdown        2.16    2022-08-24 [1] CRAN (R 4.2.1)
#>     ROpenCVLite      4.60.2  2022-09-05 [1] Github (swarm-lab/ROpenCVLite@62e5780)
#>     roxygen2         7.2.1   2022-07-18 [1] RSPM (R 4.2.0)
#>     rprojroot        2.0.3   2022-04-02 [1] RSPM (R 4.2.0)
#>     rstudioapi       0.13    2020-11-12 [1] RSPM (R 4.2.0)
#>     Rvision          0.6.2   2022-09-06 [1] Github (swarm-lab/Rvision@772d8ed)
#>     serial           3.0     2020-05-14 [1] RSPM (R 4.2.0)
#>     sessioninfo      1.2.2   2021-12-06 [1] RSPM (R 4.2.0)
#>     shiny            1.7.1   2021-10-02 [1] RSPM (R 4.2.0)
#>     shinydashboard   0.7.2   2021-09-30 [1] RSPM (R 4.2.0)
#>     shinyjs          2.1.0   2021-12-23 [1] RSPM (R 4.2.0)
#>  P  stats          * 4.2.0   2022-04-22 [2] local
#>     stringi          1.7.8   2022-07-11 [1] CRAN (R 4.2.1)
#>     stringr          1.4.1   2022-08-20 [1] RSPM (R 4.2.0)
#>     styler           1.7.0   2022-03-13 [1] RSPM (R 4.2.0)
#>  P  tcltk            4.2.0   2022-04-22 [2] local
#>     testthat       * 3.1.4   2022-04-26 [1] RSPM (R 4.2.0)
#>     tibble           3.1.7   2022-05-03 [1] RSPM (R 4.2.0)
#>  P  tools            4.2.0   2022-04-22 [2] local
#>     tzdb             0.3.0   2022-03-28 [1] RSPM (R 4.2.0)
#>     usethis          2.1.6   2022-05-25 [1] RSPM (R 4.2.0)
#>     utf8             1.2.2   2021-07-24 [1] RSPM (R 4.2.0)
#>  P  utils          * 4.2.0   2022-04-22 [2] local
#>     vctrs            0.4.1   2022-04-13 [1] RSPM (R 4.2.0)
#>     withr            2.5.0   2022-03-03 [1] RSPM (R 4.2.0)
#>     xfun             0.32    2022-08-10 [1] CRAN (R 4.2.1)
#>     xml2             1.3.3   2021-11-30 [1] RSPM (R 4.2.0)
#>     xtable           1.8-4   2019-04-21 [1] RSPM (R 4.2.0)
#>     yaml             2.3.5   2022-02-21 [1] RSPM (R 4.2.0)
#> 
#>  [1] C:/Users/corra/AppData/Local/R/win-library/4.2
#>  [2] C:/Program Files/R/R-4.2.0/library
#> 
#>  V ── Loaded and on-disk version mismatch.
#>  P ── Loaded and on-disk path mismatch.
#> 
#> ─ External software ──────────────────────────────────────────────────────────
#>  setting        value
#>  cairo          1.16.0
#>  cairoFT
#>  pango
#>  png            1.6.37
#>  jpeg           9.5
#>  tiff           LIBTIFF, Version 4.3.0
#>  tcl            8.6.12
#>  curl           7.81.0
#>  zlib           1.2.11
#>  bzlib          1.0.8, 13-Jul-2019
#>  xz             5.2.5
#>  PCRE           10.37 2021-05-26
#>  ICU            70.1
#>  TRE            TRE 0.8.0 R_fixes (BSD)
#>  iconv          win_iconv
#>  readline
#>  BLAS
#>  lapack
#>  lapack_version 3.10.0
#> 
#> ─ Python configuration ───────────────────────────────────────────────────────
#>  Python is not available
#> 
#> ──────────────────────────────────────────────────────────────────────────────
ROpenCVLite::opencvVersion()
#> [1] "Version 4.6.0"
ROpenCVLite::opencvConfig()
#> -L"C:\Users\corra\AppData\Local\R\win-library\4.2\ROpenCV\x64\mingw\lib" -lopencv_calib3d460 -lopencv_core460 -lopencv_dnn460 -lopencv_features2d460 -lopencv_flann460 -lopencv_gapi460 -lopencv_highgui460 -lopencv_imgcodecs460 -lopencv_imgproc460 -lopencv_ml460 -lopencv_objdetect460 -lopencv_photo460 -lopencv_stitching460 -lopencv_video460 -lopencv_videoio460

Created on 2022-09-07 by the reprex package (v2.0.1)

You can also find the application source code here, and the lines which make R crashes (only on my laptop, only for wrong indexes, e.g. 0... that should be the default, but with index = 1 I correctly get the internal camera snapshot!) here

Thank you! Corrado.