rasenior / ThermStats

Calculate statistics for thermal images in R
Other
3 stars 0 forks source link

Unable to proceed if metadata id not a data.frame class and filename is not coercible to numeric #9

Open ScymnusRIP opened 2 years ago

ScymnusRIP commented 2 years ago

Sorry, if i'm too verbose, but this is my first issue writing and i've work on it for a solution for a couple of days. Flir equipment provide files named as typical concatenation code like: FLIR_yyyymmdd_hhmmss_3digitmillisecond.jpg In order to facilitate picture classification, i'll export file list into an excel file that anybody could edit and than re import.

 folder_input<-choose.dir(default = "my/path", caption = "Select INPUT folder")
folder_output<-choose.dir(default = folder_input, caption = "Select OUTPUT folder")
flir_file_list<-list.files(path = folder_input, pattern = "^FLIR_.*\\.jpg$", all.files = FALSE, full.names = FALSE, recursive = FALSE, ignore.case = FALSE) 

flir_file_list2<-data.frame(spatial_rep=seq(1,length(flir_file_list),1), 
            direction=seq(1,length(flir_file_list),1), 
            camera_rep=seq(1,length(flir_file_list),1), 
            photo_no=seq(1,length(flir_file_list), 1),
            photo_name=flir_file_list,
            ambient_temp=rep(29.6,length(flir_file_list)), 
            ambient_humidity=rep(34.0,length(flir_file_list)),
            object_distance=c(rep(1.2,length(flir_file_list)/2),rep(1.5,length(flir_file_list)/2)) )

FLIR_file_list<-createWorkbook()
addWorksheet(FLIR_file_list, "FLIR_file")
writeData(FLIR_file_list, sheet = "FLIR_file", x = flir_file_list2)
saveWorkbook(FLIR_file_list, file=paste0(folder_output,"//FLIR_fileLONG_NAME.xlsx"), overwrite=TRUE)

flir_file_list2 <- read_excel(paste0(folder_output,"//FLIR_fileLONG_NAME.xlsx"))
flir_rawPICS<-batch_extract(in_dir=folder_input,
                        write_results=TRUE,
                        file_name = paste0("flir_converted_",Sys.Date()),
                        inc=flir_file_list,
                        out_dir=folder_output,
                        exiftoolpath="Path/to/Exiftools/")      

flir_converted<-batch_convert(flir_rawPICS$raw_dat,
  E = 0.98,
  OD = flir_file_list2$object_distance[PIC_index],
  RTemp = flir_file_list2$ambient_temp[PIC_index],
  ATemp = flir_file_list2$ambient_temp[PIC_index],
  IRWTemp = flir_file_list2$ambient_temp[PIC_index],
  IRT = 1,
  RH = flir_file_list2$ambient_humidity[PIC_index],
  PR1 = flir_rawPICS$camera_params$PlanckR1,
  PB = flir_rawPICS$camera_params$PlanckB,
  PF = flir_rawPICS$camera_params$PlanckF,
  PO = flir_rawPICS$camera_params$PlanckO,
  PR2 = flir_rawPICS$camera_params$PlanckR2,
  write_results = TRUE,
  out_dir = folder_output,
  file_name = paste0("flir_converted_",Sys.Date())
)

averageFLIR<-average_by_group(metadata = flir_file_list2,
                              img_list =flir_converted,                 
                              id = "photo_name",
                              grouping_var = "spatial_rep",
                              round_val = 4) 

Here first Issue: averageFLIR is empty probably due to file name and photo name do not correspond. batch_extract delete "FLIR" and ".jpg" extension. Workaround for this issue

' flir_file_list2<-as.data.frame(flir_file_list2)
flir_file_list2$photo_name<-substr(flir_file_list2$photo_name,5,24)

flir_converted<-batch_convert(flir_rawPICS$raw_dat,
  E = 0.98,
  OD = flir_file_list2$object_distance[PIC_index],
  RTemp = flir_file_list2$ambient_temp[PIC_index],
  ATemp = flir_file_list2$ambient_temp[PIC_index],
  IRWTemp = flir_file_list2$ambient_temp[PIC_index],
  IRT = 1,
  RH = flir_file_list2$ambient_humidity[PIC_index],
  PR1 = flir_rawPICS$camera_params$PlanckR1,
  PB = flir_rawPICS$camera_params$PlanckB,
  PF = flir_rawPICS$camera_params$PlanckF,
  PO = flir_rawPICS$camera_params$PlanckO,
  PR2 = flir_rawPICS$camera_params$PlanckR2,
  write_results = TRUE,
  out_dir = folder_output,
  file_name = paste0("flir_converted_",Sys.Date())
)

