├── .gitignore
├── Clarifai_Logo.png
├── LICENSE
├── README.md
├── custom_train.js
├── index.html
├── keys.js
├── package.json
└── predict.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | keys.js
3 |
--------------------------------------------------------------------------------
/Clarifai_Logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Clarifai/javascript-starter/HEAD/Clarifai_Logo.png
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright 2016 Clarifai, Inc.
2 |
3 | Licensed under the Apache License, Version 2.0 (the "License");
4 | you may not use this file except in compliance with the License.
5 | You may obtain a copy of the License at
6 |
7 | http://www.apache.org/licenses/LICENSE-2.0
8 |
9 | Unless required by applicable law or agreed to in writing, software
10 | distributed under the License is distributed on an "AS IS" BASIS,
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | See the License for the specific language governing permissions and
13 | limitations under the License.
14 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # javascript-starter
2 | A few simple examples to help you get started using the Clarifai Javascript client and API
3 |
4 | ## How to get started
5 | Download this repo, simply invoke
6 | ```script
7 | $ npm install
8 | ```
9 |
10 | ## Usage
11 |
12 | To get started, create an account at [developer.clarifai.com](http://developer.clarifai.com).
13 |
14 | Create an application, and get your API Key.
15 |
16 | This basic starter uses your API Key to make prediction calls. This will never expire so you only have to fill it in once.
17 |
18 | You'll notice that in the `.gitignore` file, it references a `keys.js` file. This is for security purposes, so you don't share your API Key with others. Add the following to that file:
19 |
20 | ```
21 | var myApiKey = 'YOUR API KEY HERE';
22 | ```
23 |
24 | You'll also notice a custom_train.js file which serves as a reference for Custom Training. Any custom models that you create (under these credentials) will appear in the dropdown menu on index.html, next to the "Custom" label
25 |
26 | ## Example Output
27 |
28 |
29 |
30 | Note the "Add image to Application" button on the bottom left of the image. This will automatically add the image to the application that is associated with your key!
31 |
--------------------------------------------------------------------------------
/custom_train.js:
--------------------------------------------------------------------------------
1 | // Require the client
2 | var Clarifai = require('clarifai');
3 | var fs = require('fs');
4 | var FileReader = require('filereader');
5 |
6 | // instantiate a new Clarifai app passing in your clientId and clientSecret
7 | var app = new Clarifai.App(
8 | CLIENT_ID,
9 | CLIENT_SECRET
10 | );
11 |
12 | trainModel();
13 |
14 | // once inputs are created, create model by giving name and list of concepts
15 | function createModel() {
16 | app.models.create('pets', ["dog", "cat"]).then(
17 | (response) => {
18 | console.log(response);
19 | },
20 | (error) => {
21 | console.error(error);
22 | }
23 | );
24 | }
25 |
26 | // inputs with concept using urls
27 | function addInputsWithUrls(urls, isPostive, concept){
28 | for (index in urls){
29 | app.inputs.create({
30 | url: urls[index],
31 | concepts: [
32 | {
33 | id: concept,
34 | value: isPostive
35 | }
36 | ]
37 | }).then(
38 | (response) => {
39 | console.log(response);
40 | },
41 | (error) => {
42 | console.error(error);
43 | }
44 | );
45 | }
46 | }
47 |
48 |
49 | // inputs with concept using filepath
50 | function addInputsWithFiles(filepath, isPostive, concept){
51 |
52 | for(data of getBase64s(getFiles(filepath))){
53 | app.inputs.create({
54 | base64: data,
55 | concepts: [
56 | {
57 | id: concept,
58 | value: isPostive
59 | }
60 | ]
61 | });
62 | }
63 | }
64 |
65 |
66 | function getFiles(dir){
67 | fileList = [];
68 |
69 | var files = fs.readdirSync(dir);
70 | for(var i in files){
71 | if (!files.hasOwnProperty(i) || i == 0) continue;
72 | var name = dir+'/'+files[i];
73 | if (!fs.statSync(name).isDirectory()){
74 | fileList.push(name);
75 | }
76 | }
77 | return fileList;
78 | }
79 |
80 | function getBase64s(fileList) {
81 | var FR = new FileReader();
82 | base64s = [];
83 | for(index in fileList) {
84 | base64s.push(base64_encode(fileList[index]));
85 | }
86 | return base64s;
87 | }
88 |
89 | // function to encode file data to base64 encoded string
90 | function base64_encode(file) {
91 | // read binary data
92 | var bitmap = fs.readFileSync(file);
93 | // convert binary data to base64 encoded string
94 | return new Buffer(bitmap).toString('base64');
95 | }
96 |
97 |
98 | // after model is created, you can now train the model
99 | function trainModel() {
100 | app.models.train("pets").then(
101 | (response) => {
102 | console.log(response);
103 | },
104 | (error) => {
105 | console.error(error);
106 | }
107 | );
108 | }
109 |
--------------------------------------------------------------------------------
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |