1
+ import * as p5 from 'p5'
2
+ import "p5/lib/addons/p5.dom" ;
3
+ // https://www.youtube.com/watch?v=yNkAuWz5lnY
4
+ // https://github.com/justadudewhohacks/face-api.js/tree/master/weights
5
+ // https://school.geekwall.in/p/Hy29kFEGm/face-recognition-in-the-browser-with-tensorflow-js-javascript
6
+ // https://nanonets.com/blog/object-detection-tensorflow-js/
7
+ import * as cocoSsd from '@tensorflow-models/coco-ssd' ;
8
+
9
+
10
+ export default function sketch ( p ) {
11
+
12
+ let capture = null ;
13
+ let cocossdModel = null ;
14
+
15
+ let drawText = "" ;
16
+ let drawings = [ ] ;
17
+
18
+
19
+ function showResults ( results ) {
20
+
21
+ console . log ( "Results : " , JSON . stringify ( results ) ) ;
22
+ const id = capture . id ( ) ;
23
+
24
+ // drawings.push(results);
25
+ drawings = results ;
26
+ p . redraw ( ) ;
27
+ // console.log("Id : ", id);
28
+ setTimeout ( ( ) => {
29
+ cocossdModel . detect ( document . getElementById ( id ) ) . then ( showResults ) ;
30
+ } , 1000 )
31
+ }
32
+
33
+ p . setup = function ( ) {
34
+
35
+ p . createCanvas ( 1280 , 720 ) ;
36
+ const constraints = {
37
+ video : {
38
+ mandatory : {
39
+ minWidth : 1280 ,
40
+ minHeight : 720
41
+ } ,
42
+ optional : [ { maxFrameRate : 10 } ]
43
+ } ,
44
+ audio : false
45
+ } ;
46
+
47
+ capture = p . createCapture ( constraints , ( ) => {
48
+ } ) ;
49
+ capture . id ( "video_element" ) ;
50
+ capture . size ( 1280 , 720 ) ;
51
+ capture . hide ( ) ;
52
+
53
+ try {
54
+ cocoSsd . load ( ) . then ( ( model ) => {
55
+ try {
56
+ cocossdModel = model ;
57
+ const id = capture . id ( ) ;
58
+ console . log ( "Id : " , id ) ;
59
+ console . log ( ": Detecting model details : " ) ;
60
+ model
61
+ . detect ( document . getElementById ( id ) )
62
+ . then ( showResults )
63
+ . catch ( ( e ) => {
64
+ console . log ( "Exception : " , e ) ;
65
+ } )
66
+ } catch ( e ) {
67
+ console . log ( e ) ;
68
+ }
69
+
70
+ } )
71
+ } catch ( e ) {
72
+ console . log ( e ) ;
73
+ }
74
+
75
+ } ;
76
+
77
+ p . draw = ( ) => {
78
+ p . background ( 255 ) ;
79
+ p . image ( capture , 0 , 0 ) ;
80
+ // p.fill(255, 0, 0);
81
+ p . fill ( 0 , 0 , 0 , 0 ) ;
82
+
83
+ // const results = drawings.pop();
84
+ // if(results && results.length > 0) {
85
+ // const drawing = drawings.pop();
86
+
87
+ drawings . map ( ( drawing ) => {
88
+ if ( drawing ) {
89
+ p . textSize ( 20 ) ;
90
+ p . strokeWeight ( 1 ) ;
91
+ const textX = drawing . bbox [ 0 ] + drawing . bbox [ 2 ] ;
92
+ const textY = drawing . bbox [ 1 ] + drawing . bbox [ 3 ] ;
93
+
94
+
95
+ const confidenetext = "Confidence: " + drawing . score . toFixed ( 1 ) ;
96
+ const textWidth = p . textWidth ( confidenetext ) ;
97
+
98
+ const itemTextWidth = p . textWidth ( drawing . class ) ;
99
+ p . text ( drawing . class , textX - itemTextWidth , textY - 50 ) ;
100
+
101
+ p . text ( confidenetext , textX - textWidth , textY - 10 ) ;
102
+ p . strokeWeight ( 4 ) ;
103
+ p . stroke ( 'rgb(100%,0%,10%)' ) ;
104
+ p . rect ( drawing . bbox [ 0 ] , drawing . bbox [ 1 ] , drawing . bbox [ 2 ] , drawing . bbox [ 3 ] ) ;
105
+ }
106
+ } )
107
+ // }
108
+ }
109
+ } ;
0 commit comments