import React, { useRef, useEffect } from "react"; import "./Search.css"; // Import CSS for styling import { Button } from "../ui/button"; import { toast } from "@/hooks/use-toast"; const Search: React.FC = () => { const videoRef = useRef(null); const canvasRef = useRef(null); useEffect(() => { const openCamera = async () => { try { const stream = await navigator.mediaDevices.getUserMedia({ video: true, }); if (videoRef.current) { videoRef.current.srcObject = stream; } } catch (err) { console.error("Error accessing the camera", err); } }; openCamera(); }, []); // Capturing image from video stream const captureImage = () => { const video = videoRef.current; const canvas = canvasRef.current; if (video && canvas) { const context = canvas.getContext("2d"); if (context) { context.drawImage(video, 0, 0, canvas.width, canvas.height); canvas.toBlob((blob) => { if (blob) { const file = new File([blob], "captured-image.png", { type: "image/png", }); callApi(file); } }, "image/png"); } } }; // Handling image upload const handleFileChange = (event: React.ChangeEvent) => { const file = event.target.files?.[0]; if (file) { callApi(file); // Calling the API with the uploaded file } }; // Calling API with the provided image file const callApi = async (file: File) => { const formData = new FormData(); formData.append("image", file); // Appending the file to the form data try { const startTime = performance.now(); const response = await fetch( `${process.env.NEXT_PUBLIC_BASE_URL}/search`, { method: "POST", body: formData, // Sending the form data } ); // Ending measuring time const endTime = performance.now(); const totalTime = (endTime - startTime) / 1000; if (response.ok) { const data = await response.json(); console.log("API call successful", data); toast({ title: data.message, description: `Name: ${data?.name}\n\nTime taken: ${totalTime.toFixed( 2 )} seconds`, }); } else { console.error("API call failed"); } } catch (error) { console.error("Error calling API", error); } }; return (

Search

Or
); }; export default Search;