Spaces:
Sleeping
Sleeping
update ui
Browse files- static/js/script.js +82 -3
- templates/index.html +37 -7
static/js/script.js
CHANGED
|
@@ -1,17 +1,92 @@
|
|
| 1 |
let currentImageUrl = "";
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
async function analyzeFood() {
|
| 4 |
const fileInput = document.getElementById('imageInput');
|
| 5 |
const loadingDiv = document.getElementById('loading');
|
| 6 |
const editorArea = document.getElementById('editorArea');
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
return;
|
| 11 |
}
|
| 12 |
|
| 13 |
const formData = new FormData();
|
| 14 |
-
formData.append('file',
|
| 15 |
|
| 16 |
loadingDiv.classList.remove('hidden');
|
| 17 |
editorArea.classList.add('hidden');
|
|
@@ -34,6 +109,10 @@ async function analyzeFood() {
|
|
| 34 |
document.getElementById('editDesc').value = data.data.description;
|
| 35 |
document.getElementById('editNutri').value = data.data.nutrition_summary;
|
| 36 |
editorArea.classList.remove('hidden');
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
} else {
|
| 38 |
alert("Lỗi từ Server: " + data.message);
|
| 39 |
}
|
|
|
|
| 1 |
let currentImageUrl = "";
|
| 2 |
|
| 3 |
+
// 1. THÊM CÁC BIẾN CHO CAMERA
|
| 4 |
+
let stream = null;
|
| 5 |
+
let capturedFile = null; // Biến này sẽ giữ ảnh vừa chụp
|
| 6 |
+
|
| 7 |
+
// 2. THÊM SỰ KIỆN LẮNG NGHE:
|
| 8 |
+
// Nếu người dùng chọn file mới từ thiết bị, xóa ảnh đã chụp trước đó để tránh gửi nhầm
|
| 9 |
+
document.getElementById('imageInput').addEventListener('change', function () {
|
| 10 |
+
capturedFile = null;
|
| 11 |
+
if (this.files.length > 0) {
|
| 12 |
+
analyzeFood();
|
| 13 |
+
}
|
| 14 |
+
});
|
| 15 |
+
|
| 16 |
+
// 3. CÁC HÀM ĐIỀU KHIỂN CAMERA
|
| 17 |
+
async function startCamera() {
|
| 18 |
+
const cameraContainer = document.getElementById('cameraContainer');
|
| 19 |
+
const video = document.getElementById('videoElement');
|
| 20 |
+
|
| 21 |
+
try {
|
| 22 |
+
// Yêu cầu quyền mở Camera (ưu tiên camera sau)
|
| 23 |
+
stream = await navigator.mediaDevices.getUserMedia({
|
| 24 |
+
video: { facingMode: 'environment' }
|
| 25 |
+
});
|
| 26 |
+
video.srcObject = stream;
|
| 27 |
+
cameraContainer.classList.remove('hidden'); // Hiện khung camera lên
|
| 28 |
+
} catch (err) {
|
| 29 |
+
console.error("Lỗi xin quyền camera:", err);
|
| 30 |
+
alert("Không thể mở Camera! Hãy đảm bảo bạn đã cấp quyền trong trình duyệt.");
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
function stopCamera() {
|
| 35 |
+
const cameraContainer = document.getElementById('cameraContainer');
|
| 36 |
+
if (stream) {
|
| 37 |
+
// Tắt từng luồng (track) của camera để giải phóng phần cứng
|
| 38 |
+
stream.getTracks().forEach(track => track.stop());
|
| 39 |
+
}
|
| 40 |
+
cameraContainer.classList.add('hidden'); // Giấu khung camera đi
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
function capturePhoto() {
|
| 44 |
+
const video = document.getElementById('videoElement');
|
| 45 |
+
const canvas = document.getElementById('canvasElement');
|
| 46 |
+
|
| 47 |
+
// Đặt kích thước "phòng tối" bằng với kích thước thật của video
|
| 48 |
+
canvas.width = video.videoWidth;
|
| 49 |
+
canvas.height = video.videoHeight;
|
| 50 |
+
|
| 51 |
+
// "Rửa" khung hình hiện tại lên canvas
|
| 52 |
+
const ctx = canvas.getContext('2d');
|
| 53 |
+
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
| 54 |
+
|
| 55 |
+
// Chuyển hình ảnh từ canvas thành một File thực thụ
|
| 56 |
+
canvas.toBlob(function (blob) {
|
| 57 |
+
capturedFile = new File([blob], "camera_capture.jpg", { type: "image/jpeg" });
|
| 58 |
+
|
| 59 |
+
// Thông báo và dọn dẹp
|
| 60 |
+
alert("📸 Đã chụp ảnh thành công! Hãy bấm 'Quét ảnh' để AI phân tích.");
|
| 61 |
+
document.getElementById('imageInput').value = ""; // Xóa tên file ở ô input (nếu có)
|
| 62 |
+
stopCamera();
|
| 63 |
+
analyzeFood();
|
| 64 |
+
|
| 65 |
+
}, 'image/jpeg', 0.9); // Chất lượng 90%
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
// 4. CẬP NHẬT HÀM GỬI API CỦA BẠN
|
| 69 |
async function analyzeFood() {
|
| 70 |
const fileInput = document.getElementById('imageInput');
|
| 71 |
const loadingDiv = document.getElementById('loading');
|
| 72 |
const editorArea = document.getElementById('editorArea');
|
| 73 |
|
| 74 |
+
// MỚI: KIỂM TRA NGUỒN ẢNH (Ưu tiên ảnh vừa chụp, sau đó mới đến ảnh chọn từ thư viện)
|
| 75 |
+
let fileToUpload = null;
|
| 76 |
+
if (capturedFile) {
|
| 77 |
+
fileToUpload = capturedFile;
|
| 78 |
+
} else if (fileInput.files.length > 0) {
|
| 79 |
+
fileToUpload = fileInput.files[0];
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
// Nếu không có ảnh từ cả 2 nguồn thì báo lỗi
|
| 83 |
+
if (!fileToUpload) {
|
| 84 |
+
alert("Vui lòng chọn ảnh hoặc chụp ảnh trước!");
|
| 85 |
return;
|
| 86 |
}
|
| 87 |
|
| 88 |
const formData = new FormData();
|
| 89 |
+
formData.append('file', fileToUpload); // Đính kèm ảnh đã chọn vào form
|
| 90 |
|
| 91 |
loadingDiv.classList.remove('hidden');
|
| 92 |
editorArea.classList.add('hidden');
|
|
|
|
| 109 |
document.getElementById('editDesc').value = data.data.description;
|
| 110 |
document.getElementById('editNutri').value = data.data.nutrition_summary;
|
| 111 |
editorArea.classList.remove('hidden');
|
| 112 |
+
|
| 113 |
+
// Xóa ảnh sau khi phân tích xong để reset trạng thái cho lần quét tiếp theo
|
| 114 |
+
capturedFile = null;
|
| 115 |
+
fileInput.value = "";
|
| 116 |
} else {
|
| 117 |
alert("Lỗi từ Server: " + data.message);
|
| 118 |
}
|
templates/index.html
CHANGED
|
@@ -1,30 +1,59 @@
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html lang="vi">
|
|
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
-
<title>
|
| 7 |
<link rel="stylesheet" href="/static/css/style.css">
|
| 8 |
</head>
|
|
|
|
| 9 |
<body>
|
| 10 |
<div class="main-container">
|
| 11 |
<div class="panel left-panel">
|
| 12 |
-
<h1>
|
| 13 |
-
|
| 14 |
<div class="upload-box">
|
| 15 |
-
<input type="file" id="imageInput" accept="image/*">
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
</div>
|
| 18 |
|
| 19 |
<div id="loading" class="hidden">
|
| 20 |
<div class="spinner"></div>
|
| 21 |
-
<p>Đang phân tích món ăn...</p>
|
| 22 |
</div>
|
| 23 |
|
| 24 |
<div id="editorArea" class="hidden">
|
| 25 |
<div class="editor-card">
|
| 26 |
<img id="editImg" src="" alt="Preview">
|
| 27 |
-
|
| 28 |
<div class="form-group">
|
| 29 |
<label>Tên món:</label>
|
| 30 |
<input type="text" id="editName" class="form-control big-text">
|
|
@@ -51,4 +80,5 @@
|
|
| 51 |
|
| 52 |
<script src="/static/js/script.js"></script>
|
| 53 |
</body>
|
|
|
|
| 54 |
</html>
|
|
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html lang="vi">
|
| 3 |
+
|
| 4 |
<head>
|
| 5 |
<meta charset="UTF-8">
|
| 6 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 7 |
+
<title>Now i know what i eat</title>
|
| 8 |
<link rel="stylesheet" href="/static/css/style.css">
|
| 9 |
</head>
|
| 10 |
+
|
| 11 |
<body>
|
| 12 |
<div class="main-container">
|
| 13 |
<div class="panel left-panel">
|
| 14 |
+
<h1> Scan </h1>
|
| 15 |
+
|
| 16 |
<div class="upload-box">
|
| 17 |
+
<input type="file" id="imageInput" accept="image/*" style="display: none;">
|
| 18 |
+
|
| 19 |
+
<label for="imageInput"
|
| 20 |
+
style="display: inline-flex; align-items: center; border-radius: 5px; border: 2px outset rgb(236, 248, 70); background-color:gold; padding: 8px 15px; cursor: pointer; margin: 0; color: black; box-sizing: border-box; font-family: inherit; font-size: 13.33px;">
|
| 21 |
+
Chọn từ máy
|
| 22 |
+
</label>
|
| 23 |
+
|
| 24 |
+
<button onclick="startCamera()" id="btnOpenCamera"
|
| 25 |
+
style="border-radius: 5px; border-color: aqua; margin-left: 10px; padding: 8px 15px; cursor: pointer; background-color: cyan;">Bật
|
| 26 |
+
Camera</button>
|
| 27 |
+
|
| 28 |
+
<div id="cameraContainer" class="hidden"
|
| 29 |
+
style="margin-top: 15px; border: 2px dashed #ccc; padding: 10px; border-radius: 8px; text-align: center;">
|
| 30 |
+
<video id="videoElement" autoplay playsinline
|
| 31 |
+
style="width: 100%; max-width: 400px; border-radius: 8px; background: #000;"></video>
|
| 32 |
+
|
| 33 |
+
<div style="margin-top: 10px; display: flex; gap: 10px; justify-content: center;">
|
| 34 |
+
<button onclick="capturePhoto()" class="btn-primary"> Chụp ngay</button>
|
| 35 |
+
<button onclick="stopCamera()" style="padding: 8px 15px; cursor: pointer;"> Đóng</button>
|
| 36 |
+
</div>
|
| 37 |
+
</div>
|
| 38 |
+
|
| 39 |
+
<canvas id="canvasElement" style="display: none;"></canvas>
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
<button onclick="analyzeFood()" id="btnAnalyze" class="btn-primary" style="display: none;">📸 Quét
|
| 45 |
+
ảnh</button>
|
| 46 |
</div>
|
| 47 |
|
| 48 |
<div id="loading" class="hidden">
|
| 49 |
<div class="spinner"></div>
|
| 50 |
+
<p> Đang phân tích món ăn...</p>
|
| 51 |
</div>
|
| 52 |
|
| 53 |
<div id="editorArea" class="hidden">
|
| 54 |
<div class="editor-card">
|
| 55 |
<img id="editImg" src="" alt="Preview">
|
| 56 |
+
|
| 57 |
<div class="form-group">
|
| 58 |
<label>Tên món:</label>
|
| 59 |
<input type="text" id="editName" class="form-control big-text">
|
|
|
|
| 80 |
|
| 81 |
<script src="/static/js/script.js"></script>
|
| 82 |
</body>
|
| 83 |
+
|
| 84 |
</html>
|