Update code.txt
Browse files
code.txt
CHANGED
|
@@ -139,3 +139,85 @@ for i, label in enumerate(label_names):
|
|
| 139 |
print(f" Recall: {recall[i]:.4f}")
|
| 140 |
print(f" F1-Score: {f1_score[i]:.4f}")
|
| 141 |
print()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
print(f" Recall: {recall[i]:.4f}")
|
| 140 |
print(f" F1-Score: {f1_score[i]:.4f}")
|
| 141 |
print()
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
import React, { useState, useEffect } from "react";
|
| 146 |
+
import * as tflite from "@tensorflow/tfjs-tflite";
|
| 147 |
+
|
| 148 |
+
function ObjectDetector() {
|
| 149 |
+
const [model, setModel] = useState(null);
|
| 150 |
+
const [imageUrl, setImageUrl] = useState(null);
|
| 151 |
+
const [predictions, setPredictions] = useState([]);
|
| 152 |
+
|
| 153 |
+
// Load the TFLite model
|
| 154 |
+
useEffect(() => {
|
| 155 |
+
const loadModel = async () => {
|
| 156 |
+
const loadedModel = await tflite.loadTFLiteModel('/path_to_your_model.tflite');
|
| 157 |
+
setModel(loadedModel);
|
| 158 |
+
};
|
| 159 |
+
|
| 160 |
+
loadModel();
|
| 161 |
+
}, []);
|
| 162 |
+
|
| 163 |
+
// Handle image input change
|
| 164 |
+
const handleImageChange = (event) => {
|
| 165 |
+
const file = event.target.files[0];
|
| 166 |
+
if (file) {
|
| 167 |
+
setImageUrl(URL.createObjectURL(file));
|
| 168 |
+
}
|
| 169 |
+
};
|
| 170 |
+
|
| 171 |
+
// Run inference on the selected image
|
| 172 |
+
const runInference = async () => {
|
| 173 |
+
if (!model || !imageUrl) return;
|
| 174 |
+
|
| 175 |
+
const imageElement = document.getElementById("inputImage");
|
| 176 |
+
|
| 177 |
+
// Prepare the image
|
| 178 |
+
const input = tflite.toTensor(imageElement, [1, 224, 224, 3]); // Resize and convert to tensor, adjust size as per your model
|
| 179 |
+
const output = await model.predict(input); // Run inference
|
| 180 |
+
|
| 181 |
+
// Interpret the results
|
| 182 |
+
// This part depends on the output format of your TFLite model (e.g., bounding boxes, class labels)
|
| 183 |
+
setPredictions(output); // Adjust this based on your model's output structure
|
| 184 |
+
};
|
| 185 |
+
|
| 186 |
+
return (
|
| 187 |
+
<div>
|
| 188 |
+
<h1>Object Detection with TFLite</h1>
|
| 189 |
+
|
| 190 |
+
{/* Input: Upload Image */}
|
| 191 |
+
<input type="file" accept="image/*" onChange={handleImageChange} />
|
| 192 |
+
|
| 193 |
+
{/* Display Selected Image */}
|
| 194 |
+
{imageUrl && (
|
| 195 |
+
<div>
|
| 196 |
+
<img id="inputImage" src={imageUrl} alt="Input" width="300px" />
|
| 197 |
+
</div>
|
| 198 |
+
)}
|
| 199 |
+
|
| 200 |
+
{/* Run Inference Button */}
|
| 201 |
+
<button onClick={runInference} disabled={!model}>
|
| 202 |
+
Run Inference
|
| 203 |
+
</button>
|
| 204 |
+
|
| 205 |
+
{/* Display Predictions */}
|
| 206 |
+
{predictions.length > 0 && (
|
| 207 |
+
<div>
|
| 208 |
+
<h2>Predictions:</h2>
|
| 209 |
+
<ul>
|
| 210 |
+
{predictions.map((pred, index) => (
|
| 211 |
+
<li key={index}>
|
| 212 |
+
{`Class: ${pred.class}, Confidence: ${pred.confidence}, Bounding Box: ${pred.bbox}`}
|
| 213 |
+
</li>
|
| 214 |
+
))}
|
| 215 |
+
</ul>
|
| 216 |
+
</div>
|
| 217 |
+
)}
|
| 218 |
+
</div>
|
| 219 |
+
);
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
export default ObjectDetector;
|
| 223 |
+
|