Around Farrowing

library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(lmerTest)
Warning: package 'lmerTest' was built under R version 4.4.3
Loading required package: lme4
Warning: package 'lme4' was built under R version 4.4.3
Loading required package: Matrix

Attaching package: 'lmerTest'
The following object is masked from 'package:lme4':

    lmer
The following object is masked from 'package:stats':

    step
library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.4.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats   1.0.0     ✔ readr     2.1.5
✔ ggplot2   3.5.1     ✔ stringr   1.5.1
✔ lubridate 1.9.3     ✔ tibble    3.2.1
✔ purrr     1.0.2     ✔ tidyr     1.3.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ tidyr::expand() masks Matrix::expand()
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
✖ tidyr::pack()   masks Matrix::pack()
✖ tidyr::unpack() masks Matrix::unpack()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(modelr)
library(purrr)
library(emmeans)
Warning: package 'emmeans' was built under R version 4.4.3
Welcome to emmeans.
Caution: You lose important information if you filter this package's results.
See '? untidy'
library(gridExtra)
Warning: package 'gridExtra' was built under R version 4.4.3

Attaching package: 'gridExtra'

The following object is masked from 'package:dplyr':

    combine
library(writexl)
Warning: package 'writexl' was built under R version 4.4.3
library(gt)
Warning: package 'gt' was built under R version 4.4.3
library(webshot2)
Warning: package 'webshot2' was built under R version 4.4.3
library(broom.mixed)
library(ggplot2)
library(isotree)
Warning: package 'isotree' was built under R version 4.4.3
library(tidyr)
library(ggforce)
Warning: package 'ggforce' was built under R version 4.4.3
library(plotrix)
Warning: package 'plotrix' was built under R version 4.4.3
library(lubridate)
library(KFAS)
Warning: package 'KFAS' was built under R version 4.4.3
Please cite KFAS in publications by using: 

  Jouni Helske (2017). KFAS: Exponential Family State Space Models in R. Journal of Statistical Software, 78(10), 1-39. doi:10.18637/jss.v078.i10.
library(zoo)

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric
library(ggforce)
library(dplyr)
library(ggplot2)
first <- read.csv("Z:/Isaac/Piglet_Detection/Around Farrowing/Cropped/Results/Sow_1_1st_detection_metrics.csv")
many <- read.csv("Z:/Isaac/Piglet_Detection/Around Farrowing/Cropped/Results/Sow_1_Many_detection_metrics.csv")
boxplot(first$area)

plot(first$area)

plot(first$centroid_x,first$centroid_y)

boxplot(first$width)

boxplot(many$area)

plot(many$area)

plot(many$centroid_x,many$centroid_y)

boxplot(many$width)

library(dplyr)
library(ggplot2)

many %>% 
  filter(score > 0.3) %>% 
  # filter(area < 15000) %>%
  # filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() +
    labs(title = "Object Detection Bounding Boxes")

many %>% 
  filter(score > 0.3) %>% 
  filter(area < 15000) %>%
  # filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() +
    labs(title = "Object Detection Bounding Boxes")

many %>% 
  filter(score > 0.3) %>% 
  filter(area < 15000) %>%
  filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() +
    labs(title = "Object Detection Bounding Boxes")

first %>% 
  filter(score > 0.2) %>%
  # filter(area < 15000) %>%
  # filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() +
    labs(title = "Object Detection Bounding Boxes")

first %>% 
  filter(score > 0.2) %>% 
  filter(area < 15000) %>%
  # filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() +
    labs(title = "Object Detection Bounding Boxes")

first %>% 
  filter(score > 0.2) %>% 
  filter(area < 15000) %>%
  filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() +
    labs(title = "Object Detection Bounding Boxes")

save(first,many,file="Z:/Isaac/Piglet_Detection/Around Farrowing/Cropped/Results/around_farrowing.RData")

First 115-135-155

first$image<- as.numeric(str_sub(first$image, end = -5))
first_before <- first %>% filter(image<135)
first_birth <- first %>% filter(image==135)
first_after <- first %>% filter(image>135)

Many 22-42-62

many$image<- as.numeric(str_sub(many$image, end = -5))
many_before <- many %>% filter(image<42)
many_birth <- many %>% filter(image==42)
many_after <- many %>% filter(image>42)
fb1 <- first_before %>% 
  filter(score > 0.2) %>% 
  # filter(area < 15000) %>%
  # filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark()

fb2 <- first_before %>% 
  filter(score > 0.2) %>% 
  filter(area < 15000) %>%
  # filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() 

fb3 <- first_before %>% 
  filter(score > 0.2) %>% 
  filter(area < 15000) %>%
  filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() 
fbi1 <- first_birth %>% 
  filter(score > 0.2) %>% 
  # filter(area < 15000) %>%
  # filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark()

fbi2 <- first_birth %>% 
  filter(score > 0.2) %>% 
  filter(area < 15000) %>%
  # filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() 

fbi3 <- first_birth %>% 
  filter(score > 0.2) %>% 
  filter(area < 15000) %>%
  filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark()
fa1 <- first_after %>% 
  filter(score > 0.2) %>% 
  # filter(area < 15000) %>%
  # filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() 

fa2 <- first_after %>% 
  filter(score > 0.2) %>% 
  filter(area < 15000) %>%
  # filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() 

fa3 <- first_after %>% 
  filter(score > 0.2) %>% 
  filter(area < 15000) %>%
  filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() 
mb1 <- many_before %>% 
  filter(score > 0.2) %>% 
  # filter(area < 15000) %>%
  # filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() 

mb2 <- many_before %>% 
  filter(score > 0.2) %>% 
  filter(area < 15000) %>%
  # filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() 

mb3 <- many_before %>% 
  filter(score > 0.2) %>% 
  filter(area < 15000) %>%
  filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() 
mbi1 <- many_birth %>% 
  filter(score > 0.2) %>% 
  # filter(area < 15000) %>%
  # filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() 

mbi2 <- many_birth %>% 
  filter(score > 0.2) %>% 
  filter(area < 15000) %>%
  # filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() 

mbi3 <- many_birth %>% 
  filter(score > 0.2) %>% 
  filter(area < 15000) %>%
  filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() 
ma1 <- many_after %>% 
  filter(score > 0.2) %>% 
  # filter(area < 15000) %>%
  # filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() 

ma2 <- many_after %>% 
  filter(score > 0.2) %>% 
  filter(area < 15000) %>%
  # filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark()

ma3 <- many_after %>% 
  filter(score > 0.2) %>% 
  filter(area < 15000) %>%
  filter(x1 > 100) %>% # 1. Filter the data first
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) + # 2. Start ggplot
    geom_rect(fill = NA, color = "red") + # 3. Add layers with +
    theme_dark() 
library(cowplot)
Warning: package 'cowplot' was built under R version 4.4.3

Attaching package: 'cowplot'
The following object is masked from 'package:gt':

    as_gtable
The following object is masked from 'package:lubridate':

    stamp
plot_grid(fb1,fbi1,fa1,mb1,mbi1,ma1,ncol=2)

plot_grid(fb2,fbi2,fa2,mb2,mbi2,ma2,ncol=2)

plot_grid(fb3,fbi3,fa3,mb3,mbi3,ma3,ncol=2)

library(dplyr)
library(ggplot2)

# Combine everything and add 'type' and 'stage' tags
all_data <- bind_rows(
  mutate(first_before, type = "First", stage = "Before"),
  mutate(many_before,  type = "Many",  stage = "Before"),
  mutate(first_birth,  type = "First", stage = "Birth"),
  mutate(many_birth,   type = "Many",  stage = "Birth"),
  mutate(first_after,  type = "First", stage = "After"),
  mutate(many_after,   type = "Many",  stage = "After")
)

# One single ggplot call for the entire 2x3 panel
all_data %>%
  filter(score > 0.2#, 
         # area < 15000, 
         # x1>100
         )%>%
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) +
  geom_rect(fill = NA, color = "red") +
  facet_grid(stage ~ type) + # Rows = stage, Columns = type
  theme_dark() +
  labs(title = "Detection Panel")

all_data %>%
  filter(score > 0.2, 
         area < 15000#, 
         # x1>100
         ) %>%
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) +
  geom_rect(fill = NA, color = "red") +
  facet_grid(stage ~ type) + # Rows = stage, Columns = type
  theme_dark() +
  labs(title = "Detection Panel")

all_data %>%
  filter(score > 0.2, 
         area < 15000, 
         x1>100) %>%
  ggplot(aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2)) +
  geom_rect(fill = NA, color = "red") +
  facet_grid(stage ~ type) + # Rows = stage, Columns = type
  theme_dark() +
  labs(title = "Detection Panel")