-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
165 lines (148 loc) · 4.72 KB
/
index.html
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>shape-detection-api demo</title>
<style>
label {
border: 1px solid black;
padding: 3px;
margin-right: 5px;
cursor: pointer;
}
label:hover {
background: #ccc;
}
</style>
</head>
<body>
<h1>shape-detection-api-demo</h1>
<div id="errorMessage" style="color: red"></div>
<div>
<label>
File Upload/FaceDetector
<input id="file" type="file" accept="image/*" onchange="faceDetector(this.files)" style="display:none;"/>
</label>
<label>
File Upload/BarcodeDetector
<input id="file" type="file" accept="image/*" onchange="barcodeDetector(this.files)" style="display:none;"/>
</label>
<label>
File Upload/TextDetector
<input id="file" type="file" accept="image/*" onchange="textDetector(this.files)" style="display:none;"/>
</label>
</div>
<canvas id="canvas"></canvas>
<div id="result"></div>
</body>
<script>
const errorMessageElement = document.getElementById("errorMessage")
if (window.FaceDetector === undefined) {
errorMessageElement.innerText = `
This demo needs to enable below the flag.
chrome://flags/#enable-experimental-web-platform-features
`
}
const canvas = document.getElementById('canvas')
function resetCanvas(img) {
const { width, height } = img
canvas.width = width
canvas.height = height
const ctx = canvas.getContext('2d')
ctx.lineWidth = 5
ctx.clearRect(0, 0, width, height)
ctx.drawImage(img, 0, 0, width, height)
return ctx
}
const lineColors = ['aqua', 'pink', 'greenyellow', 'lightcyan', 'hotpink']
let count = 0
function drawRect(ctx, x, y, width, height) {
ctx.strokeStyle = lineColors[(count++) % lineColors.length]
ctx.beginPath()
ctx.rect(x, y, width, height)
ctx.stroke()
}
function textDetector(files) {
const file = files[0]
if (!file.type.match(/image.*/)) {
errorMessageElement.innerText = `Only image files are accepted.`
return
}
try {
const reader = new FileReader()
reader.onload = async () => {
const img = new Image()
img.src = reader.result
img.onload = async () => {
ctx = resetCanvas(img)
const textDetector = new TextDetector()
const texts = await textDetector.detect(canvas)
texts.forEach((text) => {
const { boundingBox: { x, y, width, height } } = text
drawRect(ctx, x, y, width, height)
console.log(text)
document.getElementById('result').innerText = text.rawValue
})
}
}
reader.readAsDataURL(file)
} catch (e) {
console.error('Text detection failed:', e)
}
}
function barcodeDetector(files) {
const file = files[0]
if (!file.type.match(/image.*/)) {
errorMessageElement.innerText = `Only image files are accepted.`
return
}
try {
const reader = new FileReader()
reader.onload = async () => {
const img = new Image()
img.src = reader.result
img.onload = async () => {
ctx = resetCanvas(img)
const barcodeDetector = new BarcodeDetector()
const barcodes = await barcodeDetector.detect(canvas)
barcodes.forEach((barcode) => {
console.log(barcode)
document.getElementById('result').innerText = barcode.rawValue
})
}
}
reader.readAsDataURL(file)
} catch (e) {
console.error('Barcode detection failed:', e)
}
}
function faceDetector(files) {
const file = files[0]
if (!file.type.match(/image.*/)) {
errorMessageElement.innerText = `Only image files are accepted.`
return
}
try {
const reader = new FileReader()
reader.onload = async () => {
const img = new Image()
img.src = reader.result
img.onload = async () => {
ctx = resetCanvas(img)
const faceDetector = new FaceDetector()
const faces = await faceDetector.detect(canvas)
faces.forEach((face) => {
console.log(face)
const { boundingBox: { x, y, width, height } } = face
drawRect(ctx, x, y, width, height)
})
document.getElementById('result').innerText = faces.length
}
}
reader.readAsDataURL(file)
} catch (e) {
console.error('Face detection failed:', e)
}
}
</script>
</html>