윈도우에서 WEB server (IIS ) 구동해 놓고 아래와 같은 HTML 구문으로
웹서버 root에 저장하여 웹주소로 연결하여 보면 사물인식을 잘 동작한다.
//---------------------------<index.html 시작>----------------------------------
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>ESP32-CAM TensorflowJS</title>
<style>
body {
font-family: 'PT Sans', sans-serif;
background-color: #dde2e1;
margin: 0;
color: #636060;
line-height: 1.6;
}
.container {
max-width: 1180px;
text-align: center;
margin: 0 auto;
padding: 0 3rem;
}
.btn {
padding: 1rem;
color: #fff;
display: inline-block;
background: red;
margin-bottom: 1rem;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet"></script>
<script>
function classifyImg() {
const img = document.getElementById('img1');
const r = document.getElementById('results');
r.innerHTML = '';
const captureUrl = 'http://172.30.1.49/capture?t=' + Math.random(); // 자신의 카메라 IP를 입력한다.
img.crossOrigin = 'anonymous';
img.xxonload = () => {
mobilenet.load().then(model => {
model.classify(img).then(predictions => {
predictions.forEach(pred => {
r.innerHTML += `<b>${pred.className}</b> - ${pred.probability.toFixed(3)}<br/>`;
});
});
});
};
img.src = captureUrl;
}
</script>
</head>
<body>
<div class="container">
<h2>TensorflowJS with ESP32-CAM</h2>
<section>
<img id="img1" width="320" height="200" src="http://172.30.1.49:81/stream" crossorigin="anonymous" style="border:1px solid red"/> // 자신의 카메라 IP를 입력한다. 포트번호는 카메라 프로그램의 시리얼모니터링을 해보면 나타난다. (프로그램되어있음)
<div id="results"></div>
</section>
<section>
<a href="#" class="btn" xxonclick="classifyImg()">Classify the image</a>
</section>
<section id="i"></section>
</div>
</body>
</html></html>
//---------------------------------< 소스끝 >----------------------------------------
결과 화면은 다음과 같다.