Skip to content

Instantly share code, notes, and snippets.

View J3698's full-sized avatar
🐛
Inch Worm

Anti J3698

🐛
Inch Worm
  • Working
  • Working
View GitHub Profile
@app.route("/classify")
@cross_origin()
def classify_symbol():
json_str = request.args.get('points')
if json_str is None:
return {"top5": [" ", " ", " ", " ", " "]}
json_data = json.loads(json_str)
if 'data' not in json_data:
return {"top5": [" ", " ", " ", " ", " "]}
order = sorted([str(i) for i in range(1098)])
chars = sorted(set(np.load("data_processed/dataY.npy")))
def fix_predictions(output):
outs = [chars[int(order[i.item()])] for i in torch.topk(output, 5, dim = 1).indices[0]]
return ["\\" + i.split("_")[1] for i in outs]
model = Model()
model.eval()
state_dict = torch.load("Test.pt", map_location = torch.device("cpu"))
model.load_state_dict(state_dict['model'])
size = 32
from PIL import Image, ImageDraw
import numpy as np
import torch
from train2 import Model
from torchvision.transforms import ToTensor
from flask import Flask
from flask_cors import CORS, cross_origin
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
@app.route("/classify")
@cross_origin()
def classify_symbol():
import os
import random
from shutil import copyfile
from tqdm.auto import tqdm
size = 32
os.makedirs(f"images_data{size}/train", exist_ok = True)
os.makedirs(f"images_data{size}/val", exist_ok = True)
os.makedirs(f"images_data{size}/test", exist_ok = True)
@J3698
J3698 / add_re_typeset_handler.js
Created September 9, 2021 19:32
Typeset all over again
function addReTypesetHandler() {
var script = document.createElement("script");
script.innerHTML = `
last = "1";
setInterval(function() {
var tag = document.getElementsByClassName("invisible")[0];
if (tag.innerHTML != last) {
MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
last = tag.innerHTML;
}
@J3698
J3698 / classify_request.js
Last active September 9, 2021 19:30
Request predictions
function addClassifyRequestInterval() {
setInterval(function() {
if (!shouldUpdate) {
return
}
shouldUpdate = false;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
@J3698
J3698 / initial_backend.py
Last active September 10, 2021 15:53
First pass at a backend
from flask import Flask
app = Flask(__name__)
@app.route("/classify")
def classify_symbol():
return {"top5": ["A", "B", "C", "D", "E"]}
@J3698
J3698 / run_pipeline.py
Created September 7, 2021 16:45
Run the OAK pipeline
def run_pipeline(pipeline):
with depthai.Device(pipeline) as device:
q_nn = device.getOutputQueue("nn", maxSize=4, blocking=False)
while True:
in_nn = q_nn.tryGet()
if in_nn is not None:
# get data
output = in_nn.getAllLayerNames()[-1]
data = np.array(in_nn.getLayerFp16(output))