From c2f75169e8c9d5942bce48f6bfcbb742b3cd648a Mon Sep 17 00:00:00 2001 From: Shradha Saxena <103318017+ShradhaSaxena23@users.noreply.github.com> Date: Wed, 9 Oct 2024 16:42:46 +0530 Subject: [PATCH] Update AIEngine.jsx for Image Preview Added the feature of image preview in AIEngine.jsx --- frontend/src/components/AIEngine.jsx | 152 +++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) diff --git a/frontend/src/components/AIEngine.jsx b/frontend/src/components/AIEngine.jsx index ee116e15..0382b9df 100644 --- a/frontend/src/components/AIEngine.jsx +++ b/frontend/src/components/AIEngine.jsx @@ -1,6 +1,157 @@ import React, { useState } from 'react'; import { useNavigate } from 'react-router-dom'; +const AIEngine = () => { + const [selectedFile, setSelectedFile] = useState(null); + const [fileName, setFileName] = useState('No file chosen'); + const [previewUrl, setPreviewUrl] = useState(null); // To store the image preview URL + const [isLoading, setIsLoading] = useState(false); + const navigate = useNavigate(); + + const handleFileChange = (event) => { + const file = event.target.files[0]; + setSelectedFile(file); + setFileName(file ? file.name : 'No file chosen'); + + // Set image preview + if (file) { + const imageUrl = URL.createObjectURL(file); + setPreviewUrl(imageUrl); + } else { + setPreviewUrl(null); + } + }; + + const handleSubmit = async (event) => { + event.preventDefault(); + if (!selectedFile) { + alert('Please select a file'); + return; + } + + setIsLoading(true); + const formData = new FormData(); + formData.append('image', selectedFile); + + try { + const response = await fetch('https://disease-prediction-api-2.onrender.com/submit', { + method: 'POST', + body: formData, + }); + + if (response.ok) { + const result = await response.json(); + navigate('/submit', { state: { result } }); + } else { + throw new Error('File upload failed'); + } + } catch (error) { + console.error('Error:', error); + alert('An error occurred while uploading the file'); + } finally { + setIsLoading(false); + } + }; + + return ( +
+
+

🍀AI Engine🍀

+

Let AI Engine Help You To Detect Disease

+
+ +
+
+
Why is it necessary to detect disease in plants?
+

+ Plant diseases affect the growth of their respective species... +

+
+ +
+ Plant + +
+ {/* Image preview */} + {previewUrl && ( +
+ Selected file preview +
+ )} + +
+ + + {fileName} +
+ +

+ Simply upload your plant's leaf image and then see the magic of AI. +

+ +
+ +
+
+
+ +
+
Prevent Plant Disease by following these steps:
+
    +
  1. Follow Good Sanitation Practices.
  2. +
  3. Fertilize to Keep Your Plants Healthy.
  4. +
  5. Inspect Plants for Diseases Before You Bring Them Home.
  6. +
  7. Allow the Soil to Warm Before Planting.
  8. +
  9. Ensure a Healthy Vegetable Garden By Rotating Crops.
  10. +
  11. Provide Good Air Circulation.
  12. +
  13. Remove Diseased Stems and Foliage.
  14. +
+ + More info + +
+
+
+ ); +}; + +export default AIEngine; + + +{/*} +import React, { useState } from 'react'; +import { useNavigate } from 'react-router-dom'; + const AIEngine = () => { const [selectedFile, setSelectedFile] = useState(null); const [fileName, setFileName] = useState('No file chosen'); @@ -129,3 +280,4 @@ const AIEngine = () => { }; export default AIEngine; +*/} \ No newline at end of file