Skip to content

Instantly share code, notes, and snippets.

View antonyharfield's full-sized avatar

Antony Harfield antonyharfield

View GitHub Profile
@antonyharfield
antonyharfield / convert.py
Created June 13, 2020 13:38
YAMNet to TFLite conversion
import tensorflow as tf
from tensorflow.keras import Model, layers
import features as features_lib
import features_tflite as features_tflite_lib
import params
from yamnet import yamnet
def yamnet_frames_tflite_model(feature_params):
"""Defines the YAMNet model suitable for tflite conversion."""
num_samples = int(round(params.SAMPLE_RATE * 0.975))
@antonyharfield
antonyharfield / convert_fail2.py
Last active June 13, 2020 13:29
YAMNet to TFLite failed conversion 2
import tensorflow as tf
from tensorflow.keras import Model, layers
import features as features_lib
import params
from yamnet import yamnet
def yamnet_frames_tflite_model(feature_params):
num_samples = int(round(params.SAMPLE_RATE * 0.975))
waveform = layers.Input(batch_shape=(1, num_samples))
# Store the intermediate spectrogram features to use in visualization.
@antonyharfield
antonyharfield / convert_fail1.py
Last active June 13, 2020 13:29
YAMNet to TFLite failed conversion 1
import tensorflow as tf
import params
import yamnet
def main():
# Load the model and weights
model = yamnet.yamnet_frames_model(params)
model.load_weights('yamnet.h5')
# Convert the model
@antonyharfield
antonyharfield / gcs_list_directories.py
Created November 23, 2019 14:25
Google Cloud Storage list the directories or folders within a given bucket and path (not including objects!)
from google.api_core import page_iterator
from google.cloud import storage
def _item_to_value(iterator, item):
return item
def list_directories(bucket_name, path):
if not path.endswith('/'):
path += '/'
@antonyharfield
antonyharfield / RailwayOP-CompleteExample.kt
Created April 29, 2019 17:51
Railway Oriented Programming in Kotlin (as described here)
// Result is a superpowered enum that can be Success or Failure
// and the basis for a railway junction
sealed class Result<T>
data class Success<T>(val value: T): Result<T>()
data class Failure<T>(val errorMessage: String): Result<T>()
// Composition: apply a function f to Success results
infix fun <T,U> Result<T>.then(f: (T) -> Result<U>) =
when (this) {
is Success -> f(this.value)
sealed class Result<T>
data class Success<T>(val value: T): Result<T>()
data class Failure<T>(val errorMessage: String): Result<T>()
infix fun <T,U> Result<T>.compose(f: (T) -> Result<U>) =
when (this) {
is Success -> f(this.value)
is Failure -> Failure(this.errorMessage)
}
@antonyharfield
antonyharfield / package.json
Created December 29, 2018 09:33
A fulfillment web hook for Dialogflow supporting local dev and Firebase Cloud Functions
{
"name": "webhook-demo",
"version": "0.0.1",
"description": "A template for creating web hooks for Dialogflow",
"author": "Antony Harfield",
"main": "src/cloudFuncs.js",
"engines": { "node": "8" },
"scripts": {
"dev": "nodemon --inspect src/server.js",
"start": "node src/server.js",
@antonyharfield
antonyharfield / app.js
Last active April 26, 2024 09:05
A fulfillment web hook for Dialogflow supporting local dev and Firebase Cloud Functions
const express = require('express')
const { WebhookClient } = require('dialogflow-fulfillment')
const app = express()
app.get('/', (req, res) => res.send('online'))
app.post('/dialogflow', express.json(), (req, res) => {
const agent = new WebhookClient({ request: req, response: res })
function welcome () {
agent.add('Welcome to my agent!')
@antonyharfield
antonyharfield / simpleServer.js
Last active December 29, 2018 03:14
A simple fulfillment web hook for Dialogflow
const express = require('express')
const { WebhookClient } = require('dialogflow-fulfillment')
const app = express()
app.get('/', (req, res) => res.send('online'))
app.post('/dialogflow', express.json(), (req, res) => {
const agent = new WebhookClient({ request: req, response: res })
function welcome () {
agent.add('Welcome to my agent!')
@antonyharfield
antonyharfield / UIWindow+AnimatedRootViewController.swift
Last active November 2, 2017 10:41
Swift category/extension for animating the switch/swap to a new root view controller at the UIWindow level
// Swift category/extension for animating between rootViewControllers at the UIWindow level
import UIKit
extension UIWindow {
func setRootViewController(newRootViewController: UIViewController, animated: Bool) {
// No animation required
if !animated || self.rootViewController == nil {