File size: 6,799 Bytes
7e19f9a
 
b11b450
7e19f9a
 
 
 
 
 
 
7a85ee8
 
7e19f9a
 
0beeee1
 
7a85ee8
 
7e19f9a
 
 
0beeee1
7e19f9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8ffae8
7e19f9a
ece9abb
e8ffae8
 
 
ece9abb
 
e8ffae8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ece9abb
e8ffae8
 
 
 
7e19f9a
e8ffae8
7e19f9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed76384
7a85ee8
 
 
 
 
 
 
7e19f9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
document.addEventListener("DOMContentLoaded", () => {
  let lastUploadedImage = null;
  const dropzone = document.getElementById("dropzone");
  const featureSelect = document.getElementById("feature-select");
  const thinkingText = document.getElementById("thinking-text");

  function handleImageUpload(file) {
    const predictedImagesContainer = document.getElementById("predicted-images");
    predictedImagesContainer.innerHTML = "";

    const predictionContainer = document.getElementById("prediction-container");

    const imageElement = document.createElement("img");
    imageElement.src = "static/loading.gif";
    imageElement.style.height = "100px";
    imageElement.style.width = "100px";
    imageElement.style.margin = 'auto';
    predictionContainer.appendChild(imageElement);

    const selectedFeature = featureSelect.value;
    const formData = new FormData();
    thinkingText.innerText = "";
    formData.append("uploaded-image", file);
    formData.append("feature", selectedFeature);
    lastUploadedImage = file;
    fetch("/guess", {
      method: "POST",
      body: formData,
    })
      .then((response) => response.json())
      .then((data) => {
        console.log(data);
        displayResults(data);
      })
      .catch((error) => {
        console.error("Error uploading image:", error);
      });

    const reader = new FileReader();
    reader.onloadend = () => {
      const uploadedImage = new Image();

      uploadedImage.src = reader.result;
      uploadedImage.classList.add("uploaded-image");
      uploadedImage.onload = async () => {

  const imageWidth = uploadedImage.width;
        const imageHeight = uploadedImage.height;
        const imageSize = 300;
        // Create a canvas to draw cropped image
        const canvas = document.createElement("canvas");
        const ctx = canvas.getContext("2d");
        canvas.width = imageSize;
        canvas.height = imageSize;

        let sourceX, sourceY, sourceWidth, sourceHeight;

        if (imageWidth > imageHeight) {
            // Image is wider than it is tall
            sourceHeight = imageHeight;
            sourceWidth = imageHeight;
            sourceX = (imageWidth - sourceWidth) / 2;
            sourceY = 0;
        } else {
            // Image is taller than it is wide or square
            sourceWidth = imageWidth;
            sourceHeight = imageWidth;
            sourceY = (imageHeight - sourceHeight) / 2;
            sourceX = 0;
        }

        ctx.drawImage(uploadedImage, sourceX, sourceY, sourceWidth, sourceHeight, 0, 0, imageSize, imageSize);
        
        const croppedImage = new Image();
        croppedImage.src = canvas.toDataURL();
        croppedImage.classList.add("uploaded-image");
        dropzone.innerHTML = "";
        dropzone.appendChild(croppedImage);
      };
    };

    if (file) {
      reader.readAsDataURL(file);
    }
  }


dropzone.addEventListener("click", () => {
    const fileInput = document.createElement("input");
    fileInput.type = "file";
    fileInput.accept = "image/*";
    fileInput.onchange = (e) => {
      const file = e.target.files[0];
      handleImageUpload(file);
    };
    fileInput.click();
});


dropzone.addEventListener("dragover", (e) => {
    e.preventDefault();
    dropzone.classList.add("highlight");
});


dropzone.addEventListener("dragleave", () => {
    dropzone.classList.remove("highlight");
});


dropzone.addEventListener("drop", (e) => {
    e.preventDefault();
    dropzone.classList.remove("highlight");

    const file = e.dataTransfer.files[0];
    handleImageUpload(file);
});

featureSelect.addEventListener("change", () => {
  if (lastUploadedImage) {
    handleImageUpload(lastUploadedImage);
  }
});

function displayResults(data) {
  thinkingText.innerText = "";

  const predictionContainer = document.getElementById("prediction-container");
  const loadingImage = predictionContainer.querySelector('img[src="static/loading.gif"]');
  if (loadingImage) {
      predictionContainer.removeChild(loadingImage);
  }

  const predictedImagesContainer = document.getElementById("predicted-images");
  predictedImagesContainer.innerHTML = ""; // Clear previous images

  for (let i = 0; i < data.images.length; i++) {
    const imageUrl = data.images[i];
    const predictionUrl = data.predictions[i];
    const name = data.names[i];
    const species = data.species[i];

    // Create a container div for each image and its anchor
    const imageContainer = document.createElement("div");

    // Create the anchor element
    const anchorElement = document.createElement("a");
    anchorElement.href = predictionUrl;
    anchorElement.target = "_blank"; // Open the link in a new tab

    // Create the caption element
    const captionElement = document.createElement("div");
    captionElement.classList.add("image-caption");
    captionElement.textContent = name; // Use the name from the 'names' list as the caption text

    // Create the caption element
    const speciesElement = document.createElement("div");
    speciesElement.classList.add("image-species");
    speciesElement.textContent = species; // Use the name from the 'names' list as the caption text

    // Create the image element
    const imageElement = document.createElement("img");
    // Add cache-busting parameter to the image URL
    const cacheBustUrl = imageUrl + `?cache=${Date.now()}`;
    // Set the lazy loading attribute
    imageElement.loading = "lazy";
    // Set the data-src attribute with the cache-busted URL
    imageElement.src = cacheBustUrl;

    const tooltip = document.createElement("div");
    tooltip.classList.add("image-tooltip");
    tooltip.textContent = species; // Use the name from the 'names' list as the tooltip text

    // Append the image to the anchor and the anchor to the container
    anchorElement.appendChild(imageElement);
    anchorElement.appendChild(captionElement);
    imageContainer.appendChild(tooltip);
    imageContainer.appendChild(anchorElement);
    captionElement.style.opacity = "0";
    captionElement.style.opacity = "1";
    

    // Append the container to the predictedImagesContainer
    predictedImagesContainer.appendChild(imageContainer);
    
    // Add the tooltip element to the document body
    document.body.appendChild(tooltip);

    // Add event listeners to handle tooltip visibility
    imageContainer.addEventListener("mouseover", () => {
      tooltip.style.opacity = "1";
    });

    imageContainer.addEventListener("mouseout", () => {
      tooltip.style.opacity = "0";
    });

    // Position the tooltip dynamically based on cursor movement
    imageContainer.addEventListener("mousemove", (event) => {
      tooltip.style.left = event.pageX + "px";
      tooltip.style.top = event.pageY + "px";
    });
  }
}


});