Skip to content

Instantly share code, notes, and snippets.

@burrussmp
burrussmp / SegmentationOneHotEncoder.py
Last active May 5, 2020 18:44
Takes in a matrix HxWx1 where each pixel is a label in the set {0,..,k} and returns a matrix of size HxWx(k+1)
"""
One-hot-encodes segmentation map
@params
y: np.ndarray
HxWx1 seegmentation map where each element is in set {0,...,num_labels}
num_labels: int
Defines the set {0,...,num_labels} which is used to one-hot-encode the segmentation map
Ex. For annotation segmentation, we have 26 letters, so we say 26. It is assumed that a 0 means no class of interest.
@return
target: np.ndarray
@burrussmp
burrussmp / PaperBackground.py
Created May 3, 2020 18:22
Create a realistic paper-like background
BG_COLOR = 209
BG_SIGMA = 5
MONOCHROME = 1
def blank_image(width=1024, height=1024, background=BG_COLOR):
"""
It creates a blank image of the given background color
"""
img = np.full((height, width, MONOCHROME), background, np.uint8)
return img
@burrussmp
burrussmp / createMesh.cs
Created April 26, 2020 21:39
Create a mesh in Unity
IEnumerator CreateMesh(){
while(inFirst)
yield return new WaitForSeconds(0.1f);
int meshID = 97;
int cur = 0;
var resources = Resources.FindObjectsOfTypeAll(typeof(Material));
foreach(var face in mList){
int num_of_vertices = Convert.ToInt32(face[face.Count-1]);
// should we make it hollow?
if (mMap["special"] == 'H' && cur < 4){
@burrussmp
burrussmp / Triangulate.cs
Created April 26, 2020 21:35
Triangulate function to extract the triangles from a list of vertices to construct a mesh in Unity
public class Triangulator
{
private List<Vector2> m_points = new List<Vector2>();
public Triangulator (Vector2[] points) {
m_points = new List<Vector2>(points);
}
public int[] Triangulate() {
List<int> indices = new List<int>();
@burrussmp
burrussmp / DetectChanges.cs
Last active April 26, 2020 21:19
Vuforia handle detection of image target
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
Debug.Log("Detected");
@burrussmp
burrussmp / getFaceData.cs
Last active April 26, 2020 21:10
GET face data from server and parse it into a list
UnityWebRequest www = UnityWebRequest.Get("http://SERVERIP:PORT/data");
yield return www.SendWebRequest();
Debug.Log(www.responseCode);
if(www.isNetworkError || www.isHttpError || www.responseCode == 500) {
GameObject myObject = GameObject.Find("ARCamera");
myObject.GetComponent<Buttons>().showError("Need to Capture Front and Side!");
yield return new WaitForSeconds(3);
myObject.GetComponent<Buttons>().clearError();
}
@burrussmp
burrussmp / sendImageServer.cs
Last active April 26, 2020 21:08
c# code to capture image from unity
IEnumerator TakePhoto(string type) // Start this Coroutine on some button click
{
// NOTE - you almost certainly have to do this here:
yield return new WaitForEndOfFrame();
Texture2D tex = new Texture2D(Screen.width, Screen.height,TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
tex.Apply();
@burrussmp
burrussmp / handleImagePostRequest.py
Created April 26, 2020 20:32
A POST request handler to parse and receive a PNG image
def deal_post_data(self):
ctype, pdict = cgi.parse_header(self.headers['Content-Type'])
pdict['boundary'] = bytes(pdict['boundary'], "utf-8")
pdict['CONTENT-LENGTH'] = int(self.headers['Content-Length'])
if ctype == 'multipart/form-data':
form = cgi.FieldStorage( fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD':'POST', 'CONTENT_TYPE':self.headers['Content-Type'], })
# typeOfImage = form["Type"].value + '.png'
bbox = {
'x':int(form["x"].value),
'y':int(form["y"].value),
@burrussmp
burrussmp / getMesh.py
Last active April 26, 2020 20:25
A GET handler to stringify a list of faces where each element in the list is a set of points that define the face in a clockwise winding order
def get_mesh(self):
if processor.ready_to_send():
faces = processor.get_faces()
"""Respond to a GET request."""
self.send_response(200)
self.send_header("Content-type", "text")
self.end_headers()
for i in range(len(faces)):
self.wfile.write("N".encode())
self.wfile.write(str(len(faces[i])).encode())
def create3DFaces(sideHull,frontHull):
sideHull = normalize(sideHull)
frontHull = normalize(frontHull)
sideHull = addZAxis(sideHull)
frontHull = addZAxis(frontHull)
frontHull = rotate_by_90(frontHull)
frontHull,sideHull,hull_back = match_front_face(frontHull,sideHull)
faces = construct_faces(frontHull,hull_back)
faces = scale_down_faces(faces)
return faces