You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
6.3 KiB

  1. // import React, { useRef, useEffect } from "react";
  2. // import "./Search.css"; // Import CSS for styling
  3. // const Search: React.FC = () => {
  4. // const videoRef = useRef<HTMLVideoElement>(null);
  5. // const canvasRef = useRef<HTMLCanvasElement>(null);
  6. // // Automatically open the camera when the component mounts
  7. // useEffect(() => {
  8. // const openCamera = async () => {
  9. // try {
  10. // const stream = await navigator.mediaDevices.getUserMedia({
  11. // video: true,
  12. // });
  13. // if (videoRef.current) {
  14. // videoRef.current.srcObject = stream;
  15. // }
  16. // } catch (err) {
  17. // console.error("Error accessing the camera", err);
  18. // }
  19. // };
  20. // openCamera();
  21. // }, []);
  22. // // Capture image from video stream
  23. // const captureImage = () => {
  24. // const video = videoRef.current;
  25. // const canvas = canvasRef.current;
  26. // if (video && canvas) {
  27. // const context = canvas.getContext("2d");
  28. // if (context) {
  29. // context.drawImage(video, 0, 0, canvas.width, canvas.height);
  30. // canvas.toBlob((blob) => {
  31. // if (blob) {
  32. // const file = new File([blob], "captured-image.png", {
  33. // type: "image/png",
  34. // });
  35. // callApi(file);
  36. // }
  37. // }, "image/png");
  38. // }
  39. // }
  40. // };
  41. // // Call API with the captured image file
  42. // const callApi = async (file: File) => {
  43. // const formData = new FormData();
  44. // formData.append("image", file); // Append the file to the form data
  45. // try {
  46. // const startTime = performance.now();
  47. // const response = await fetch(
  48. // `${process.env.NEXT_PUBLIC_BASE_URL}/search`,
  49. // {
  50. // method: "POST",
  51. // body: formData, // Send the form data
  52. // }
  53. // );
  54. // // End measuring time
  55. // const endTime = performance.now();
  56. // const totalTime = (endTime - startTime) / 1000;
  57. // if (response.ok) {
  58. // const data = await response.json();
  59. // console.log("API call successful", data);
  60. // alert(
  61. // `Search result: ${data.message}\nName: ${
  62. // data?.name
  63. // }\nTime taken: ${totalTime.toFixed(2)} seconds`
  64. // );
  65. // } else {
  66. // console.error("API call failed");
  67. // }
  68. // } catch (error) {
  69. // console.error("Error calling API", error);
  70. // }
  71. // };
  72. // return (
  73. // <div className="search-container">
  74. // <h2>Search</h2>
  75. // <div className="video-container">
  76. // <video ref={videoRef} autoPlay playsInline className="video" />
  77. // </div>
  78. // <button type="button" onClick={captureImage} className="capture-button">
  79. // Capture Image
  80. // </button>
  81. // <canvas
  82. // ref={canvasRef}
  83. // style={{ display: "none" }}
  84. // width="640"
  85. // height="480"
  86. // />
  87. // </div>
  88. // );
  89. // };
  90. // export default Search;
  91. import React, { useRef, useEffect, useState } from "react";
  92. import "./Search.css"; // Import CSS for styling
  93. const Search: React.FC = () => {
  94. const videoRef = useRef<HTMLVideoElement>(null);
  95. const canvasRef = useRef<HTMLCanvasElement>(null);
  96. const [uploadedFile, setUploadedFile] = useState<File | null>(null);
  97. // Automatically opennig the camera when the component mounts
  98. useEffect(() => {
  99. const openCamera = async () => {
  100. try {
  101. const stream = await navigator.mediaDevices.getUserMedia({
  102. video: true,
  103. });
  104. if (videoRef.current) {
  105. videoRef.current.srcObject = stream;
  106. }
  107. } catch (err) {
  108. console.error("Error accessing the camera", err);
  109. }
  110. };
  111. openCamera();
  112. }, []);
  113. // Capturing image from video stream
  114. const captureImage = () => {
  115. const video = videoRef.current;
  116. const canvas = canvasRef.current;
  117. if (video && canvas) {
  118. const context = canvas.getContext("2d");
  119. if (context) {
  120. context.drawImage(video, 0, 0, canvas.width, canvas.height);
  121. canvas.toBlob((blob) => {
  122. if (blob) {
  123. const file = new File([blob], "captured-image.png", {
  124. type: "image/png",
  125. });
  126. callApi(file);
  127. }
  128. }, "image/png");
  129. }
  130. }
  131. };
  132. // Handling image upload
  133. const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
  134. const file = event.target.files?.[0];
  135. if (file) {
  136. setUploadedFile(file); // Storing the uploaded file in state
  137. callApi(file); // Calling the API with the uploaded file
  138. }
  139. };
  140. // Calling API with the provided image file
  141. const callApi = async (file: File) => {
  142. const formData = new FormData();
  143. formData.append("image", file); // Appending the file to the form data
  144. try {
  145. const startTime = performance.now();
  146. const response = await fetch(
  147. `${process.env.NEXT_PUBLIC_BASE_URL}/search`,
  148. {
  149. method: "POST",
  150. body: formData, // Sending the form data
  151. }
  152. );
  153. // Ending measuring time
  154. const endTime = performance.now();
  155. const totalTime = (endTime - startTime) / 1000;
  156. if (response.ok) {
  157. const data = await response.json();
  158. console.log("API call successful", data);
  159. alert(
  160. `Search result: ${data.message}\nName: ${
  161. data?.name
  162. }\nTime taken: ${totalTime.toFixed(2)} seconds`
  163. );
  164. } else {
  165. console.error("API call failed");
  166. }
  167. } catch (error) {
  168. console.error("Error calling API", error);
  169. }
  170. };
  171. return (
  172. <div className="search-container">
  173. <h2>Search</h2>
  174. <div className="video-container">
  175. <video ref={videoRef} autoPlay playsInline className="video" />
  176. </div>
  177. <button type="button" onClick={captureImage} className="capture-button">
  178. Capture Image
  179. </button>
  180. <div>Or</div>
  181. <div className="upload-container">
  182. <label htmlFor="upload" className="upload-label">
  183. Upload Image
  184. </label>
  185. <input
  186. type="file"
  187. id="upload"
  188. accept="image/*"
  189. onChange={handleFileChange}
  190. className="upload-input"
  191. />
  192. </div>
  193. <canvas
  194. ref={canvasRef}
  195. style={{ display: "none" }}
  196. width="640"
  197. height="480"
  198. />
  199. </div>
  200. );
  201. };
  202. export default Search;