I wanted to see how easy it was to do photogrammetry (create 3d models using photos) using PyTorch3D by Facebook AI Research.

It’s a neat idea - a library that supports differentiable rendering of meshes and textures. They have an example of fitting a mesh with texture, and as such the only inputs that go into this script are the 2d images target_images, silhouette_images (that I created using an edge detection algo skimage.filters.sobel - perhaps this isn’t the best way to go about it), and cameras target_cameras with known input locations (although perhaps these can be estimated too). Results on some 3d data I had weren’t great - I need to play around with it to get it to work.

On the topic of estimating camera angles, I found very interesting reading about Perspective-n-Point, specifically in the context of head pose estimation which looks like a well studied topic (enough for nice libraries to exist - I wrote a simple demo below that estimates these pose angles based on images, based on work by others).

The basic idea is that we first find a face, enclose it in a box and find some positions on the face (e.g. nose, eyes). Then, using the distances between these points, etc. (assuming a rigid face), you’d be able to work out the orientation of the face.

Code
# bzip2 -d http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2

import cv2, dlib
import numpy as np
import matplotlib.pyplot as plt
plt.ion(); plt.style.use('ggplot')

img = np.load('img.npy') # or use cv2.imread

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')

class Points2D:
    def __init__(self, img):
        self.face = predictor(img, detector(img)[0])
    def _get(self, i):
        return [self.face.part(i).x, self.face.part(i).y]
    def points(self):
        vertices = [30, 8, 36, 45, 48, 54]
        return np.array(list(map(self._get, vertices)), dtype='float')

def process_face(face):

    ''' skeleton from learnopencv.com / yinguobing head pose estimation '''
    face_model_points = np.array([
        [   0.0,    0.0,    0.0],
        [   0.0, -330.0, - 65.0],
        [-225.0,  170.0, -135.0],
        [ 225.0,  170.0, -135.0],
        [-150.0, -150.0, -125.0],
        [ 150.0, -150.0, -125.0]])

    face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)

    focal_length = face.shape[1]
    center = (face.shape[1]/2, face.shape[0]/2)

    camera_matrix = np.array([
        [focal_length, 1, center[0]],
        [0, focal_length, center[1]],
        [0, 0, 1]], dtype='float')

    dist_coefs = np.zeros((4, 1))
    image_points = Points2D(face).points()

    success, rotation_vector, translation_vector =\
        cv2.solvePnP(face_model_points, image_points,
                     camera_matrix, dist_coefs)

    return rotation_vector

rotation = process_face(img) # pitch roll yaw
points = Points2D(img).points()

plt.imshow(img)
plt.scatter(points[:, 0], points[:, 1])


2021

Gaussian Processes in MGCV

I lay out the canonical GP interpretation of MGCV’s GAM parameters here. Prof. Wood updated the package with stationary GP smooths after a request. Running through the predict.gam source code in a debugger, the computation of predictions appears to be as follows:

~1 min read

Photogrammetry

I wanted to see how easy it was to do photogrammetry (create 3d models using photos) using PyTorch3D by Facebook AI Research.

1 min read

Dead Code & Syntax Trees

This post was motivated by some R code that I came across (over a thousand lines of it) with a bunch of if-statements that were never called. I wanted an automatic way to get a minimal reproducing example of a test from this file. While reading about how to do this, I came across Dead Code Elimination, which kills unused and unreachable code and variables as an example.

1 min read
Back to Top ↑

2020

Astrophotography

I used to do a fair bit of astrophotography in university - it’s harder to find good skies now living in the city. Here are some of my old pictures. I’ve kept making rookie mistakes (too much ISO, not much exposure time, using a slow lens, bad stacking, …), for that I apologize!

1 min read

Probabilistic PCA

I’ve been reading about PPCA, and this post summarizes my understanding of it. I took a lot of this from Pattern Recognition and Machine Learning by Bishop.

1 min read

Spotify Data Exploration

The main objective of this post was just to write about my typical workflow and views. The structure of this data is also outside my immediate domain so I thought it’d be fun to write up a small diary working with the data.

6 min read

Random Stuff

For dealing with road/city networks, refer to Geoff Boeing’s blog and his amazing python package OSMnx. Go to Shapely for manipulation of line segments and other objects in python, networkx for networks in python and igraph for networks in R.

5 min read

Morphing with GPs

The main aim here was to morph space inside a square but such that the transformation preserves some kind of ordering of the points. I wanted to use it to generate some random graphs on a flat surface and introduce spatial deformation to make the graphs more interesting.

2 min read

Speech Synthesis

The initial aim here was to model speech samples as realizations of a Gaussian process with some appropriate covariance function, by conditioning on the spectrogram. I fit a spectral mixture kernel to segments of audio data and concatenated the segments to obtain the full waveform.

4 min read
Back to Top ↑

2019

Back to Top ↑

2018

Back to Top ↑