Computer Vision Test for Casa Grande

Alex Kozela - Originally written by Bon Woo Koo & Subhro Guhathakurta; modified by Uijeong Hwang

2024-11-4

library(tidyverse)
library(magrittr)
library(osmdata)
library(sfnetworks)
library(units)
library(sf)
library(tidygraph)
library(tmap)
library(here)
ttm()

We are going to do quick analyses using the semantic segmentation model results.

Merge the data processed from Colab back to R.

If you are having trouble running the Colab script, you may use these files: geojson and csv.

# Load the sampled points.
edges_azi <- st_read("/Users/alexkozela/Desktop/R Fall 2024/Street View and Computer Vision/edges_azi_casa_grande.geojson") # path to your "edges_azi_{city_name}.geojson" file
## Reading layer `edges_azi_casa_grande' from data source 
##   `/Users/alexkozela/Desktop/R Fall 2024/Street View and Computer Vision/edges_azi_casa_grande.geojson' 
##   using driver `GeoJSON'
## Simple feature collection with 14068 features and 9 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -111.9362 ymin: 32.76382 xmax: -111.5686 ymax: 33.03604
## Geodetic CRS:  WGS 84
# Download the output from the computer vision models.
seg_output <- read.csv("/Users/alexkozela/Desktop/R Fall 2024/Street View and Computer Vision/seg_output.csv") # path to your "seg_output.csv" file

# Join them back to the `edges_azi` object that was used to download images.
edges_seg_output <- edges_azi %>% 
  inner_join(seg_output, by=c("node_id"="img_id"))

The numeric values in each object column represent the number of pixels in images predicted as that object. Check out the first few rows and see which objects comprise the majority of streetscapes.

