Skip to content

Commit 6b1d2c7

Browse files
committed
Add Google Vision with React Native
1 parent d809d9c commit 6b1d2c7

File tree

8 files changed

+5907
-0
lines changed

8 files changed

+5907
-0
lines changed

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ cp -r tensorflowjs-template new
1818
1. [image-to-tensor](image-to-tensor/)
1919
1. [tensor-reversal](tensor-reversal/)
2020
1. [old-tv](old-tv/)
21+
1. [Vision](vision/)
2122

2223
## Ressources
2324

2425
- [The TensorFlow.js gallery](https://github.com/tensorflow/tfjs/blob/master/GALLERY.md)
2526
- [Awesome TensorFlow.js](https://github.com/aaronhma/awesome-tensorflow-js)
27+
- [IA Vision - Google Cloud](https://cloud.google.com/vision)

Diff for: vision/.gitignore

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# OSX
2+
#
3+
.DS_Store
4+
5+
# Xcode
6+
#
7+
build/
8+
*.pbxuser
9+
!default.pbxuser
10+
*.mode1v3
11+
!default.mode1v3
12+
*.mode2v3
13+
!default.mode2v3
14+
*.perspectivev3
15+
!default.perspectivev3
16+
xcuserdata
17+
*.xccheckout
18+
*.moved-aside
19+
DerivedData
20+
*.hmap
21+
*.ipa
22+
*.xcuserstate
23+
project.xcworkspace
24+
25+
# Android/IntelliJ
26+
#
27+
build/
28+
.idea
29+
.gradle
30+
local.properties
31+
*.iml
32+
33+
# node.js
34+
#
35+
node_modules/
36+
npm-debug.log
37+
yarn-error.log
38+
39+
# BUCK
40+
buck-out/
41+
\.buckd/
42+
*.keystore
43+
44+
# fastlane
45+
#
46+
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
47+
# screenshots whenever they are needed.
48+
# For more information about the recommended setup visit:
49+
# https://docs.fastlane.tools/best-practices/source-control/
50+
51+
*/fastlane/report.xml
52+
*/fastlane/Preview.html
53+
*/fastlane/screenshots
54+
55+
# Bundle artifacts
56+
*.jsbundle
57+
58+
# CocoaPods
59+
/ios/Pods/
60+
61+
# Expo
62+
.expo/*
63+
web-build/
64+
.env

Diff for: vision/App.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import * as ImagePicker from 'expo-image-picker';
2+
import React from 'react';
3+
import { Button, Image, StyleSheet, Text, View } from 'react-native';
4+
5+
const API_KEY = 'ADD_YOUR_KEY_HERE';
6+
const API_URL = `https://vision.googleapis.com/v1/images:annotate?key=${API_KEY}`;
7+
8+
async function callGoogleVisionAsync(image) {
9+
const body = {
10+
requests: [
11+
{
12+
image: {
13+
content: image,
14+
},
15+
features: [
16+
{
17+
type: 'LABEL_DETECTION',
18+
maxResults: 1,
19+
},
20+
],
21+
},
22+
],
23+
};
24+
25+
const response = await fetch(API_URL, {
26+
method: 'POST',
27+
headers: {
28+
Accept: 'application/json',
29+
'Content-Type': 'application/json',
30+
},
31+
body: JSON.stringify(body),
32+
});
33+
const result = await response.json();
34+
console.log('callGoogleVisionAsync -> result', result);
35+
36+
return result.responses[0].labelAnnotations[0].description;
37+
}
38+
39+
export default function App() {
40+
const [image, setImage] = React.useState(null);
41+
const [status, setStatus] = React.useState(null);
42+
const [permissions, setPermissions] = React.useState(false);
43+
44+
const askPermissionsAsync = async () => {
45+
let permissionResult = await ImagePicker.requestCameraPermissionsAsync();
46+
47+
if (permissionResult.granted === false) {
48+
alert('Permission to access camera roll is required!');
49+
return;
50+
} else {
51+
setPermissions(true);
52+
}
53+
};
54+
55+
const takePictureAsync = async () => {
56+
const { cancelled, uri, base64 } = await ImagePicker.launchCameraAsync({
57+
base64: true,
58+
});
59+
60+
if (!cancelled) {
61+
setImage(uri);
62+
setStatus('Loading...');
63+
try {
64+
const result = await callGoogleVisionAsync(base64);
65+
setStatus(result);
66+
} catch (error) {
67+
setStatus(`Error: ${error.message}`);
68+
}
69+
} else {
70+
setImage(null);
71+
setStatus(null);
72+
}
73+
};
74+
75+
return (
76+
<View style={styles.container}>
77+
{permissions === false ? (
78+
<Button onPress={askPermissionsAsync} title="Ask permissions" />
79+
) : (
80+
<>
81+
{image && <Image style={styles.image} source={{ uri: image }} />}
82+
{status && <Text style={styles.text}>{status}</Text>}
83+
<Button onPress={takePictureAsync} title="Take a Picture" />
84+
</>
85+
)}
86+
</View>
87+
);
88+
}
89+
90+
const styles = StyleSheet.create({
91+
container: {
92+
flex: 1,
93+
backgroundColor: 'white',
94+
alignItems: 'center',
95+
justifyContent: 'center',
96+
},
97+
image: {
98+
width: 300,
99+
height: 300,
100+
},
101+
text: {
102+
margin: 5,
103+
},
104+
});

Diff for: vision/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# React Native with Google Vision
2+
3+
Using [vision.googleapis.com](https://cloud.google.com/vision) to identify the content of a picture.
4+
Thanks to @EvanBacon [for the example](https://github.com/expo/examples/tree/master/with-google-vision).
5+
6+
## Result
7+
8+
![React Native with Google Vision](./images/google-cloud-vision.gif)
9+
10+
## Getting Started
11+
12+
**Requirements**
13+
14+
- Google Cloud Vision API key
15+
16+
```
17+
yarn
18+
yarn start // then scan the QR Code
19+
```

Diff for: vision/babel.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = function (api) {
2+
api.cache(true);
3+
return {
4+
presets: ['babel-preset-expo'],
5+
};
6+
};

Diff for: vision/images/google-cloud-vision.gif

4.73 MB
Loading

Diff for: vision/package.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"dependencies": {
3+
"expo": "~39.0.0",
4+
"expo-image-picker": "~9.1.0",
5+
"expo-permissions": "^9.3.0",
6+
"react": "~16.13.0",
7+
"react-dom": "~16.13.0",
8+
"react-native": "https://github.com/expo/react-native/archive/sdk-39.0.2.tar.gz",
9+
"react-native-web": "0.13.13"
10+
},
11+
"devDependencies": {
12+
"@babel/core": "7.9.0"
13+
},
14+
"scripts": {
15+
"start": "expo start",
16+
"android": "expo start --android",
17+
"ios": "expo start --ios",
18+
"web": "expo web",
19+
"eject": "expo eject"
20+
},
21+
"private": true
22+
}

0 commit comments

Comments
 (0)