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
@J3698
J3698 / export_and_run.py
Created September 7, 2021 15:55
Putting a model onto the OAK-1
def main():
clear_exports()
export_test()
optimize_model("test")
convert_blob("test")
pipeline = setup_pipeline()
run_pipeline(pipeline)
@J3698
J3698 / test_model.py
Created September 7, 2021 16:03
Test model for the OAK-1
class Test(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return x / 2
@J3698
J3698 / export_test_model.py
Created September 7, 2021 16:03
Export simple test model
def export_test():
image = torch.ones((1, 3, 256, 256), requires_grad = False)
model = Test()
torch.onnx.export(model, image, "exports/test.onnx",
export_params = True,
opset_version = 11,
do_constant_folding = True,
input_names = ['input'],
output_names = ['output']
@J3698
J3698 / optimize_model.py
Created September 7, 2021 16:18
Optimize model for OAK-1
def optimize_model(name):
optimizer_path = "/opt/intel/openvino_2020.1.023/" + \
"deployment_tools/model_optimizer/mo.py"
subprocess.run(["python3", optimizer_path,
"--input_model", f"./exports/{name}.onnx",
"--data_type", "FP16"])
os.rename(name + ".bin", os.path.join("./exports", name + ".bin"))
os.rename(name + ".xml", os.path.join("./exports", name + ".xml"))
os.rename(name + ".mapping", os.path.join("./exports", name + ".mapping"))
@J3698
J3698 / blob_conversion.py
Created September 7, 2021 16:24
Convert OpenVINO to blob
import blobconverter
def convert_blob(name):
xmlfile = os.path.join("./exports", name) + ".xml"
binfile = os.path.join("./exports", name) + ".bin"
blob_path = blobconverter.from_openvino(
xml = xmlfile,
bin = binfile,
data_type = "FP16",
@J3698
J3698 / setup_pipeline.py
Created September 7, 2021 16:39
Set up pipeline for simple model OAK-1
def setup_pipeline():
pipeline = depthai.Pipeline()
cam_rgb = pipeline.createColorCamera()
cam_rgb.setPreviewSize(256, 256)
cam_rgb.setInterleaved(False)
detection_nn = pipeline.createNeuralNetwork()
detection_nn.setBlobPath("./exports/test_openvino_2021.4_5shave.blob")
cam_rgb.preview.link(detection_nn.input)
@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))
@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 / 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 / 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;
}