firebaser यहाँ
अपने कार्य की तैनाती वास्तव में मैं क्या सामान्य रूप से के लिए इंतजार करने को तैयार हूँ की तुलना में अधिक समय लगता है। हम इसे सुधारने के लिए कड़ी मेहनत कर रहे हैं और (जैसा कि ब्रेंडन ने कहा) स्थानीय एमुलेटर पर काम कर रहे हैं।
लेकिन इस पल के लिए, मैं ज्यादातर अपने वास्तविक व्यापार तर्क को पहले एक अलग नोड स्क्रिप्ट में लिखता हूं। इस तरह से मैं node speech.js
के साथ स्थानीय कमांड प्रॉम्प्ट से इसका परीक्षण कर सकता हूं। एक बार जब मैं संतुष्ट हूं कि फ़ंक्शन काम करता है, तो मैं इसे अपनी वास्तविक फ़ंक्शन फ़ाइल में कॉपी/पेस्ट करता हूं या (बेहतर) speech
मॉड्यूल को अपनी फ़ंक्शंस फ़ाइल में आयात करता हूं और इसे वहां से आमंत्रित करता हूं।
एक संक्षिप्त उदाहरण है कि मैं जल्दी खोद गया था जब मैं क्लाउड विजन एपीआई का उपयोग करके टेक्स्ट निष्कर्षण तारों में था। मुझे लगता है कि होता है एक फ़ाइल ocr.js
कहा जाता है:
var fetch = require('node-fetch');
function extract_text(url, gcloud_authorization) {
console.log('extract_text from image '+url+' with authorization '+gcloud_authorization);
return fetch(url).then(function(res) {
return res.buffer();
}).then(function(buffer) {
return fetch('https://vision.googleapis.com/v1/images:annotate?key='+gcloud_authorization, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
"requests":[
{
"image":{
"content": buffer.toString('base64')
},
"features":[
{
"type":"TEXT_DETECTION",
"maxResults":1
}
]
}
]
})
});
}).then(function(res) {
var json = res.json();
if (res.status >= 200 && res.status < 300) {
return json;
} else {
return json.then(Promise.reject.bind(Promise));
}
}).then(function(json) {
if (json.responses && json.responses.length && json.responses[0].error) {
return Promise.reject(json.responses[0].error);
}
return json.responses[0].textAnnotations[0].description;
});
}
if (process.argv.length > 2) {
// by passing the image URL and gcloud access token, you can test this module
process.argv.forEach(a => console.log(a));
extract_text(
process.argv[2], // image URL
process.argv[3] // gcloud access token or API key
).then(function(description) {
console.log(description);
}).catch(function(error) {
console.error(error);
});
}
exports.extract_text = extract_text;
और फिर मेरे कार्य index.js में, मेरे पास है:
var functions = require('firebase-functions');
var fetch = require('node-fetch');
var ocr = require('./ocr.js');
exports.ocr = functions.database().path('/messages/{room}/{id}').onWrite(function(event) {
console.log('OCR triggered for /messages/'+event.params.room+'/'+event.params.id);
if (!event.data || !event.data.exists()) return;
if (event.data.ocr) return;
if (event.data.val().text.indexOf("https://firebasestorage.googleapis.com/") !== 0) return; // only OCR images
console.log(JSON.stringify(functions.env));
return ocr.extract_text(event.data.val().text, functions.env.googlecloud.apikey).then(function(text) {
return event.data.adminRef.update({ ocr: text });
});
});
तो तुम यह पिछले फ़ाइल देख सकते हैं के रूप में वास्तव में है तारों के बारे में "कार्यकर्ता विधि" ocr.extract_text
डेटाबेस स्थान पर।
नोट यह कुछ समय पहले एक परियोजना है, इसलिए कुछ वाक्यविन्यास (अधिकतर functions.env
भाग) थोड़ा बदल सकता है।
स्रोत
2017-03-11 00:41:06
कोई अपडेट? धन्यवाद। –
https कार्यों के लिए अल्फा संस्करण फ़ायरबेस एमुलेटर उपलब्ध: https://firebase.google।कॉम/डॉक्स/फ़ंक्शंस/लोकल-एमुलेटर –