Skip to content

Instantly share code, notes, and snippets.

View geohot's full-sized avatar

George Hotz geohot

View GitHub Profile
@geohot
geohot / extract.py
Last active July 18, 2022 05:13
extract imagenet ILSVRC2012 recursive tar
# extract ILSVRC2012 without killing your SSD
import tarfile
import os
import sys
def mkdir(x):
try:
os.makedirs(x)
except OSError, e:
from keras.models import Sequential, Model
from keras.layers import Dense, Input, BatchNormalization as BN
input_img1 = Input(shape=(4,), name="input_img1")
vision_model = Sequential()
vision_model.add(Dense(4, input_shape=(4,)))
vision_model.add(BN(axis=1))
vision_model.add(Dense(4))
@geohot
geohot / tf_bundle.py
Created February 23, 2017 09:49
Really Low Quality Bundle Adjustment in Tensorflow
guess = np.concatenate([np.array(map(lambda x: np.array(x[:,3]).ravel(), rts)), fpts[:, 0:3]], axis=0)
selects = []
xys = []
for i, trk in enumerate(good_good_tracks):
for shot, kp in trk:
xy = G.node[(shot,kp)]['kp']/0.81412059
xys.append(xy)
select = np.zeros((len(frms)+len(good_good_tracks),))
select[shot] = 1
select[len(frms)+i] = 1
@geohot
geohot / get_vin.py
Created September 13, 2017 08:39
Gets VIN from Prius
#!/usr/bin/env python
def msg(x):
print "S:",x.encode("hex")
if len(x) <= 7:
ret = chr(len(x)) + x
else:
assert False
return ret.ljust(8, "\x00")
def isotp_send(panda, x, addr, bus=0):
import cv2
import numpy as np
from tqdm import tqdm
def test_frame_gen(limit=None):
fq = []
cap = cv2.VideoCapture("data/test.mp4")
cnt = 0
while 1:
ret, frame = cap.read()
@geohot
geohot / ransac_polyfit.py
Last active July 31, 2023 06:39
RANSAC polyfit. Fit polynomials with RANSAC in Python
def ransac_polyfit(x, y, order=3, n=20, k=100, t=0.1, d=100, f=0.8):
# Thanks https://en.wikipedia.org/wiki/Random_sample_consensus
# n – minimum number of data points required to fit the model
# k – maximum number of iterations allowed in the algorithm
# t – threshold value to determine when a data point fits a model
# d – number of close data points required to assert that a model fits well to data
# f – fraction of close data points required
besterr = np.inf
def getFundamentalMatrix(pts1, pts2):
vvec = []
for p1, p2 in zip(pts1, pts2):
vvec.append([p2[0] * p1[0], p2[0] * p1[1], p2[0], p2[1] * p1[0], p2[1] * p1[1], p2[1], p1[0], p1[1], 1])
vvec = np.array(vvec)
U, S, Vt = np.linalg.svd(vvec)
Fvl = Vt[-1]
om = Fvl.reshape(3,3)
@geohot
geohot / colorchecker.py
Created February 10, 2018 00:22
ColorChecker colors
real_colors = [[115,82,68],[194,150,130],[98,122,157],[87,108,67],[133,128,177],[103,189,170],[214,126,44],[80,91,166],[193,90,99],[94,60,108],[157,188,64],[224,163,46],[56,61,150],[70,148,73],[175,54,60],[231,199,31],[187,86,149],[8,133,161],[243,243,242],[200,200,200],[160,160,160],[122,122,121],[85,85,85],[52,52,52]]
@geohot
geohot / lines.py
Created May 8, 2018 05:29
Lines in homogeneous coordinates
# homogenous line from two points (this isn't on the internet...)
line = [0,0,0]
line[0] = (f1[1] - f2[1]) * (f1[1] * f2[0] - f1[0] * f2[1])
line[1] = (f1[0] - f2[0]) * (f1[0] * f2[1] - f1[1] * f2[0])
line[2] = (f1[0] * f2[1] - f1[1] * f2[0]) * (f1[1] * f2[0] - f1[0] * f2[1])
line = np.array(line)
# line intersection (https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection#Using_homogeneous_coordinates)
p = np.array([l1[1] * l2[2] - l2[1] * l1[2], l2[0] * l1[2] - l1[0] * l2[2], l1[0] * l2[1] - l2[0] * l1[1]])
p /= p[2]
@geohot
geohot / pyreceive.py
Created May 24, 2018 17:35
Receiver for EON camera
#!/usr/bin/env python
import os
import sys
import argparse
import zmq
import json
import cv2
import numpy as np
from hexdump import hexdump
import scipy.misc