Upload
Upload
useEffect(() => {
// Just to show we received the image name from Link
if (imageNameFromLink) {
console.log("Received image name from link:", imageNameFromLink);
}
}, [imageNameFromLink]);
setSelectedFile(file);
setError("");
setImageUrl("");
setDetectionData(null);
setOcrText(""); // Reset OCR text
setLoading(true);
try {
// Upload the image to the backend API
const response = await axios.post(`${base_url}/upload-image`, formData, {
headers: {
"Content-Type": "multipart/form-data",
},
});
try {
const response = await axios.post(`${base_url}/detect-image`, {
imageUrl: uploadedImageUrl,
});
setDetectionData(response.data);
try {
// Use the canvas to crop the image
const img = new Image();
img.src = imageUrl;
// Draw the image on the canvas, cropping it to the number plate region
ctx.drawImage(
img,
numberPlate.x - numberPlate.width / 2,
numberPlate.y - numberPlate.height / 2,
numberPlate.width,
numberPlate.height,
0,
0,
numberPlate.width,
numberPlate.height
);
return (
<div
key={pred.detection_id}
style={{
position: "absolute",
left: (pred.x - pred.width / 2) * scaleX,
top: (pred.y - pred.height / 2) * scaleY,
width: pred.width * scaleX,
height: pred.height * scaleY,
border: "2px solid #FF0000",
boxSizing: "border-box",
borderRadius: "4px",
}}
>
<span
style={{
position: "absolute",
bottom: "0px",
left: "5px",
color: "white",
backgroundColor: "rgba(0, 0, 0, 0.7)",
padding: "2px 5px",
fontSize: "12px",
borderRadius: "3px",
}}
>
{pred.class} ({(pred.confidence * 100).toFixed(2)}%)
</span>
</div>
);
});
};
return (
<div style={styles.container}>
<h1 style={styles.title}>Upload Here</h1>
<div style={styles.inputGroup}>
<label htmlFor="file-upload" style={styles.uploadLabel}>
<span style={styles.uploadButton}>📤 Select Image</span>
<input
id="file-upload"
type="file"
accept="image/*"
onChange={handleFileChange}
style={styles.fileInput}
/>
</label>
</div>
{imageUrl && (
<div style={styles.imageContainer}>
<img
id="uploaded-image"
src={imageUrl}
alt="Uploaded"
style={styles.image}
/>
{renderBoundingBoxes()}
</div>
)}
{ocrText && (
<div style={styles.ocrContainer}>
<h3>Extracted Text:</h3>
<p>{ocrText}</p>
</div>
)}
const styles = {
container: {
padding: "20px",
maxWidth: "800px",
margin: "0 auto",
textAlign: "center",
fontFamily: "Arial, sans-serif",
backgroundColor: "#f9f9f9",
borderRadius: "8px",
boxShadow: "0 4px 6px rgba(0,0,0,0.1)",
},
title: {
marginBottom: "20px",
color: "#333",
},
inputGroup: {
display: "flex",
justifyContent: "center",
marginBottom: "20px",
},
uploadLabel: {
position: "relative",
display: "inline-block",
cursor: "pointer",
},
uploadButton: {
padding: "10px 20px",
backgroundColor: "#00c46a",
color: "#fff",
borderRadius: "4px",
fontSize: "16px",
transition: "background-color 0.3s",
},
fileInput: {
position: "absolute",
top: 0,
left: 0,
opacity: 0,
width: "100%",
height: "100%",
cursor: "pointer",
},
imageContainer: {
position: "relative",
display: "inline-block",
maxWidth: "100%",
borderRadius: "8px",
overflow: "hidden",
boxShadow: "0 4px 6px rgba(0,0,0,0.1)",
},
image: {
maxWidth: "100%",
height: "auto",
display: "block",
},
ocrContainer: {
marginTop: "20px",
padding: "10px",
backgroundColor: "#f1f1f1",
borderRadius: "6px",
boxShadow: "0 2px 4px rgba(0,0,0,0.1)",
},
errorText: {
color: "#FF0000",
marginTop: "10px",
},
};