Skip to content

Commit

Permalink
Trying to run PlantId API
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwardHaoranLee committed Nov 8, 2020
1 parent 6c9d6fa commit f9099cc
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 54 deletions.
6 changes: 6 additions & 0 deletions .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 35 additions & 48 deletions Flourish/Gateways/PlantIdentification.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,41 @@
import React from "react";
import fileUrl from "file-url";
import React from 'react';
import { View, Image, StyleSheet } from 'react-native';
import RNImgToBase64 from "react-native-image-base64";

class PlantIdentification extends React.Component {
sendIdentification = (imageFilePath) => {
const files = [fileUrl(imageFilePath)];
const promises = files.map((file) => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (event) => {
const res = event.target.result;
console.log(res);
resolve(res);
}
reader.readAsDataURL(file)
})
})
module.exports = function sendIdentification(FilePath){
let base64files
RNImgToBase64.getBase64ForTag(FilePath, base64 => {base64files = base64}, err => console.log(err))

Promise.all(promises).then((base64files) => {
console.log(base64files)

const data = {
api_key: "ftgMU1minamhEWUAjMdlh2VVWMTYPGSkT5BioLhADeQNkqDBrN",
images: base64files,
modifiers: ["crops_fast", "similar_images"],
plant_language: "en",
plant_details: ["common_names",
"url",
"name_authority",
"wiki_description",
"taxonomy",
"synonyms"]
};
console.log(base64files)

fetch('https://api.plant.id/v2/identify', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
return(data)
})
.catch((error) => {
console.error('Error:', error);
return(error)
});
const data = {
api_key: "-- ask for one: https://web.plant.id/api-access-request/ --",
images: base64files,
modifiers: ["crops_fast", "similar_images"],
plant_language: "en",
plant_details: ["common_names",
"url",
"name_authority",
"wiki_description",
"taxonomy",
"synonyms"]
};

fetch('https://api.plant.id/v2/identify', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
})
.catch((error) => {
console.error('Error:', error);
});


};
}
};
1 change: 0 additions & 1 deletion Flourish/Tabs/FlourishingTabScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export default class FlourishingTabScreen extends React.Component {
for (let i = 0; i < plants.length; i++) {
for (let j = 0; j < plants[i].reminder.length; j++) {
let r = plants[i].reminder[j];
console.log(r);
if (r.date.getTime() < today.getTime() + 1 * dayMili && r.date.getTime() > today.getTime() - dayMili) {
todayReminders.push(r);
} else if (r.date.getTime() <= week.getTime() && r.date.getTime() > today.getTime()) {
Expand Down
7 changes: 4 additions & 3 deletions Flourish/Tabs/PhotoTabScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import React, { useState, useEffect } from 'react';
import { Button, View, Platform, StyleSheet, TouchableOpacity, Text, Image} from 'react-native';
import * as ImagePicker from 'expo-image-picker';
import { Camera} from 'expo-camera';

export default function PhotoTabScreen() {
const plantID = require("../Gateways/PlantIdentification")


export default function PhotoTabScreen() {

const [hasPermission, setHasPermission] = useState(null);
const [image, setImage] = useState(null);
Expand Down Expand Up @@ -37,9 +37,9 @@ export default function PhotoTabScreen() {
mediaTypes: ImagePicker.MediaTypeOptions.Images,
allowsEditing: true
});
console.log(result);
if (!result.cancelled) {
setImage(result.path);
plantID(result.uri)
}};

return (
Expand Down Expand Up @@ -68,6 +68,7 @@ export default function PhotoTabScreen() {
if (!result.cancelled) {
this.setState({ image: result.uri });
}
plantID(result.uri)
}

return (
Expand Down
91 changes: 91 additions & 0 deletions Flourish/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions Flourish/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web",
"eject": "expo eject"
"eject": "expo eject",
"postinstall": "node_modules/.bin/rn-nodeify --install crypto,fs,https --hack"
},
"dependencies": {
"@ant-design/icons": "^4.2.2",
Expand All @@ -24,19 +25,22 @@
"react-native": "https://github.com/expo/react-native/archive/sdk-39.0.4.tar.gz",
"react-native-elements": "^3.0.0-alpha.1",
"react-native-gesture-handler": "^1.8.0",
"react-native-image-base64": "^0.1.4",
"react-native-image-crop-picker": "^0.35.1",
"react-native-reanimated": "^1.13.1",
"react-native-screens": "^2.13.0",
"react-native-svg": "12.1.0",
"react-native-web": "~0.13.12",
"react-native-web-webview": "^1.0.2",
"react-native-webview": "^10.10.2",
"react-navigation": "^4.4.3"
"react-navigation": "^4.4.3",
"rn-nodeify": "^10.2.0"
},
"devDependencies": {
"@babel/core": "~7.9.0",
"@react-navigation/native": "^5.8.6",
"file-url": "^3.0.0",
"react-native-fs": "^2.16.6",
"react-native-vector-icons": "^7.1.0"
},
"private": true
Expand Down

0 comments on commit f9099cc

Please sign in to comment.