김태영

add temp image file & request image address by ajax

...@@ -3,7 +3,18 @@ var router = express.Router(); ...@@ -3,7 +3,18 @@ var router = express.Router();
3 3
4 /* GET home page. */ 4 /* GET home page. */
5 router.get('/', function(req, res, next) { 5 router.get('/', function(req, res, next) {
6 - res.render('index', { title: 'Express' }); 6 + res.render('index', { title: 'MWD' , url: "/images/1.jpg"});
7 +});
8 +
9 +/* predict() 의 결과를 querystring으로 받아 그것에 맞는 이미지파일을 보내준다. */
10 +/* /data?id=n 의 형태로 id값을 전달해 준다. */
11 +router.get('/data', function(req, res, next){
12 +
13 + id = req.query.id;
14 + console.log(id);
15 + //보내주기.
16 + res.send(data);
7 }); 17 });
8 18
9 module.exports = router; 19 module.exports = router;
20 +
......
...@@ -7,14 +7,19 @@ ...@@ -7,14 +7,19 @@
7 <body> 7 <body>
8 <h1><%= title %></h1> 8 <h1><%= title %></h1>
9 <p>Welcome to <%= title %></p> 9 <p>Welcome to <%= title %></p>
10 -
11 <!--https://teachablemachine.withgoogle.com/ 를 통해 학습시키고 얻은 스켈레톤 코드 --> 10 <!--https://teachablemachine.withgoogle.com/ 를 통해 학습시키고 얻은 스켈레톤 코드 -->
11 +
12 <div>Teachable Machine Image Model</div> 12 <div>Teachable Machine Image Model</div>
13 <button type="button" onclick="init()">Start</button> 13 <button type="button" onclick="init()">Start</button>
14 <div id="webcam-container"></div> 14 <div id="webcam-container"></div>
15 <div id="label-container"></div> 15 <div id="label-container"></div>
16 + <div id="check_image">
17 + <img src="/images/1.jpg">
18 + </div>
19 +
16 <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.3.1/dist/tf.min.js"></script> 20 <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.3.1/dist/tf.min.js"></script>
17 <script src="https://cdn.jsdelivr.net/npm/@teachablemachine/image@0.8/dist/teachablemachine-image.min.js"></script> 21 <script src="https://cdn.jsdelivr.net/npm/@teachablemachine/image@0.8/dist/teachablemachine-image.min.js"></script>
22 + <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
18 <script type="text/javascript"> 23 <script type="text/javascript">
19 // More API functions here: 24 // More API functions here:
20 // https://github.com/googlecreativelab/teachablemachine-community/tree/master/libraries/image 25 // https://github.com/googlecreativelab/teachablemachine-community/tree/master/libraries/image
...@@ -24,6 +29,9 @@ ...@@ -24,6 +29,9 @@
24 29
25 let model, webcam, labelContainer, maxPredictions; 30 let model, webcam, labelContainer, maxPredictions;
26 31
32 + //predic이전 값을 기억해주는 전역변수. -999로 초기화.
33 + var last_result_predict=-999;
34 +
27 // Load the image model and setup the webcam 35 // Load the image model and setup the webcam
28 async function init() { 36 async function init() {
29 const modelURL = URL + "model.json"; 37 const modelURL = URL + "model.json";
...@@ -61,11 +69,56 @@ ...@@ -61,11 +69,56 @@
61 async function predict() { 69 async function predict() {
62 // predict can take in an image, video or canvas html element 70 // predict can take in an image, video or canvas html element
63 const prediction = await model.predict(webcam.canvas); 71 const prediction = await model.predict(webcam.canvas);
72 +
73 + //나중에 모두 하고나면 이부분은 항목별 일치확률을 보여주는 것으로 제거해도 무방.
64 for (let i = 0; i < maxPredictions; i++) { 74 for (let i = 0; i < maxPredictions; i++) {
65 const classPrediction = 75 const classPrediction =
66 prediction[i].className + ": " + prediction[i].probability.toFixed(2); 76 prediction[i].className + ": " + prediction[i].probability.toFixed(2);
67 labelContainer.childNodes[i].innerHTML = classPrediction; 77 labelContainer.childNodes[i].innerHTML = classPrediction;
68 } 78 }
79 +
80 + //ajax로 요청을 보낼 값 초기값 -1
81 + var predict_id = -1;
82 +
83 + //해당 항목의 일치확률이 몇프로 이상이어야 해당 항목이라 판단할지 결정하는 변수
84 + var check_probablity=0.95;
85 + //0: 완벽 1: 안씀 2: 턱스크 3: 입스크
86 +
87 + if(prediction[0].probability.toFixed(2)>check_probablity){
88 + predict_id=0;
89 + }
90 + else if(prediction[1].probability.toFixed(2)>check_probablity){
91 + predict_id=1;
92 + }
93 + else if(prediction[2].probability.toFixed(2)>check_probablity){
94 + predict_id=2;
95 + }
96 + else if(prediction[3].probability.toFixed(2)>check_probablity){
97 + predict_id=3;
98 + }
99 +
100 + //이전 결과와 다를떄만 ajax요청
101 + //last_result_predict는 전역변수. 초기값 -999로 설정되어있다.
102 + //이것 없으면 계속 요청이 보내진다.
103 + if(last_result_predict!=predict_id){
104 +
105 + //ajax로 서버에 그 id에 해당하는 이미지주소를 달라고 요청한다.
106 + if(predict_id > 0){
107 + //last_result_predict값을 지금 결과로 나옴 predict로 초기화해준다.
108 + last_result_predict = predict_id;
109 + $(function() {
110 + $.ajax({
111 + type: 'GET',
112 + datatype:'json',
113 + url: '/data?id='+predict_id,
114 + success: function(result) {
115 + //결과값 확인
116 + console.log(result);
117 + }
118 + });
119 + })
120 + }
121 + }
69 } 122 }
70 </script> 123 </script>
71 </body> 124 </body>
......