Spaces:
Sleeping
Sleeping
Commit ·
1210fba
1
Parent(s): 1d7a262
requirements.txt
Browse files- app.py +3 -3
- best.pt → best_float16.tflite +2 -2
- index.js +0 -31
- js/index.js +104 -0
- templates/index.html +108 -1
- vercel.json +5 -0
app.py
CHANGED
|
@@ -10,7 +10,7 @@ from PIL import Image
|
|
| 10 |
app = Flask(__name__)
|
| 11 |
|
| 12 |
# Load YOLO model
|
| 13 |
-
trained_model = YOLO("
|
| 14 |
|
| 15 |
# Load TFLite model
|
| 16 |
interpreter = tf.lite.Interpreter(model_path='model_chip_v4.tflite')
|
|
@@ -42,9 +42,9 @@ def predict():
|
|
| 42 |
image = Image.open(file.stream)
|
| 43 |
image = image.convert('RGB')
|
| 44 |
image = np.array(image)
|
| 45 |
-
image_rgb = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 46 |
|
| 47 |
-
results = trained_model.predict(
|
| 48 |
|
| 49 |
if isinstance(results, list):
|
| 50 |
result = results[0]
|
|
|
|
| 10 |
app = Flask(__name__)
|
| 11 |
|
| 12 |
# Load YOLO model
|
| 13 |
+
trained_model = YOLO("best_float16.tflite")
|
| 14 |
|
| 15 |
# Load TFLite model
|
| 16 |
interpreter = tf.lite.Interpreter(model_path='model_chip_v4.tflite')
|
|
|
|
| 42 |
image = Image.open(file.stream)
|
| 43 |
image = image.convert('RGB')
|
| 44 |
image = np.array(image)
|
| 45 |
+
# image_rgb = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 46 |
|
| 47 |
+
results = trained_model.predict(image)
|
| 48 |
|
| 49 |
if isinstance(results, list):
|
| 50 |
result = results[0]
|
best.pt → best_float16.tflite
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e752e90ca32f978d980d34a1c7277d420000a4bae852cbfad482996da4823a21
|
| 3 |
+
size 22436674
|
index.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
| 1 |
-
function uploadImage() {
|
| 2 |
-
const file = imageInput.files[0];
|
| 3 |
-
if (!file) {
|
| 4 |
-
alert("Please select an image to upload.");
|
| 5 |
-
return;
|
| 6 |
-
}
|
| 7 |
-
|
| 8 |
-
const formData = new FormData();
|
| 9 |
-
formData.append("file", file);
|
| 10 |
-
|
| 11 |
-
fetch("/predict", {
|
| 12 |
-
method: "POST",
|
| 13 |
-
body: formData,
|
| 14 |
-
})
|
| 15 |
-
.then((response) => response.json())
|
| 16 |
-
.then((data) => {
|
| 17 |
-
if (data.confidence_score !== undefined) {
|
| 18 |
-
const reader = new FileReader();
|
| 19 |
-
reader.onload = function (e) {
|
| 20 |
-
resultsImg.src = e.target.result;
|
| 21 |
-
};
|
| 22 |
-
reader.readAsDataURL(file);
|
| 23 |
-
alert("Confidence Score: " + data.confidence_score);
|
| 24 |
-
} else {
|
| 25 |
-
alert(data.error);
|
| 26 |
-
}
|
| 27 |
-
})
|
| 28 |
-
.catch((error) => {
|
| 29 |
-
console.error("Error:", error);
|
| 30 |
-
});
|
| 31 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
js/index.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const video = document.getElementById("camera");
|
| 2 |
+
const canvas = document.getElementById("canvas");
|
| 3 |
+
const captureBtn = document.getElementById("capture-btn");
|
| 4 |
+
const imageInput = document.getElementById("imageInput");
|
| 5 |
+
const resultsImg = document.getElementById("results");
|
| 6 |
+
const chooseFileBtn = document.getElementById("chooseFileBtn");
|
| 7 |
+
const uploadSuccess = document.getElementById("uploadSuccess");
|
| 8 |
+
const confidenceScore = document.getElementById("confidenceScore");
|
| 9 |
+
|
| 10 |
+
// Access the webcam only if not on mobile
|
| 11 |
+
if (window.innerWidth > 768) {
|
| 12 |
+
navigator.mediaDevices
|
| 13 |
+
.getUserMedia({ video: true })
|
| 14 |
+
.then((stream) => {
|
| 15 |
+
video.srcObject = stream;
|
| 16 |
+
})
|
| 17 |
+
.catch((err) => {
|
| 18 |
+
console.error("Error accessing webcam: ", err);
|
| 19 |
+
});
|
| 20 |
+
} else {
|
| 21 |
+
// If the screen is small, stop the webcam stream if it exists
|
| 22 |
+
if (video.srcObject) {
|
| 23 |
+
const stream = video.srcObject;
|
| 24 |
+
const tracks = stream.getTracks();
|
| 25 |
+
|
| 26 |
+
tracks.forEach((track) => {
|
| 27 |
+
track.stop();
|
| 28 |
+
});
|
| 29 |
+
|
| 30 |
+
video.srcObject = null;
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
// Capture the image
|
| 35 |
+
captureBtn.addEventListener("click", () => {
|
| 36 |
+
const context = canvas.getContext("2d");
|
| 37 |
+
canvas.width = video.videoWidth;
|
| 38 |
+
canvas.height = video.videoHeight;
|
| 39 |
+
context.drawImage(video, 0, 0, canvas.width, canvas.height);
|
| 40 |
+
|
| 41 |
+
canvas.toBlob((blob) => {
|
| 42 |
+
const formData = new FormData();
|
| 43 |
+
formData.append("file", blob, "captured_image.png");
|
| 44 |
+
|
| 45 |
+
fetch("/predict", {
|
| 46 |
+
method: "POST",
|
| 47 |
+
body: formData,
|
| 48 |
+
})
|
| 49 |
+
.then((response) => response.json())
|
| 50 |
+
.then((data) => {
|
| 51 |
+
if (data.confidence_score !== undefined) {
|
| 52 |
+
resultsImg.src = canvas.toDataURL("image/png"); // Display captured image
|
| 53 |
+
confidenceScore.textContent =
|
| 54 |
+
"Confidence Score: " + data.confidence_score;
|
| 55 |
+
uploadSuccess.style.display = "block"; // Show success message
|
| 56 |
+
} else {
|
| 57 |
+
alert(data.error);
|
| 58 |
+
}
|
| 59 |
+
})
|
| 60 |
+
.catch((error) => {
|
| 61 |
+
console.error("Error:", error);
|
| 62 |
+
});
|
| 63 |
+
}, "image/png");
|
| 64 |
+
});
|
| 65 |
+
|
| 66 |
+
// Trigger click on the hidden file input when the custom button is clicked
|
| 67 |
+
chooseFileBtn.addEventListener("click", () => {
|
| 68 |
+
imageInput.click();
|
| 69 |
+
});
|
| 70 |
+
|
| 71 |
+
// Upload the selected image
|
| 72 |
+
function uploadImage() {
|
| 73 |
+
const file = imageInput.files[0];
|
| 74 |
+
if (!file) {
|
| 75 |
+
alert("Please select an image to upload.");
|
| 76 |
+
return;
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
const formData = new FormData();
|
| 80 |
+
formData.append("file", file);
|
| 81 |
+
|
| 82 |
+
fetch("/predict", {
|
| 83 |
+
method: "POST",
|
| 84 |
+
body: formData,
|
| 85 |
+
})
|
| 86 |
+
.then((response) => response.json())
|
| 87 |
+
.then((data) => {
|
| 88 |
+
if (data.confidence_score !== undefined) {
|
| 89 |
+
const reader = new FileReader();
|
| 90 |
+
reader.onload = function (e) {
|
| 91 |
+
resultsImg.src = e.target.result; // Display uploaded image
|
| 92 |
+
confidenceScore.textContent =
|
| 93 |
+
"Confidence Score: " + data.confidence_score;
|
| 94 |
+
uploadSuccess.style.display = "block"; // Show success message
|
| 95 |
+
};
|
| 96 |
+
reader.readAsDataURL(file);
|
| 97 |
+
} else {
|
| 98 |
+
alert(data.error);
|
| 99 |
+
}
|
| 100 |
+
})
|
| 101 |
+
.catch((error) => {
|
| 102 |
+
console.error("Error:", error);
|
| 103 |
+
});
|
| 104 |
+
}
|
templates/index.html
CHANGED
|
@@ -129,6 +129,7 @@
|
|
| 129 |
margin-bottom: 10px;
|
| 130 |
}
|
| 131 |
</style>
|
|
|
|
| 132 |
</head>
|
| 133 |
<body>
|
| 134 |
<!-- Camera Container for larger devices -->
|
|
@@ -174,7 +175,7 @@
|
|
| 174 |
<p id="confidenceScore"></p>
|
| 175 |
</div>
|
| 176 |
|
| 177 |
-
<script>
|
| 178 |
const video = document.getElementById("camera");
|
| 179 |
const canvas = document.getElementById("canvas");
|
| 180 |
const captureBtn = document.getElementById("capture-btn");
|
|
@@ -279,6 +280,112 @@
|
|
| 279 |
console.error("Error:", error);
|
| 280 |
});
|
| 281 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
</script>
|
| 283 |
</body>
|
| 284 |
</html>
|
|
|
|
| 129 |
margin-bottom: 10px;
|
| 130 |
}
|
| 131 |
</style>
|
| 132 |
+
<!-- <script src="script.js" defer></script> -->
|
| 133 |
</head>
|
| 134 |
<body>
|
| 135 |
<!-- Camera Container for larger devices -->
|
|
|
|
| 175 |
<p id="confidenceScore"></p>
|
| 176 |
</div>
|
| 177 |
|
| 178 |
+
<!-- <script>
|
| 179 |
const video = document.getElementById("camera");
|
| 180 |
const canvas = document.getElementById("canvas");
|
| 181 |
const captureBtn = document.getElementById("capture-btn");
|
|
|
|
| 280 |
console.error("Error:", error);
|
| 281 |
});
|
| 282 |
}
|
| 283 |
+
</script> -->
|
| 284 |
+
<script>
|
| 285 |
+
const video = document.getElementById("camera");
|
| 286 |
+
const canvas = document.getElementById("canvas");
|
| 287 |
+
const captureBtn = document.getElementById("capture-btn");
|
| 288 |
+
const imageInput = document.getElementById("imageInput");
|
| 289 |
+
const resultsImg = document.getElementById("results");
|
| 290 |
+
const chooseFileBtn = document.getElementById("chooseFileBtn");
|
| 291 |
+
const uploadSuccess = document.getElementById("uploadSuccess");
|
| 292 |
+
const confidenceScore = document.getElementById("confidenceScore");
|
| 293 |
+
|
| 294 |
+
// Access the webcam only if not on mobile
|
| 295 |
+
if (window.innerWidth > 768) {
|
| 296 |
+
navigator.mediaDevices
|
| 297 |
+
.getUserMedia({ video: true })
|
| 298 |
+
.then((stream) => {
|
| 299 |
+
video.srcObject = stream;
|
| 300 |
+
})
|
| 301 |
+
.catch((err) => {
|
| 302 |
+
console.error("Error accessing webcam: ", err);
|
| 303 |
+
});
|
| 304 |
+
} else {
|
| 305 |
+
// If the screen is small, stop the webcam stream if it exists
|
| 306 |
+
if (video.srcObject) {
|
| 307 |
+
const stream = video.srcObject;
|
| 308 |
+
const tracks = stream.getTracks();
|
| 309 |
+
|
| 310 |
+
tracks.forEach((track) => {
|
| 311 |
+
track.stop();
|
| 312 |
+
});
|
| 313 |
+
|
| 314 |
+
video.srcObject = null;
|
| 315 |
+
}
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
// Capture the image
|
| 319 |
+
captureBtn.addEventListener("click", () => {
|
| 320 |
+
const context = canvas.getContext("2d");
|
| 321 |
+
canvas.width = video.videoWidth;
|
| 322 |
+
canvas.height = video.videoHeight;
|
| 323 |
+
context.drawImage(video, 0, 0, canvas.width, canvas.height);
|
| 324 |
+
|
| 325 |
+
canvas.toBlob((blob) => {
|
| 326 |
+
const formData = new FormData();
|
| 327 |
+
formData.append("file", blob, "captured_image.png");
|
| 328 |
+
|
| 329 |
+
fetch("/predict", {
|
| 330 |
+
method: "POST",
|
| 331 |
+
body: formData,
|
| 332 |
+
})
|
| 333 |
+
.then((response) => response.json())
|
| 334 |
+
.then((data) => {
|
| 335 |
+
if (data.confidence_score !== undefined) {
|
| 336 |
+
resultsImg.src = canvas.toDataURL("image/png"); // Display captured image
|
| 337 |
+
confidenceScore.textContent =
|
| 338 |
+
"Confidence Score: " + data.confidence_score;
|
| 339 |
+
uploadSuccess.style.display = "block"; // Show success message
|
| 340 |
+
} else {
|
| 341 |
+
alert(data.error);
|
| 342 |
+
}
|
| 343 |
+
})
|
| 344 |
+
.catch((error) => {
|
| 345 |
+
console.error("Error:", error);
|
| 346 |
+
});
|
| 347 |
+
}, "image/png");
|
| 348 |
+
});
|
| 349 |
+
|
| 350 |
+
// Trigger click on the hidden file input when the custom button is clicked
|
| 351 |
+
chooseFileBtn.addEventListener("click", () => {
|
| 352 |
+
imageInput.click();
|
| 353 |
+
});
|
| 354 |
+
|
| 355 |
+
// Upload the selected image
|
| 356 |
+
function uploadImage() {
|
| 357 |
+
const file = imageInput.files[0];
|
| 358 |
+
if (!file) {
|
| 359 |
+
alert("Please select an image to upload.");
|
| 360 |
+
return;
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
const formData = new FormData();
|
| 364 |
+
formData.append("file", file);
|
| 365 |
+
|
| 366 |
+
fetch("/predict", {
|
| 367 |
+
method: "POST",
|
| 368 |
+
body: formData,
|
| 369 |
+
})
|
| 370 |
+
.then((response) => response.json())
|
| 371 |
+
.then((data) => {
|
| 372 |
+
if (data.confidence_score !== undefined) {
|
| 373 |
+
const reader = new FileReader();
|
| 374 |
+
reader.onload = function (e) {
|
| 375 |
+
resultsImg.src = e.target.result; // Display uploaded image
|
| 376 |
+
confidenceScore.textContent =
|
| 377 |
+
"Confidence Score: " + data.confidence_score;
|
| 378 |
+
uploadSuccess.style.display = "block"; // Show success message
|
| 379 |
+
};
|
| 380 |
+
reader.readAsDataURL(file);
|
| 381 |
+
} else {
|
| 382 |
+
alert(data.error);
|
| 383 |
+
}
|
| 384 |
+
})
|
| 385 |
+
.catch((error) => {
|
| 386 |
+
console.error("Error:", error);
|
| 387 |
+
});
|
| 388 |
+
}
|
| 389 |
</script>
|
| 390 |
</body>
|
| 391 |
</html>
|
vercel.json
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"version": 1,
|
| 3 |
+
"builds": [{ "src": "app.py", "use": "@vercel/python" }],
|
| 4 |
+
"routes": [{ "src": "/(.*)", "dest": "app.py" }]
|
| 5 |
+
}
|