Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
face
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Кубота
face
Commits
3ca0f4af
Commit
3ca0f4af
authored
Jun 07, 2018
by
vincent
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pad input to square
parent
89e9691e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
12 deletions
+40
-12
index.ts
src/faceDetectionNet/index.ts
+20
-5
index.ts
src/index.ts
+5
-1
utils.ts
src/utils.ts
+15
-6
No files found.
src/faceDetectionNet/index.ts
View file @
3ca0f4af
...
@@ -33,6 +33,20 @@ function fromImageData(input: ImageData[]) {
...
@@ -33,6 +33,20 @@ function fromImageData(input: ImageData[]) {
return
tf
.
cast
(
tf
.
concat
(
imgTensors
,
0
),
'float32'
)
return
tf
.
cast
(
tf
.
concat
(
imgTensors
,
0
),
'float32'
)
}
}
function
padToSquare
(
imgTensor
:
tf
.
Tensor4D
):
tf
.
Tensor4D
{
const
[
_
,
height
,
width
]
=
imgTensor
.
shape
if
(
height
===
width
)
{
return
imgTensor
}
if
(
height
>
width
)
{
const
pad
=
tf
.
fill
([
1
,
height
,
height
-
width
,
3
],
0
)
as
tf
.
Tensor4D
return
tf
.
concat
([
imgTensor
,
pad
],
2
)
}
const
pad
=
tf
.
fill
([
1
,
width
-
height
,
width
,
3
],
0
)
as
tf
.
Tensor4D
return
tf
.
concat
([
imgTensor
,
pad
],
1
)
}
function
getImgTensor
(
input
:
ImageData
|
ImageData
[]
|
number
[])
{
function
getImgTensor
(
input
:
ImageData
|
ImageData
[]
|
number
[])
{
return
tf
.
tidy
(()
=>
{
return
tf
.
tidy
(()
=>
{
...
@@ -44,9 +58,11 @@ function getImgTensor(input: ImageData|ImageData[]|number[]) {
...
@@ -44,9 +58,11 @@ function getImgTensor(input: ImageData|ImageData[]|number[]) {
:
null
:
null
)
)
return
imgDataArray
!==
null
return
padToSquare
(
?
fromImageData
(
imgDataArray
)
imgDataArray
!==
null
:
fromData
(
input
as
number
[])
?
fromImageData
(
imgDataArray
)
:
fromData
(
input
as
number
[])
)
})
})
}
}
...
@@ -71,7 +87,7 @@ export function faceDetectionNet(weights: Float32Array) {
...
@@ -71,7 +87,7 @@ export function faceDetectionNet(weights: Float32Array) {
function
forward
(
input
:
ImageData
|
ImageData
[]
|
number
[])
{
function
forward
(
input
:
ImageData
|
ImageData
[]
|
number
[])
{
return
tf
.
tidy
(
return
tf
.
tidy
(
()
=>
forwardTensor
(
getImgTensor
(
input
))
()
=>
forwardTensor
(
padToSquare
(
getImgTensor
(
input
)
))
)
)
}
}
...
@@ -81,7 +97,6 @@ export function faceDetectionNet(weights: Float32Array) {
...
@@ -81,7 +97,6 @@ export function faceDetectionNet(weights: Float32Array) {
maxResults
:
number
=
100
,
maxResults
:
number
=
100
,
):
Promise
<
FaceDetectionNet
.
Detection
[]
>
{
):
Promise
<
FaceDetectionNet
.
Detection
[]
>
{
const
imgTensor
=
getImgTensor
(
input
)
const
imgTensor
=
getImgTensor
(
input
)
const
[
_
,
height
,
width
]
=
imgTensor
.
shape
const
[
_
,
height
,
width
]
=
imgTensor
.
shape
const
{
const
{
...
...
src/index.ts
View file @
3ca0f4af
import
*
as
tf
from
'@tensorflow/tfjs-core'
;
import
{
euclideanDistance
}
from
'./euclideanDistance'
;
import
{
euclideanDistance
}
from
'./euclideanDistance'
;
import
{
faceDetectionNet
}
from
'./faceDetectionNet'
;
import
{
faceDetectionNet
}
from
'./faceDetectionNet'
;
import
{
faceRecognitionNet
}
from
'./faceRecognitionNet'
;
import
{
faceRecognitionNet
}
from
'./faceRecognitionNet'
;
...
@@ -7,7 +9,8 @@ export {
...
@@ -7,7 +9,8 @@ export {
euclideanDistance
,
euclideanDistance
,
faceDetectionNet
,
faceDetectionNet
,
faceRecognitionNet
,
faceRecognitionNet
,
normalize
normalize
,
tf
}
}
export
*
from
'./utils'
export
*
from
'./utils'
\ No newline at end of file
src/utils.ts
View file @
3ca0f4af
...
@@ -23,9 +23,15 @@ export function round(num: number) {
...
@@ -23,9 +23,15 @@ export function round(num: number) {
return
Math
.
floor
(
num
*
100
)
/
100
return
Math
.
floor
(
num
*
100
)
/
100
}
}
export
type
Dimensions
=
{
width
:
number
height
:
number
}
export
function
drawMediaToCanvas
(
export
function
drawMediaToCanvas
(
canvasArg
:
string
|
HTMLCanvasElement
,
canvasArg
:
string
|
HTMLCanvasElement
,
mediaArg
:
string
|
HTMLImageElement
|
HTMLVideoElement
mediaArg
:
string
|
HTMLImageElement
|
HTMLVideoElement
,
dims
?:
Dimensions
):
CanvasRenderingContext2D
{
):
CanvasRenderingContext2D
{
const
canvas
=
getElement
(
canvasArg
)
const
canvas
=
getElement
(
canvasArg
)
const
media
=
getElement
(
mediaArg
)
const
media
=
getElement
(
mediaArg
)
...
@@ -37,21 +43,24 @@ export function drawMediaToCanvas(
...
@@ -37,21 +43,24 @@ export function drawMediaToCanvas(
throw
new
Error
(
'drawMediaToCanvas - expected media to be of type: HTMLImageElement | HTMLVideoElement'
)
throw
new
Error
(
'drawMediaToCanvas - expected media to be of type: HTMLImageElement | HTMLVideoElement'
)
}
}
canvas
.
width
=
media
.
width
const
{
width
,
height
}
=
dims
||
media
canvas
.
height
=
media
.
height
canvas
.
width
=
width
canvas
.
height
=
height
const
ctx
=
getContext2dOrThrow
(
canvas
)
const
ctx
=
getContext2dOrThrow
(
canvas
)
ctx
.
drawImage
(
media
,
0
,
0
,
media
.
width
,
media
.
height
)
ctx
.
drawImage
(
media
,
0
,
0
,
width
,
height
)
return
ctx
return
ctx
}
}
export
function
mediaToImageData
(
media
:
HTMLImageElement
|
HTMLVideoElement
):
ImageData
{
export
function
mediaToImageData
(
media
:
HTMLImageElement
|
HTMLVideoElement
,
dims
?:
Dimensions
):
ImageData
{
if
(
!
(
media
instanceof
HTMLImageElement
||
media
instanceof
HTMLVideoElement
))
{
if
(
!
(
media
instanceof
HTMLImageElement
||
media
instanceof
HTMLVideoElement
))
{
throw
new
Error
(
'mediaToImageData - expected media to be of type: HTMLImageElement | HTMLVideoElement'
)
throw
new
Error
(
'mediaToImageData - expected media to be of type: HTMLImageElement | HTMLVideoElement'
)
}
}
const
ctx
=
drawMediaToCanvas
(
document
.
createElement
(
'canvas'
),
media
)
const
ctx
=
drawMediaToCanvas
(
document
.
createElement
(
'canvas'
),
media
)
return
ctx
.
getImageData
(
0
,
0
,
media
.
width
,
media
.
height
)
const
{
width
,
height
}
=
dims
||
media
return
ctx
.
getImageData
(
0
,
0
,
width
,
height
)
}
}
export
function
mediaSrcToImageData
(
export
function
mediaSrcToImageData
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment