index.html 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <template>
  2. <div>
  3. <video ref="video" width="640" height="480" autoplay></video>
  4. <button @click="capture">拍照</button>
  5. <canvas ref="canvas" width="640" height="480" style="display:none;"></canvas>
  6. <div v-if="results.length > 0">
  7. <h3>请选择最相似的标准图片:</h3>
  8. <div v-for="(result, index) in results" :key="index">
  9. <p>{{ result.name }} - 相似度: {{ result.similarity }}%</p>
  10. <button @click="selectImage(result.name)">选择</button>
  11. </div>
  12. </div>
  13. <div v-if="selectedData">
  14. <h3>选择的图片数据:</h3>
  15. <ul>
  16. <li v-for="(item, index) in selectedData" :key="index">{{ item }}</li>
  17. </ul>
  18. </div>
  19. </div>
  20. </template>
  21. <script>
  22. export default {
  23. data() {
  24. return {
  25. results: [],
  26. selectedData: null
  27. };
  28. },
  29. mounted() {
  30. this.startCamera();
  31. },
  32. methods: {
  33. startCamera() {
  34. navigator.mediaDevices.getUserMedia({ video: true })
  35. .then(stream => {
  36. this.$refs.video.srcObject = stream;
  37. })
  38. .catch(err => {
  39. console.error("Error accessing the camera: ", err);
  40. });
  41. },
  42. capture() {
  43. const canvas = this.$refs.canvas;
  44. const video = this.$refs.video;
  45. canvas.getContext('2d').drawImage(video, 0, 0, 640, 480);
  46. canvas.toBlob(blob => {
  47. const formData = new FormData();
  48. formData.append('file', blob, 'photo.png');
  49. const uploadUrl = 'http://localhost:5500/upload';
  50. fetch(uploadUrl, {
  51. method: 'POST',
  52. body: formData
  53. })
  54. .then(response => response.json())
  55. .then(data => {
  56. this.results = data;
  57. });
  58. }, 'image/png');
  59. },
  60. selectImage(name) {
  61. const selectUrl = 'http://localhost:5500/select';
  62. fetch(selectUrl, {
  63. method: 'POST',
  64. headers: {
  65. 'Content-Type': 'application/json'
  66. },
  67. body: JSON.stringify({ name: name })
  68. })
  69. .then(response => response.json())
  70. .then(data => {
  71. this.selectedData = data;
  72. });
  73. }
  74. }
  75. };
  76. </script>