index.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>KNN 图像分类器 - 图片上传 (CDN)</title>
  7. <style>
  8. body {
  9. font-family: sans-serif;
  10. padding: 10px;
  11. display: flex;
  12. flex-direction: column;
  13. align-items: flex-start; /* 左对齐 */
  14. }
  15. button {
  16. padding: 8px 15px;
  17. margin-top: 5px; /* 添加上边距 */
  18. margin-right: 5px;
  19. cursor: pointer;
  20. }
  21. #image-preview-container {
  22. margin-top: 15px;
  23. margin-bottom: 15px;
  24. min-height: 227px; /* 保持最小高度以便布局 */
  25. border: 1px dashed #ccc;
  26. padding: 5px;
  27. display: flex; /* 使用 flex 布局 */
  28. justify-content: center; /* 水平居中 */
  29. align-items: center; /* 垂直居中 */
  30. width: 227px; /* 固定宽度 */
  31. height: 227px; /* 固定高度 */
  32. background-color: #f8f8f8;
  33. overflow: hidden; /* 隐藏超出部分 */
  34. }
  35. #image-preview {
  36. max-width: 100%;
  37. max-height: 100%;
  38. display: block;
  39. object-fit: contain; /* 保持图片比例 */
  40. }
  41. #controls-container {
  42. margin-top: 15px;
  43. }
  44. .class-controls {
  45. margin-bottom: 10px;
  46. display: flex;
  47. align-items: center;
  48. }
  49. .class-controls span {
  50. margin-left: 10px;
  51. font-size: 0.9em;
  52. color: #555;
  53. min-width: 150px; /* 给状态文本留出空间 */
  54. }
  55. #status {
  56. margin-top: 15px;
  57. font-weight: bold;
  58. color: navy;
  59. }
  60. </style>
  61. </head>
  62. <body>
  63. <h1>KNN 图像分类器 - 图片上传</h1>
  64. <p>选择一张图片,然后点击按钮进行识别或训练。</p>
  65. <input type="file" id="file-input" accept="image/*">
  66. <div id="image-preview-container">
  67. <img id="image-preview" src="#" alt="图片预览" style="display: none;"/>
  68. <span id="preview-placeholder">请选择一张图片</span>
  69. </div>
  70. <button id="classify-button" disabled>识别图片</button>
  71. <div id="status">准备就绪。</div>
  72. <div id="controls-container">
  73. <h2>训练控件</h2>
  74. <!-- 训练按钮和状态将由 JS 动态创建 -->
  75. </div>
  76. <!-- TensorFlow.js 库 (必须先加载核心库) -->
  77. <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest/dist/tf.min.js"></script>
  78. <!-- MobileNet 模型 -->
  79. <script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet@latest/dist/mobilenet.min.js"></script>
  80. <!-- KNN 分类器 -->
  81. <script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/knn-classifier@latest/dist/knn-classifier.min.js"></script>
  82. <!-- 我们的应用逻辑 -->
  83. <script src="app.js"></script>
  84. </body>
  85. </html>