From 809a5c574a7cfde33811caa54503ff80ea0aece0 Mon Sep 17 00:00:00 2001 From: Andrew Flowers Date: Thu, 30 Jul 2015 11:17:58 -0400 Subject: [PATCH] add buster posey mvp scripts --- buster-posey-mvp/README.md | 10 +++ .../baseball_imgcap_for_release.py | 41 +++++++++ buster-posey-mvp/catcher_framing_capture.R | 84 +++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 buster-posey-mvp/README.md create mode 100644 buster-posey-mvp/baseball_imgcap_for_release.py create mode 100644 buster-posey-mvp/catcher_framing_capture.R diff --git a/buster-posey-mvp/README.md b/buster-posey-mvp/README.md new file mode 100644 index 0000000..a3f29d3 --- /dev/null +++ b/buster-posey-mvp/README.md @@ -0,0 +1,10 @@ +### Buster Posey MVP + +The code behind the story [Buster Posey’s Pitch Framing Makes Him A Potential MVP](http://fivethirtyeight.com/features/buster-poseys-pitch-framing-makes-him-a-potential-mvp/). + +File | Description +---|--------- +`baseball_imgcap_for_release.py` | Captures and crops images from a computer screen every .05 seconds. +`catcher_framing_capture.R` | Reads the images into R and calculates the number of pixels changed between successive images. + +Source: [Bloomberg Video](https://www.bsports.com/pro#.Vbo2RvlVhBc) diff --git a/buster-posey-mvp/baseball_imgcap_for_release.py b/buster-posey-mvp/baseball_imgcap_for_release.py new file mode 100644 index 0000000..0979c9c --- /dev/null +++ b/buster-posey-mvp/baseball_imgcap_for_release.py @@ -0,0 +1,41 @@ +from PIL import ImageFile +from PIL import ImageGrab +from PIL import * +import time +import sys +import glob +import math +import os +import gc +from multiprocessing import Process +import win32ui +import win32gui +import win32con + +def piccap(i, w=300, h=300): + hwnd = win32gui.GetDesktopWindow() + wDC = win32gui.GetWindowDC(hwnd) + dcObj = win32ui.CreateDCFromHandle(wDC) + cDC = dcObj.CreateCompatibleDC() + dataBitMap = win32ui.CreateBitmap() + dataBitMap.CreateCompatibleBitmap(dcObj, w, h) + cDC.SelectObject(dataBitMap) + cDC.BitBlt((0, 0), (w, h), dcObj, (800, 300), win32con.SRCCOPY) + dataBitMap.SaveBitmapFile(cDC, "./" + str(i) + "img.png") + win32gui.DeleteObject(dataBitMap.GetHandle()) + dcObj.DeleteDC() + cDC.DeleteDC() + win32gui.ReleaseDC(hwnd, wDC) + gc.collect() + +def piccapCom2(cut): + i = 0 + for i in range(0, cut): + ti = time.clock() + piccap(i) + while time.clock() < (ti + .05): + pass + +'''first, capture all the screens''' +'''movies needs to play simultaneously''' +piccapCom2(int(sys.argv[1])) diff --git a/buster-posey-mvp/catcher_framing_capture.R b/buster-posey-mvp/catcher_framing_capture.R new file mode 100644 index 0000000..df82fdc --- /dev/null +++ b/buster-posey-mvp/catcher_framing_capture.R @@ -0,0 +1,84 @@ +##library I use for reading in images. +library(bmp) + +##attempting to capture the moment immediately after catching +##made a list of each moment for susac and for posey +posey <- c(30, 167, 332, 457, 822, 1016, 1199, +1437, 1621, 1770, 1924, 2101, 2251, 2442, 2594, +2757, 2918, 3072, 3205, 3356, 3526, 3685, +4068, 4217) +susac <- c(751, 1286, 1485, 1666, 2030, +2187) + +##for Posey +##read in files +v <- list.files("buster_posey_catching/") +v <- v[order(as.numeric(unlist(lapply(strsplit(v, split="i"), "[[", 1))))] +setwd("buster_posey_catching/") +full <- list() +for(i in 1:length(v)) +{ +full[[i]] <- read.bmp(v[i]) +} +setwd("../") + +##compare motion patterns across frames +posz <- vector() +for(i in 2:length(full)) +{ +posz[i] <- sum(abs(full[[i]][1:250,1:250,2:4]-full[[i-1]][1:250,1:250,2:4])>1) +print(i) +} + +posm <- list() +for(i in 1:length(posey)) +{ +posm[[i]] <- posz[posey[i]:(posey[i]+100)] +} + +##same for Susac! +v <- list.files("andrew_susac_catching/") +v <- v[order(as.numeric(unlist(lapply(strsplit(v, split="i"), "[[", 1))))] +setwd("andrew_susac_catching/") +full <- list() +for(i in 1:length(v)) +{ +full[[i]] <- read.bmp(v[i]) +} +setwd("../") + +susz <- vector() +for(i in 2:length(full)) +{ +susz[i] <- sum(abs(full[[i]][1:250,1:250,2:4]-full[[i-1]][1:250,1:250,2:4])>1) +print(i) +} + +susm <- list() +for(i in 1:length(susac)) +{ +susm[[i]] <- susz[susac[i]:(susac[i]+100)] +} + +##now averaging motion patterns +##across the receptions +posv <- vector() +susv <- vector() + +for(i in 1:100) +{ +posv[i] <- mean(unlist(lapply(posm, "[[", i))) +} + +for(i in 1:100) +{ +susv[i] <- mean(unlist(lapply(susm, "[[", i))) +} + +plot(posv[1:20], type="l", col="red", lwd=3, +ylim=c(80000, 120000), xlab="Time", xaxt="no", +ylab="Total Pixel Movement") +lines(susv[1:20], type="l", col="blue", lwd=3) +axis(side=1, at=c(0,5,10,15,20), +labels=as.character(c(0,5,10,15,20)*.05)) +