-
Notifications
You must be signed in to change notification settings - Fork 31
Add Tree Hazard Detector and Netlify Deployment Config #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import { useState, useRef, useCallback } from 'react'; | ||
| import Webcam from 'react-webcam'; | ||
|
|
||
| const TreeDetector = ({ onBack }) => { | ||
| const webcamRef = useRef(null); | ||
| const [imgSrc, setImgSrc] = useState(null); | ||
| const [detections, setDetections] = useState([]); | ||
| const [loading, setLoading] = useState(false); | ||
| const [cameraError, setCameraError] = useState(null); | ||
|
|
||
| const capture = useCallback(() => { | ||
| const imageSrc = webcamRef.current.getScreenshot(); | ||
| setImgSrc(imageSrc); | ||
| }, [webcamRef]); | ||
|
|
||
| const retake = () => { | ||
| setImgSrc(null); | ||
| setDetections([]); | ||
| }; | ||
|
|
||
| const detectTreeHazard = async () => { | ||
| if (!imgSrc) return; | ||
| setLoading(true); | ||
| setDetections([]); | ||
|
|
||
| try { | ||
| // Convert base64 to blob | ||
| const res = await fetch(imgSrc); | ||
| const blob = await res.blob(); | ||
| const file = new File([blob], "image.jpg", { type: "image/jpeg" }); | ||
|
|
||
| const formData = new FormData(); | ||
| formData.append('image', file); | ||
|
|
||
| // Call Backend API | ||
| const response = await fetch('/api/detect-tree-hazard', { | ||
| method: 'POST', | ||
| body: formData, | ||
| }); | ||
|
|
||
| if (response.ok) { | ||
| const data = await response.json(); | ||
| setDetections(data.detections); | ||
| if (data.detections.length === 0) { | ||
| alert("No tree hazard detected."); | ||
| } | ||
| } else { | ||
| console.error("Detection failed"); | ||
| alert("Detection failed. Please try again."); | ||
| } | ||
| } catch (error) { | ||
| console.error("Error:", error); | ||
| alert("An error occurred during detection."); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="p-4 max-w-md mx-auto h-full flex flex-col"> | ||
| <button onClick={onBack} className="self-start text-blue-600 mb-2"> | ||
| ← Back | ||
| </button> | ||
| <h2 className="text-2xl font-bold mb-4 text-green-800">Tree Hazard Detector</h2> | ||
|
|
||
| {cameraError ? ( | ||
| <div className="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative"> | ||
| <strong className="font-bold">Camera Error:</strong> | ||
| <span className="block sm:inline"> {cameraError}</span> | ||
| </div> | ||
| ) : ( | ||
| <div className="mb-4 rounded-lg overflow-hidden shadow-lg border-2 border-gray-300 bg-gray-100 min-h-[300px] relative"> | ||
| {!imgSrc ? ( | ||
| <Webcam | ||
| audio={false} | ||
| ref={webcamRef} | ||
| screenshotFormat="image/jpeg" | ||
| className="w-full h-full object-cover" | ||
| onUserMediaError={(err) => setCameraError("Could not access camera. Please check permissions.")} | ||
| /> | ||
| ) : ( | ||
| <div className="relative"> | ||
| <img src={imgSrc} alt="Captured" className="w-full" /> | ||
| {/* Since CLIP doesn't give boxes, we just show a banner if detected */} | ||
| {detections.length > 0 && ( | ||
| <div className="absolute top-0 left-0 right-0 bg-red-600 text-white p-2 text-center font-bold opacity-90"> | ||
| DETECTED: {detections.map(d => d.label).join(', ')} | ||
| </div> | ||
| )} | ||
| </div> | ||
| )} | ||
| </div> | ||
| )} | ||
|
|
||
| <div className="flex justify-center gap-4"> | ||
| {!imgSrc ? ( | ||
| <button | ||
| onClick={capture} | ||
| disabled={!!cameraError} | ||
| className={`bg-blue-600 text-white px-6 py-2 rounded-full font-semibold shadow-md hover:bg-blue-700 transition ${cameraError ? 'opacity-50 cursor-not-allowed' : ''}`} | ||
| > | ||
| Capture Photo | ||
| </button> | ||
| ) : ( | ||
| <> | ||
| <button | ||
| onClick={retake} | ||
| className="bg-gray-500 text-white px-6 py-2 rounded-full font-semibold shadow-md hover:bg-gray-600 transition" | ||
| > | ||
| Retake | ||
| </button> | ||
| <button | ||
| onClick={detectTreeHazard} | ||
| disabled={loading} | ||
| className={`bg-green-600 text-white px-6 py-2 rounded-full font-semibold shadow-md hover:bg-green-700 transition flex items-center ${loading ? 'opacity-70 cursor-wait' : ''}`} | ||
| > | ||
| {loading ? ( | ||
| <> | ||
| <svg className="animate-spin -ml-1 mr-2 h-5 w-5 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"> | ||
| <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle> | ||
| <path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path> | ||
| </svg> | ||
| Analyzing... | ||
| </> | ||
| ) : 'Detect Hazard'} | ||
| </button> | ||
| </> | ||
| )} | ||
| </div> | ||
|
|
||
| <p className="mt-4 text-sm text-gray-600 text-center"> | ||
| Point camera at fallen trees or dangerous branches. | ||
| </p> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default TreeDetector; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,29 +1,15 @@ | ||||||||||||||||||||||
| # Netlify Configuration for VishwaGuru Frontend | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Build settings | ||||||||||||||||||||||
| [build] | ||||||||||||||||||||||
| base = "frontend" | ||||||||||||||||||||||
| publish = "dist" | ||||||||||||||||||||||
| command = "npm run build" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Build environment | ||||||||||||||||||||||
| [build.environment] | ||||||||||||||||||||||
| NODE_VERSION = "20" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Environment variables (set these in Netlify dashboard) | ||||||||||||||||||||||
| # VITE_API_URL = https://your-backend.onrender.com | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Redirects for SPA | ||||||||||||||||||||||
| [[redirects]] | ||||||||||||||||||||||
| from = "/*" | ||||||||||||||||||||||
| to = "/index.html" | ||||||||||||||||||||||
| status = 200 | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Headers for security | ||||||||||||||||||||||
| [[headers]] | ||||||||||||||||||||||
| for = "/*" | ||||||||||||||||||||||
| [headers.values] | ||||||||||||||||||||||
| X-Frame-Options = "DENY" | ||||||||||||||||||||||
| X-Content-Type-Options = "nosniff" | ||||||||||||||||||||||
| X-XSS-Protection = "1; mode=block" | ||||||||||||||||||||||
| Referrer-Policy = "strict-origin-when-cross-origin" | ||||||||||||||||||||||
| [[redirects]] | ||||||||||||||||||||||
| from = "/api/*" | ||||||||||||||||||||||
| to = "https://vishwaguru-backend.onrender.com/api/:splat" | ||||||||||||||||||||||
| status = 200 | ||||||||||||||||||||||
| force = true | ||||||||||||||||||||||
|
||||||||||||||||||||||
| force = true | |
| force = true | |
| [[headers]] | |
| for = "/*" | |
| [headers.values] | |
| X-Frame-Options = "DENY" | |
| X-Content-Type-Options = "nosniff" | |
| X-XSS-Protection = "1; mode=block" | |
| Referrer-Policy = "strict-origin-when-cross-origin" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new detect_tree_clip function is missing a docstring, unlike the other detection functions in this file (e.g., detect_vandalism_clip has a docstring). Add a docstring to describe the function's purpose, parameters, and return value for consistency and better maintainability.