index.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>LineDetect Alpha_v1.02</title>
  7. <style>
  8. body {
  9. font-family: Arial, sans-serif;
  10. margin: 0;
  11. padding: 0;
  12. display: flex;
  13. justify-content: center;
  14. align-items: center;
  15. height: 100vh;
  16. background-color: #f0f4f8;
  17. }
  18. .container {
  19. text-align: center;
  20. width: 80%;
  21. max-width: 2000px;
  22. background-color: #ffffff;
  23. border-radius: 8px;
  24. padding: 40px;
  25. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  26. overflow: auto; /* ?????? */
  27. }
  28. h1 {
  29. color: #333;
  30. font-size: 24px;
  31. margin-bottom: 20px;
  32. }
  33. h2.subtitle {
  34. color: #666;
  35. font-size: 18px;
  36. margin-bottom: 20px;
  37. }
  38. input[type="file"] {
  39. margin-bottom: 20px;
  40. padding: 10px;
  41. font-size: 16px;
  42. border: 1px solid #ccc;
  43. border-radius: 4px;
  44. }
  45. button {
  46. padding: 12px 20px;
  47. background-color: #4CAF50;
  48. color: white;
  49. border: none;
  50. border-radius: 4px;
  51. font-size: 16px;
  52. cursor: pointer;
  53. transition: background-color 0.3s;
  54. }
  55. button:hover {
  56. background-color: #45a049;
  57. }
  58. .result {
  59. visibility: hidden; /* ??????? */
  60. display: flex;
  61. flex-direction: column;
  62. align-items: center;
  63. margin-top: 40px;
  64. max-height: 60vh; /* ?????? */
  65. overflow-y: auto; /* ??????? */
  66. }
  67. .result img {
  68. max-width: 100%; /* ????????? */
  69. height: auto; /* ??????????? */
  70. object-fit: cover;
  71. border-radius: 8px;
  72. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  73. margin-bottom: 20px;
  74. }
  75. .uploaded-image {
  76. width: 512px; /* ???? */
  77. height: 512px; /* ???? */
  78. object-fit: cover; /* ???????????????? */
  79. border-radius: 8px;
  80. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  81. }
  82. </style>
  83. </head>
  84. <body>
  85. <div class="container">
  86. <h1>LineDetect Alpha_v1.02</h1> <!-- ???? -->
  87. <h2 class="subtitle">请上传图片</h2> <!-- ????? -->
  88. <input type="file" id="fileInput" accept="image/*" />
  89. <button onclick="uploadImage()">上传</button> <!-- ?????? -->
  90. <div class="result">
  91. <div>
  92. <h2>原图</h2>
  93. <img id="uploadedImage" class="uploaded-image" src="" alt="Uploaded Image" style="display: none;" />
  94. </div>
  95. <div>
  96. <h2>结果</h2>
  97. <img id="resultImage" src="" alt="Prediction Result" style="display: none;" />
  98. </div>
  99. </div>
  100. </div>
  101. <script>
  102. async function uploadImage() {
  103. const fileInput = document.getElementById('fileInput');
  104. const file = fileInput.files[0];
  105. if (!file) {
  106. alert("???????????");
  107. return;
  108. }
  109. // ?? canvas ?????? 512x512
  110. const resizedImageBlob = await resizeImage(file, 512, 512);
  111. // ???????
  112. const uploadedImage = document.getElementById('uploadedImage');
  113. uploadedImage.src = URL.createObjectURL(resizedImageBlob);
  114. uploadedImage.style.display = 'block';
  115. // ???????????
  116. const formData = new FormData();
  117. formData.append("file", resizedImageBlob, file.name); // ??????
  118. const response = await fetch('http://0.0.0.0:808/predict', {
  119. method: 'POST',
  120. body: formData,
  121. });
  122. if (response.ok) {
  123. const resultBlob = await response.blob();
  124. const resultImage = document.getElementById('resultImage');
  125. resultImage.src = URL.createObjectURL(resultBlob);
  126. resultImage.style.display = 'block';
  127. document.querySelector('.result').style.visibility = 'visible'; // ????
  128. } else {
  129. alert("?????");
  130. }
  131. }
  132. /**
  133. * ?? canvas ????
  134. * @param {File} file - ??????
  135. * @param {number} width - ????
  136. * @param {number} height - ????
  137. * @returns {Promise<Blob>} - ???????? Blob
  138. */
  139. async function resizeImage(file, width, height) {
  140. return new Promise((resolve, reject) => {
  141. const img = new Image();
  142. img.onload = () => {
  143. const canvas = document.createElement('canvas');
  144. canvas.width = width;
  145. canvas.height = height;
  146. const ctx = canvas.getContext('2d');
  147. // ????????????
  148. ctx.drawImage(img, 0, 0, width, height);
  149. // ? canvas ??? Blob
  150. canvas.toBlob(blob => {
  151. resolve(blob);
  152. }, file.type || 'image/jpeg');
  153. };
  154. img.onerror = reject;
  155. img.src = URL.createObjectURL(file);
  156. });
  157. }
  158. </script>
  159. </body>
  160. </html>