Skip to content

Instantly share code, notes, and snippets.

@codePrincess
codePrincess / result.swift
Last active May 23, 2018 09:46
Validate the result of a CoreML request
public func handleClassification(request: VNRequest, error: Error?) {
guard let observations = request.results as? [VNClassificationObservation]
else { fatalError("unexpected result type from VNCoreMLRequest") }
guard let best = observations.first else {
fatalError("classification didn't return any results")
}
DispatchQueue.main.async {
if best.identifier.starts(with: "Unknown") || best.confidence < 0.50 {
@codePrincess
codePrincess / prediction_coreml.swift
Last active May 23, 2018 09:46
Do a prediction with CoreML
let image = UIImage(named:"catinmood")
do {
let pixelBuffer = image.pixelBuffer(width: 227, height:227)
let classifierRequestHandler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer!, options: [:])
try classifierRequestHandler.perform(classificationRequest!)
} catch {
print("something went terribly wrong during classification")
}
@codePrincess
codePrincess / init.swift
Last active May 23, 2018 09:45
Initialise CoreMLRequest
let model = try VNCoreMLModel(for: catmoodprediction().model)
let request = VNCoreMLRequest(model: model, completionHandler: self.handleClassification)
var classificationRequest = [ request ]
func recognizeWithLocalModel(_ image: UIImage) {
if let data = generateMultiArrayFrom(image: image) {
guard let modelOutput = try? singleNumberModel?.prediction(input: data) else {
return
}
if let result = modelOutput?.classLabel {
self.addLabelForOCR(text: "\(result)")
} else {
print("no result available")
func generateMultiArrayFrom(image: UIImage) -> MLMultiArray? {
guard let data = try? MLMultiArray(shape: [8,8], dataType: .double) else {
return nil
}
let hTileWidth = image.size.width / 8
let vTileWidth = image.size.height / 8
var xPos : CGFloat = 0
var yPos : CGFloat = 0
func drawStroke(context: CGContext?, touch: UITouch) {
let location = touch.location(in: self)
if touch.type == .stylus {
minX = min(minX, Int(location.x))
minY = min(minY, Int(location.y))
maxX = max(maxX, Int(location.x))
maxY = max(maxY, Int(location.y))
//....
}
//....
#!/usr/bin/env python
#print(__doc__)
# Author: Gael Varoquaux <gael dot varoquaux at normalesup dot org>
# License: BSD 3 clause
# Standard scientific Python imports
import matplotlib.pyplot as plt
import pickle
from coremltools.converters import sklearn as sklearn_to_ml
from sklearn.externals import joblib
model = joblib.load('mymodel.pkl')
print('Converting model')
coreml_model = sklearn_to_ml.convert(model)
print('Saving CoreML model')
coreml_model.save('mycoremlmodel.mlmodel')
@codePrincess
codePrincess / faceapi.cs
Created August 25, 2017 07:14
Azure Function for calling Cognitive Services Face API
#r "Newtonsoft.Json"
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@codePrincess
codePrincess / todo.js
Created August 17, 2017 17:42
get slack payload - save to backend
module.exports = function(context, req) {
//context.log('Node.js HTTP trigger function processed a request. RequestUri=%s', req.originalUrl);
context.log('push data:', req.body);
var str = req.body;
var results = str.split("&");
var slackText = getParam(results, "text").replace(/\+/g, " ").replace(/mytodo/g, "");
var userName = getParam(results, "user_name").replace(/\+/g, " ");
context.log(getParam(results, "token"));