-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
257 lines (257 loc) · 7.73 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="author" content="guidons">
<link rel="Shortcut Icon" href="./res/code.svg" type="image/x-icon"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>智能车模拟器</title>
<style type="text/css">
body {
margin: 0;
}
div.container {
height: 100vh;
display: flex;
flex-direction: row;
}
div.container > div.left {
width: 70%;
overflow: hidden;
resize: horizontal;
min-width: 50%;
}
div.container > div.right {
flex: 1;
overflow: hidden;
}
div.container > div.right > div.right-container {
height: 100%;
display: flex;
flex-direction: column;
}
div.container > div.right > div.right-container > div.top {
height: 50%;
overflow: hidden;
resize: vertical;
}
div.container > div.right > div.right-container > div.bottom {
flex: 1;
overflow-y: scroll;
background-color: black;
border: 3px solid white;
color: green;
}
div#button-container {
display: flex;
flex-direction: row;
background-color: #18191a;
}
button {
height: 30px;
color: black;
border-radius: 3px;
border-width: 3px;
margin: 3px 10px 3px 10px;
outline: none;
font-family: auto;
font-size: 17px;
text-align: center;
cursor: pointer;
flex: auto;
}
button#b1 {
background-color: rgb(50 115 220);
}
button#b2 {
background-color: rgb(170 0 34);
}
button#b3 {
background-color: rgb(152 121 0);
}
button#b1:hover {
background-color: white;
}
button#b2:hover {
background-color: white;
}
button#b3:hover {
background-color: white;
}
div#echarts {
position: absolute;
bottom: 0px;
left: 10px;
/* border: 3px solid #888888;
border-radius: 8%;
opacity: 0.6; */
}
canvas#fpv, canvas#fpv_img {
position: absolute;
bottom: 10px;
z-index: 10;
}
</style>
<script src="./js/libs/three/three.min.js"></script>
<script src="./js/libs/three/webgl-check.min.js"></script>
<script src="./js/libs/echarts/echarts.min.js"></script>
<script src="./js/libs/three/stat.min.js"></script>
<script src="./js/libs/ace/ace.js"></script>
<script src="./js/libs/ace/ext-language_tools.js"></script>
<script src="./js/libs/three/lil-gui.min.js"></script>
<script src="./js/libs/cannon/cannon.min.js"></script>
<script src="./js/libs/three/OrbitControls.min.js"></script>
<script src="./js/libs/three/TransformControls.min.js"></script>
</head>
<body>
<div class="container">
<input type="file" id="file" accept="image/*" onchange="control.get_path()" hidden>
<input type="file" id="model" accept="application/json" onchange="control.get_model()" hidden>
<input type="file" id="weights" accept="application/octet-stream" onchange="control.get_weights()" hidden>
<input type="file" id="label" accept="text/plain" onchange="control.get_label()" hidden>
<a id="download" download="download.png" hidden></a>
<div class="left">
<canvas id="fpv" width="320px" height="240px"></canvas>
<canvas id="fpv_img" width="320px" height="240px" hidden></canvas>
<div id="echarts"></div>
</div>
<div class="right">
<div class="right-container">
<div class="top" id="editor">// // 导入tensorflow.js
// importScripts("https://cdn.staticfile.org/tensorflow/3.14.0/tf.min.js")
// var model;
// const list = get_label();
// function preprocess(imageTensor) {
// const widthToHeight = imageTensor.shape[1] / imageTensor.shape[0];
// let squareCrop;
// if (widthToHeight > 1) {
// const heightToWidth = imageTensor.shape[0] / imageTensor.shape[1];
// const cropTop = (1-heightToWidth) / 2;
// const cropBottom = 1 - cropTop;
// squareCrop = [[cropTop, 0, cropBottom, 1]];
// } else {
// const cropLeft = (1-widthToHeight) / 2;
// const cropRight = 1 - cropLeft;
// squareCrop = [[0, cropLeft, 1, cropRight]];
// }
// // Expand image input dimensions to add a batch dimension of size 1.
// const crop = tf.image.cropAndResize(
// tf.expandDims(imageTensor), squareCrop, [0], [224, 224]);
// return crop.div(255);
// }
// async function setup(){
// print("开始");
// // 设置loop函数为1000ms执行一次
// loop_ms(1000);
// // 加载模型
// model = await tf.loadGraphModel(tf.io.browserFiles(get_model()));
// }
// // loop函数会周期执行
// function loop(){
// // ImageData转为Tensor
// img = tf.browser.fromPixels(get_img())
// // 预处理
// pix = preprocess(img);
// // 预测
// const res = model.predict(pix).dataSync();
// res.forEach((score, pred) => {
// if (score > 0.5) print(list[pred]);
// })
// }
let P = 2.5, D = 1;
let speed = 100, last_error = 0, last_center = -1;
// setup函数仅执行一次,完成初始化
function setup(){
print("开始");
// 设置loop函数为100ms执行一次
loop_ms(100);
}
// loop函数会周期执行
function loop(){
// 获取图片ImageData,并转换为opencv格式
let src = img2Mat(get_img());
// 创建一个图像
let dst = new cv.Mat();
// 转换灰度图
cv.cvtColor(src, dst, cv.COLOR_RGBA2GRAY);
// 自适应阈值二值化
cv.adaptiveThreshold(dst, dst, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 3, 2);
// 裁剪ROI图像
src = dst.roi(new cv.Rect(0, dst.rows/3, dst.cols, 10));
let left = 0, right = src.cols-1, center = src.cols/2;
if (last_center == -1) {
last_center = center;
}
// 左右边界搜索
for (let i = last_center; i < src.cols; i++) {
if (src.ucharAt(5, i) == 0){
right = i;
break;
}
}
for (let i = last_center; i >= 0; i--) {
if (src.ucharAt(5, i) == 0){
left = i;
break;
}
}
// 误差计算
let error = left + 80 - center;
// pid示波器显示
pid_show(error, 0);
// pid计算
let duty = P * error + D * (error - last_error);
// 舵机限幅
if (duty > 500) {
duty = 500;
} else if (duty < -500) {
duty = -500;
}
// 左右电机舵机PWM输出
if (right - left < 220) {
control(speed, speed, duty);
last_error = error;
last_center = center;
}
// 转换为RGBA图像
cv.cvtColor(dst, dst, cv.COLOR_GRAY2RGBA);
// 画线
cv.line(dst, new cv.Point(left, dst.rows/3-5), new cv.Point(left, dst.rows/3+15), new cv.Scalar(255, 0, 0, 255), 2);
cv.line(dst, new cv.Point(right, dst.rows/3-5), new cv.Point(right, dst.rows/3+15), new cv.Scalar(0, 255, 0, 255), 2);
cv.rectangle(dst,{x:0,y:dst.rows/3},{x:dst.cols,y:dst.rows/3+10},[0,0,255,255]);
// 显示图像
img_show(dst);
// 手动释放内存
src.delete();
dst.delete();
} </div>
<div id="button-container">
<button id='b1'>运行</button>
<button id='b2'>停止</button>
<button id='b3' onclick="document.querySelector('.bottom').innerText =''">清除</button>
</div>
<div class="bottom">
</div>
</div>
</div>
</div>
<script>
if (/Android|iPhone|iPad|iPod|BlackBerry|webOS|Windows Phone|SymbianOS|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
document.body.innerHTML = '';
let msg = document.createElement('h1');
msg.innerHTML = '请使用PC端打开';
msg.style.textAlign = 'center';
msg.style.color = 'red';
document.body.appendChild(msg);
} else {
if (WEBGL.isWebGLAvailable()) {
let script = document.createElement("script");
script.setAttribute('src', './js/app.js');
document.body.appendChild(script);
} else {
document.body.innerHTML = '';
document.body.appendChild(WEBGL.getWebGLErrorMessage());
}
}
</script>
</body>
</html>