Skip to content

Instantly share code, notes, and snippets.

@jirawatee
Last active May 27, 2019 19:45
Show Gist options
  • Save jirawatee/001e7ae08cb18ff75875a72e211c1237 to your computer and use it in GitHub Desktop.
Save jirawatee/001e7ae08cb18ff75875a72e211c1237 to your computer and use it in GitHub Desktop.
Logo detection in UCL
exports.logoDetection = functions.region(region).runWith(runtimeOpts)
.storage.object()
.onFinalize(async (object) => {
const fileName = object.name // ดึงชื่อไฟล์มา
const userId = fileName.split('.')[0] // แยกชื่อไฟล์ออกมา ซึ่งมันก็คือ userId
// ทำนายโลโกที่อยู่ในภาพด้วย Cloud Vision API
const [result] = await client.logoDetection(`gs://${object.bucket}/${fileName}`);
const logos = result.logoAnnotations;
// เอาผลลัพธ์มาเก็บใน array ซึ่งเป็นโครงสร้างของ Quick Reply
let itemArray = []
logos.forEach(logo => {
if (logo.score >= 0.7) { // ค่าความแม่นยำของการทำนายต้องได้ตั้งแต่ 70% ขึ้นไป
itemArray.push({
type: 'action',
action: {
type: 'postback', // action ประเภท postback
label: logo.description, // ชื่อที่จะแสดงในปุ่ม Quick Reply
data: `team=${logo.description}`, // ส่งข้อมูลทีมกลับไปแบบลับๆ
displayText: logo.description // ชื่อที่จะถูกส่งเข้าห้องแชทหลังจากคลิกปุ่ม Quick Reply
}
});
}
})
// กำหนดตัวแปรมา 2 ตัว
let msg = '';
let quickItems = null;
// ตรวจสอบว่ามีผลลัพธ์การทำนายหรือไม่
if (itemArray.length > 0) {
msg = 'เลือกทีมที่คิดว่าใช่มาหน่อยซิ';
quickItems = { items: itemArray };
} else {
msg = 'ไม่พบโลโกในภาพ ลองส่งรูปมาใหม่ซิ';
quickItems = null;
}
// ส่งข้อความหาผู้ใช้ว่าพบโลโกหรือไม่ พร้อม Quick Reply(กรณีมีผลการทำนาย)
push(userId, msg, quickItems)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment