-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (72 loc) · 2.57 KB
/
index.js
File metadata and controls
81 lines (72 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Example of using API4AI face analyzer.
// Use 'demo' mode just to try api4ai for free. ⚠️ Free demo is rate limited and must not be used in real projects.
//
// Use 'normal' mode if you have an API Key from the API4AI Developer Portal. This is the method that users should normally prefer.
//
// Use 'rapidapi' if you want to try api4ai via RapidAPI marketplace.
// For more details visit:
// https://rapidapi.com/api4ai-api4ai-default/api/face-detection14/details
const MODE = 'demo'
// Your API4AI key. Fill this variable with the proper value if you have one.
const API4AI_KEY = ''
// Your RapidAPI key. Fill this variable with the proper value if you want
// to try api4ai via RapidAPI marketplace.
const RAPIDAPI_KEY = ''
const OPTIONS = {
demo: {
url: 'https://demo.api4ai.cloud/face-analyzer/v1/results',
headers: {}
},
normal: {
url: 'https://api4ai.cloud/face-analyzer/v1/results',
headers: { 'X-API-KEY': API4AI_KEY }
},
rapidapi: {
url: 'https://face-detection14.p.rapidapi.com/v1/results',
headers: { 'X-RapidAPI-Key': RAPIDAPI_KEY }
}
}
document.addEventListener('DOMContentLoaded', function (event) {
const input = document.getElementById('file')
const raw = document.getElementById('raw')
const sectionRaw = document.getElementById('sectionRaw')
const parsed = document.getElementById('parsed')
const sectionParsed = document.getElementById('sectionParsed')
const spinner = document.getElementById('spinner')
input.addEventListener('change', (event) => {
const file = event.target.files[0]
if (!file) {
return false
}
sectionRaw.hidden = true
sectionParsed.hidden = true
spinner.hidden = false
// Preapare request.
const form = new FormData()
form.append('image', file)
const requestOptions = {
method: 'POST',
body: form,
headers: OPTIONS[MODE].headers
}
// Make request.
fetch(OPTIONS[MODE].url, requestOptions)
.then(response => response.json())
.then(function (response) {
// Print raw response.
raw.textContent = JSON.stringify(response, undefined, 2)
sectionRaw.hidden = false
// Parse response and print detected faces count.
const facesCount = response.results[0].entities[0].objects.length
parsed.textContent = facesCount ? `${facesCount} face(s) detected.` : 'No faces detected.'
sectionParsed.hidden = false
})
.catch(function (error) {
// Error can be handled here.
console.error(error)
})
.then(function () {
spinner.hidden = true
})
})
})