averageFLIR<-average_by_group(metadata = flir_file_list2,
                              img_list =flir_converted,                 
                              id = "photo_name",
                              grouping_var = "spatial_rep",
                              round_val = 4)`

After this could proceed coercing matrix list to raster stack stackFLIR<-stack_imgs(flir_converted) ..and averaging raster stack

`averageFLIRstack<-average_by_group(metadata = flir_file_list2,
                              img_list =stackFLIR,
                              id = "photo_name",
                              grouping_var = "spatial_rep",
                              round_val = 4)`

Second Issue: "averageFLIRstack" is empty and were generated sob error "Error in x[[1]] : subscript out of bounds" I've found that in vignette, provided example show that metadata$photo_no is an integer class, while i'm not able to coerce to integer file name. Even if i'll will guess how to do it, then filename is no longer equal to photo_name in flir_file_list2 Workaround for this issue Renaming all files with a short number names (like collapsing time parts in 9characters long filename) and restarting over the whole process. It run successifully

`flir_file_list2 <- read_excel(paste0(folder_output,"//FLIR_fileNUMBER_NAME.xlsx"))
flir_file_list2<-as.data.frame(flir_file_list2)

flir_rawPICS<-batch_extract(in_dir=folder_input,
                        write_results=TRUE,
                        file_name = paste0("flir_converted_",Sys.Date()),
                        inc=flir_file_list,
                        out_dir=folder_output,
                        exiftoolpath="D:/AgriSearchInnovations/Comuni/termocamera/Exiftools/")

flir_converted<-batch_convert(flir_rawPICS$raw_dat,
  E = 0.98,
  OD = flir_file_list2$object_distance[PIC_index],
  RTemp = flir_file_list2$ambient_temp[PIC_index],
  ATemp = flir_file_list2$ambient_temp[PIC_index],
  IRWTemp = flir_file_list2$ambient_temp[PIC_index],
  IRT = 1,
  RH = flir_file_list2$ambient_humidity[PIC_index],
  PR1 = flir_rawPICS$camera_params$PlanckR1,
  PB = flir_rawPICS$camera_params$PlanckB,
  PF = flir_rawPICS$camera_params$PlanckF,
  PO = flir_rawPICS$camera_params$PlanckO,
  PR2 = flir_rawPICS$camera_params$PlanckR2,
  write_results = TRUE,
  out_dir = folder_output,
  file_name = paste0("flir_converted_",Sys.Date())
)`
In order to eliminate residual "." and convert into integer field
`flir_file_list2$photo_name<-paste0(substr(flir_file_list2$photo_name,1,9))                             
flir_file_list2$photo_name<-as.numeric(flir_file_list2$photo_name)
flir_file_list2$photo_name<-as.integer(flir_file_list2$photo_name)
averageFLIR<-average_by_group(metadata = flir_file_list2,
                              img_list =flir_converted,                 
                              id = "photo_name",
                              grouping_var = "spatial_rep",
                              round_val = 4)
stackFLIR<-stack_imgs(flir_converted)
averageFLIRstack<-average_by_group(metadata = flir_file_list2,
                              img_list =stackFLIR,
                              id = "photo_name",
                              grouping_var = "spatial_rep",
                              round_val = 4)

raster::plot(averageFLIRstack)

` Output graph is here attached Is possible to generally work around these issue keeping indipendentely from coercing filename to integer??

raster plot averageFLIRstack FLIR_fileNUMBER_NAME.xlsx FLIR_fileLONG_NAME.xlsx FLIR_20220711_025752_369 FLIR_20220711_024843_161 FLIR_20220711_024859_911 FLIR_20220711_024905_792 FLIR_20220711_024910_046 FLIR_20220711_025005_865 FLIR_20220711_025012_785 FLIR_20220711_025729_424 FLIR_20220711_025736_327 FLIR_20220711_025739_925 FLIR_20220711_025743_586 FLIR_20220711_025747_357