Skip to content

Commit ba65295

Browse files
committed
Face and object detection webcam react
1 parent 6353487 commit ba65295

File tree

6 files changed

+312
-21
lines changed

6 files changed

+312
-21
lines changed

package-lock.json

+63
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6+
"@tensorflow-models/coco-ssd": "^2.0.0",
7+
"@tensorflow/tfjs-converter": "^1.2.9",
8+
"@tensorflow/tfjs-core": "^1.2.9",
9+
"p5": "^0.9.0",
610
"react": "^16.9.0",
711
"react-dom": "^16.9.0",
12+
"react-p5-wrapper": "^2.0.0",
813
"react-scripts": "3.1.2"
914
},
1015
"scripts": {

src/App.js

+9-21
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,13 @@ import React from 'react';
22
import logo from './logo.svg';
33
import './App.css';
44

5-
function App() {
6-
return (
7-
<div className="App">
8-
<header className="App-header">
9-
<img src={logo} className="App-logo" alt="logo" />
10-
<p>
11-
Edit <code>src/App.js</code> and save to reload.
12-
</p>
13-
<a
14-
className="App-link"
15-
href="https://reactjs.org"
16-
target="_blank"
17-
rel="noopener noreferrer"
18-
>
19-
Learn React
20-
</a>
21-
</header>
22-
</div>
23-
);
24-
}
5+
import Container from './components/Container';
256

26-
export default App;
7+
export default class app extends React.Component {
8+
9+
render() {
10+
return (
11+
<Container />
12+
)
13+
}
14+
}

src/components/Container.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
import P5Wrapper from 'react-p5-wrapper';
3+
import sampleSketch from './sketch/sample';
4+
5+
6+
export default class Container extends React.Component {
7+
8+
constructor(props) {
9+
super(props);
10+
}
11+
12+
render() {
13+
return (
14+
<P5Wrapper sketch={sampleSketch} />
15+
)
16+
}
17+
}
+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)