head(edges_seg_output)
## Simple feature collection with 6 features and 159 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -111.912 ymin: 32.82126 xmax: -111.7285 ymax: 32.92935
## Geodetic CRS:  WGS 84
##    type         X        Y         azi   osm_id      highway    length edge_id
## 1   end -111.9120 32.87939   0.8520503 16899542  residential  984.0128       3
## 2   end -111.7660 32.87902 183.3210644 16899636 unclassified  766.1236       4
## 3  mid1 -111.7332 32.92935 -91.0585797 16900822  residential  399.1226       9
## 4 start -111.7285 32.82126 115.1155569 16901804  residential  272.7187      14
## 5   end -111.8434 32.87208  89.9190373 16902441  residential 1612.6224      17
## 6 start -111.7766 32.88137 179.9170428 16910136  residential  220.3332      39
##   node_id wall building    sky floor  tree ceiling   road bed windowpane grass
## 1      12    0        0 203527     0     0       0 158547   0          0     0
## 2      16    0     4023 200917     0    99       0 126854   0          0     0
## 3      34    0    17240 183372     0 10344       0 173887   0          0   416
## 4      53    0        0 179269     0  4199       0 133528   0          0    25
## 5      68    0        0 198680     0 17605       0      0   0          0     0
## 6     153 2796      512 188870     0 22342       0 173698   0          0  3468
##   cabinet sidewalk person  earth door table mountain plant curtain chair   car
## 1       0        0      0  47366    0     0       15     0       0     0     0
## 2       0        0     90  47411    0     0      135     0       0     0  2303
## 3       0     7757      0    627    0     0     3667  1352       0     0 10896
## 4       0        0      0  75183    0     0    15321     0       0     0     0
## 5       0        0      0 187164    0     0        0     0       0     0     0
## 6       0        0      0  16482    0     0        0   300       0     0     0
##   water painting sofa shelf house sea mirror rug field armchair seat fence desk
## 1     0        0    0     0     0   0      0   0     0        0    0   130    0
## 2     0        0    0     0     0   0      0   0     0        0    0 27606    0
## 3     0        0    0     0     0   0      0   0     0        0    0    42    0
## 4     0        0    0     0     0   0      0   0     0        0    0     0    0
## 5     0        0    0     0     0   0      0   0  5257        0    0     0    0
## 6     0        0    0     0     0   0      0   0   928        0    0   204    0
##   rock wardrobe lamp bathtub railing cushion base box column signboard
## 1    0        0    0       0       0       0    0   0      0         0
## 2    0        0    0       0       0       0    0   0      0         0
## 3    0        0    0       0       0       0    0   0      0         0
## 4    0        0    0       0       0       0    0   0      0         0
## 5    0        0    0       0       0       0    0   0      0       894
## 6    0        0    0       0       0       0    0   0      0         0
##   chest.of.drawers counter sand sink skyscraper fireplace refrigerator
## 1                0       0    0    0          0         0            0
## 2                0       0    0    0          0         0            0
## 3                0       0    0    0          0         0            0
## 4                0       0    0    0          0         0            0
## 5                0       0    0    0          0         0            0
## 6                0       0    0    0          0         0            0
##   grandstand path stairs runway case pool.table pillow screen.door stairway
## 1          0    0      0      0    0          0      0           0        0
## 2          0    0      0      0    0          0      0           0        0
## 3          0    0      0      0    0          0      0           0        0
## 4          0    0      0      0    0          0      0           0        0
## 5          0    0      0      0    0          0      0           0        0
## 6          0    0      0      0    0          0      0           0        0
##   river bridge bookcase blind coffee.table toilet flower book hill bench
## 1     0     14        0     0            0      0      0    0    0     0
## 2     0      0        0     0            0      0      0    0    0     0
## 3     0      0        0     0            0      0      0    0    0     0
## 4     0      0        0     0            0      0      0    0 2075     0
## 5     0      0        0     0            0      0      0    0    0     0
## 6     0      0        0     0            0      0      0    0    0     0
##   countertop stove palm kitchen.island computer swivel.chair boat bar
## 1          0     0    0              0        0            0    0   0
## 2          0     0    0              0        0            0    0   0
## 3          0     0    0              0        0            0    0   0
## 4          0     0    0              0        0            0    0   0
## 5          0     0    0              0        0            0    0   0
## 6          0     0    0              0        0            0    0   0
##   arcade.machine hovel bus towel light truck tower chandelier awning
## 1              0     0   0     0     0     0     0          0      0
## 2              0     0   0     0     0     0     0          0      0
## 3              0     0   0     0     0     0     0          0      0
## 4              0     0   0     0     0     0     0          0      0
## 5              0     0   0     0     0     0     0          0      0
## 6              0     0   0     0     0     0     0          0      0
##   streetlight booth television.receiver airplane dirt.track apparel pole land
## 1           0     0                   0        0          0       0    1    0
## 2           0     0                   0        0          0       0  162    0
## 3           0     0                   0        0          0       0    0    0
## 4           0     0                   0        0          0       0    0    0
## 5           0     0                   0        0          0       0    0    0
## 6           0     0                   0        0          0       0    0    0
##   bannister escalator ottoman bottle buffet poster stage van ship fountain
## 1         0         0       0      0      0      0     0   0    0        0
## 2         0         0       0      0      0      0     0   0    0        0
## 3         0         0       0      0      0      0     0   0    0        0
## 4         0         0       0      0      0      0     0   0    0        0
## 5         0         0       0      0      0      0     0   0    0        0
## 6         0         0       0      0      0      0     0   0    0        0
##   conveyer.belt canopy washer plaything swimming.pool stool barrel basket
## 1             0      0      0         0             0     0      0      0
## 2             0      0      0         0             0     0      0      0
## 3             0      0      0         0             0     0      0      0
## 4             0      0      0         0             0     0      0      0
## 5             0      0      0         0             0     0      0      0
## 6             0      0      0         0             0     0      0      0
##   waterfall tent bag minibike cradle oven ball food step tank trade.name
## 1         0    0   0        0      0    0    0    0    0    0          0
## 2         0    0   0        0      0    0    0    0    0    0          0
## 3         0    0   0        0      0    0    0    0    0    0          0
## 4         0    0   0        0      0    0    0    0    0    0          0
## 5         0    0   0        0      0    0    0    0    0    0          0
## 6         0    0   0        0      0    0    0    0    0    0          0
##   microwave pot animal bicycle lake dishwasher screen blanket sculpture hood
## 1         0   0      0       0    0          0      0       0         0    0
## 2         0   0      0       0    0          0      0       0         0    0
## 3         0   0      0       0    0          0      0       0         0    0
## 4         0   0      0       0    0          0      0       0         0    0
## 5         0   0      0       0    0          0      0       0         0    0
## 6         0   0      0       0    0          0      0       0         0    0
##   sconce vase traffic.light tray ashcan fan pier crt.screen plate monitor
## 1      0    0             0    0      0   0    0          0     0       0
## 2      0    0             0    0      0   0    0          0     0       0
## 3      0    0             0    0      0   0    0          0     0       0
## 4      0    0             0    0      0   0    0          0     0       0
## 5      0    0             0    0      0   0    0          0     0       0
## 6      0    0             0    0      0   0    0          0     0       0
##   bulletin.board shower radiator glass clock flag                   geometry
## 1              0      0        0     0     0    0  POINT (-111.912 32.87939)
## 2              0      0        0     0     0    0  POINT (-111.766 32.87902)
## 3              0      0        0     0     0    0 POINT (-111.7332 32.92935)
## 4              0      0        0     0     0    0 POINT (-111.7285 32.82126)
## 5              0      0        0     0     0    0 POINT (-111.8434 32.87208)
## 6              0      0        0     0     0    0 POINT (-111.7766 32.88137)

Calculate Greenness

Let’s define greenness as the sum of tree, grass, and plant.

edges_seg_output %<>% # `a %<>%` is the same as `a <- a %>%`; you need to import `magrittr` to use this pipe
  mutate(greenness = tree + grass + plant,
         pct_greenness = greenness/(640*640))

tmap_mode("view")
## tmap mode set to interactive viewing
# Map!
t1 <- tm_basemap("OpenStreetMap")+
  tm_shape(edges_seg_output) + 
  tm_dots(col = "pct_greenness", style="quantile", palette = 'viridis')

t2 <- tm_basemap(leaflet::providers$Esri.WorldImagery) +
  tm_shape(edges_seg_output %>% st_bbox() %>% st_as_sfc()) + 
  tm_borders(lwd = 2, col = 'white') 

tmap_arrange(t1, t2, sync = T)

Calculate Building-to-street Ratio

Let’s calculate building-to-street ratio. Major ‘building’ objects include building and house. Major ‘street’ objects include road, sidewalk, and car.

edges_seg_output %<>% 
  mutate(
    b2s_ratio = (building + house) / (road + sidewalk + car),
    b2s_ratio = case_when(
      b2s_ratio >= 1 ~ 1, # Let's set the upper limit as 1.
    TRUE ~ b2s_ratio))

t1 <- tm_basemap("OpenStreetMap")+
  tm_shape(edges_seg_output) + 
  tm_dots(col = "b2s_ratio", style="quantile", palette = 'viridis')

t2 <- tm_basemap(leaflet::providers$Esri.WorldImagery) +
  tm_shape(edges_seg_output %>% st_bbox() %>% st_as_sfc()) + 
  tm_borders(lwd = 2, col = 'white')

tmap_arrange(t1, t2, sync = T)

Violin plots comparing the segmentation results by type of road

Are there significant differences between road types?

edges_seg_output %<>%
  filter(!is.na(highway)) %>% 
  mutate(highway = factor(highway, levels = c('unclassified','residential','tertiary',
                                              'secondary','primary','trunk'))) %>%
  mutate(pct_building = building/(640*640),
         pct_sky = sky/(640*640),
         pct_road = road/(640*640),
         pct_sidewalk = sidewalk/(640*640))

edges_seg_output %>% 
  pivot_longer(cols = c(b2s_ratio, pct_greenness, pct_building, 
                        pct_sky, pct_road, pct_sidewalk), 
               names_to = 'variable', 
               values_to = "value") %>% 
  ggplot(mapping = aes(x = highway, y = value)) +
  geom_violin(fill = "#c7ffd6") +
  stat_summary(fun=mean, geom="point", size=1, color="red")+
  coord_flip() +
  theme_bw() +
  facet_wrap(~variable, scales = "free_x", nrow = 2)
## Warning: Removed 1 row containing non-finite outside the scale range
## (`stat_ydensity()`).
## Warning: Removed 1 row containing non-finite outside the scale range
## (`stat_summary()`).