-
Notifications
You must be signed in to change notification settings - Fork 36
Add Traffic Sign and Abandoned Vehicle HF UI Integrations #499
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
b4aeb9c
f3b0e7a
829415e
24cdd83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import { useState, useRef, useCallback } from 'react'; | ||
| import Webcam from 'react-webcam'; | ||
|
|
||
| const AbandonedVehicleDetector = ({ 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); | ||
|
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a null-safe capture path for webcam readiness. Line 12 can throw when Proposed fix const capture = useCallback(() => {
- const imageSrc = webcamRef.current.getScreenshot();
+ const imageSrc = webcamRef.current?.getScreenshot?.();
+ if (!imageSrc) {
+ setCameraError('Camera is not ready. Please try again.');
+ return;
+ }
setImgSrc(imageSrc);
}, [webcamRef]);🤖 Prompt for AI Agents |
||
| }, [webcamRef]); | ||
|
|
||
| const retake = () => { | ||
| setImgSrc(null); | ||
| setDetections([]); | ||
| }; | ||
|
|
||
| const detectAbandonedVehicle = 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); | ||
|
|
||
| const API_URL = import.meta.env.VITE_API_URL || ''; | ||
|
|
||
| // Call Backend API | ||
| const response = await fetch(`${API_URL}/api/detect-abandoned-vehicle`, { | ||
| method: 'POST', | ||
| body: formData, | ||
| }); | ||
|
Comment on lines
+35
to
+41
|
||
|
|
||
| if (response.ok) { | ||
| const data = await response.json(); | ||
| setDetections(data.detections); | ||
| if (data.detections.length === 0) { | ||
| alert("No abandoned vehicle 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="flex flex-col h-full"> | ||
| <button onClick={onBack} className="self-start text-blue-600 mb-2"> | ||
| ← Back | ||
| </button> | ||
| <div className="p-4 max-w-md mx-auto w-full"> | ||
| <h2 className="text-2xl font-bold mb-4">Abandoned Vehicle 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={() => setCameraError("Could not access camera. Please check permissions.")} | ||
| /> | ||
| ) : ( | ||
| <div className="relative"> | ||
| <img src={imgSrc} alt="Captured" className="w-full" /> | ||
| {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={detectAbandonedVehicle} | ||
| disabled={loading} | ||
| className={`bg-red-600 text-white px-6 py-2 rounded-full font-semibold shadow-md hover:bg-red-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'} | ||
| </button> | ||
| </> | ||
| )} | ||
| </div> | ||
|
|
||
| <p className="mt-4 text-sm text-gray-600 text-center"> | ||
| Point camera at a vehicle to check if it's abandoned or wrecked. | ||
| </p> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default AbandonedVehicleDetector; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,141 @@ | ||||||||||||||||||||||||
| import { useState, useRef, useCallback } from 'react'; | ||||||||||||||||||||||||
| import Webcam from 'react-webcam'; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const TrafficSignDetector = ({ 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(); | ||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Guard the webcam ref before calling Prompt for AI agents |
||||||||||||||||||||||||
| setImgSrc(imageSrc); | ||||||||||||||||||||||||
|
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guard webcam ref before capture to avoid runtime crash. Line 12 can throw if Proposed fix const capture = useCallback(() => {
- const imageSrc = webcamRef.current.getScreenshot();
+ const imageSrc = webcamRef.current?.getScreenshot?.();
+ if (!imageSrc) {
+ setCameraError('Camera is not ready. Please try again.');
+ return;
+ }
setImgSrc(imageSrc);
}, [webcamRef]);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
| }, [webcamRef]); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const retake = () => { | ||||||||||||||||||||||||
| setImgSrc(null); | ||||||||||||||||||||||||
| setDetections([]); | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const detectTrafficSign = 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); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const API_URL = import.meta.env.VITE_API_URL || ''; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Call Backend API | ||||||||||||||||||||||||
| const response = await fetch(`${API_URL}/api/detect-traffic-sign`, { | ||||||||||||||||||||||||
| method: 'POST', | ||||||||||||||||||||||||
| body: formData, | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
Comment on lines
+35
to
+41
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (response.ok) { | ||||||||||||||||||||||||
| const data = await response.json(); | ||||||||||||||||||||||||
| setDetections(data.detections); | ||||||||||||||||||||||||
| if (data.detections.length === 0) { | ||||||||||||||||||||||||
| alert("No traffic sign issue 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="flex flex-col h-full"> | ||||||||||||||||||||||||
| <button onClick={onBack} className="self-start text-blue-600 mb-2"> | ||||||||||||||||||||||||
| ← Back | ||||||||||||||||||||||||
| </button> | ||||||||||||||||||||||||
| <div className="p-4 max-w-md mx-auto w-full"> | ||||||||||||||||||||||||
| <h2 className="text-2xl font-bold mb-4">Traffic Sign 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={() => setCameraError("Could not access camera. Please check permissions.")} | ||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||
| ) : ( | ||||||||||||||||||||||||
| <div className="relative"> | ||||||||||||||||||||||||
| <img src={imgSrc} alt="Captured" className="w-full" /> | ||||||||||||||||||||||||
| {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={detectTrafficSign} | ||||||||||||||||||||||||
| disabled={loading} | ||||||||||||||||||||||||
| className={`bg-red-600 text-white px-6 py-2 rounded-full font-semibold shadow-md hover:bg-red-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'} | ||||||||||||||||||||||||
| </button> | ||||||||||||||||||||||||
| </> | ||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| <p className="mt-4 text-sm text-gray-600 text-center"> | ||||||||||||||||||||||||
| Point camera at a traffic sign to check for damage or vandalism. | ||||||||||||||||||||||||
| </p> | ||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| export default TrafficSignDetector; | ||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,14 @@ | ||||||
| const API_URL = import.meta.env.VITE_API_URL || ''; | ||||||
| const _getApiUrl = () => { | ||||||
| if (typeof process !== 'undefined' && process.env && process.env.NODE_ENV === 'test') { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Avoid using Prompt for AI agents
Suggested change
|
||||||
| return ''; | ||||||
| } | ||||||
| try { | ||||||
| return import.meta.env.VITE_API_URL || ''; | ||||||
| } catch (e) { | ||||||
| return ''; | ||||||
| } | ||||||
| }; | ||||||
| const API_URL = _getApiUrl(); | ||||||
|
|
||||||
| let authToken = localStorage.getItem('token'); | ||||||
|
|
||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.
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.
P2: This new component substantially duplicates existing detector code, which increases maintenance cost and makes bug fixes harder to propagate consistently.
Prompt for AI agents