Skip to content

Instantly share code, notes, and snippets.

View zsajjad's full-sized avatar
👊
Always Exploring

Zain Sajjad zsajjad

👊
Always Exploring
View GitHub Profile
@zsajjad
zsajjad / ViewController.swift
Last active March 10, 2020 12:15
View Controller for Peekaboo iFrame
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad();
@zsajjad
zsajjad / MainActivity.java
Last active March 10, 2020 12:15
Peekaboo - Android WebView Sample
package fetchsky.androidsample;
import android.annotation.TargetApi;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebResourceRequest;
@zsajjad
zsajjad / processImage.js
Last active August 18, 2018 23:12
Simple Method to capture image and provide it to RNTextDetector
/**
* processImage
*
* Responsible for getting image from react native camera and
* starting image processing.
*
* @param {string} uri Path for the image to be processed
* @param {object} imageProperties Other properties of image to be processed
* @memberof App
* @author Zain Sajjad
@zsajjad
zsajjad / imageToTensor.ts
Last active April 20, 2020 20:39
Converting images to ONNX.js Tensor for processing.
import ndarray from 'ndarray';
import ops from 'ndarray-ops';
import { Tensor } from 'onnxjs';
export function preProcess(ctx: CanvasRenderingContext2D): Tensor {
const imageData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
const { data, width, height } = imageData;
const dataTensor = ndarray(new Float32Array(data), [width, height, 4]);
const dataProcessedTensor = ndarray(new Float32Array(width * height * 3), [1, 3, width, height]);
ops.assign(dataProcessedTensor.pick(0, 0, null, null), dataTensor.pick(null, null, 2));
@zsajjad
zsajjad / loadImageToCanvas.ts
Last active January 8, 2019 22:16
Drawing an image from URL in html canvas
import loadImage from 'blueimp-load-image';
const imageLoaderConfigs = {
maxWidth: 224,
maxHeight: 224,
cover: true,
crop: true,
canvas: true,
crossOrigin: 'Anonymous',
}
@zsajjad
zsajjad / runModel.ts
Created January 8, 2019 22:22
Running ONNX.js model
export async function runModel(model: InferenceSession, preProcessedData: Tensor): Promise<[Tensor, number]> {
const start = new Date();
try {
const outputData = await model.run([preProcessedData]);
const end = new Date();
const inferenceTime = (end.getTime() - start.getTime());
const output = outputData.values().next().value;
return [output, inferenceTime];
} catch (e) {
console.error(e);
@zsajjad
zsajjad / imagenet.ts
Last active January 8, 2019 22:33
Find top k imagenet classes
import _ from 'lodash';
import { imagenetClasses } from '../data/imagenet';
/**
* Find top k imagenet classes
*/
export function imagenetClassesTopK(classProbabilities: any, k = 5) {
const probs = _.isTypedArray(classProbabilities) ?
Array.prototype.slice.call(classProbabilities) : classProbabilities;
@zsajjad
zsajjad / selectImage.js
Last active March 11, 2019 22:56
React Native Image styling app using Fritz SDK - Code files Image Selection
import ImagePicker from 'react-native-image-picker';
/* More info on all the options
*
* https://github.com/react-native-community/react-native-image-picker/blob/master/docs/Reference.md#options
*
*/
const options = {
title: 'Select Image for styling',
storageOptions: {
@zsajjad
zsajjad / Podflie
Last active March 12, 2019 20:50
React Native Image styling app using Fritz SDK - Podfile
# Uncomment the next line to define a global platform for your project
platform :ios, '11.0'
target 'MyStylingApp' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
# Linking React Native for Pods Projects
rn_path = '../node_modules/react-native'
pod 'yoga', path: "#{rn_path}/ReactCommon/yoga/yoga.podspec"
@zsajjad
zsajjad / frtizStyleModel.js
Last active March 16, 2019 16:58
Method to create JS instance of fritz styling model
import { RNFritzVisionImageStyling } from 'react-native-fritz';
const darkKnights = await RNFritzVisionImageStyling({
name: 'darkKnights',
modelIdentifier: '55cdc02f89174e1c8458690d5b8621ae', // Your model identifier here
customModel: true,
modelVersion: 1,
});