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.

217 lines
6.1 KiB

1 week ago
  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 } 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. useEffect(() => {
  97. const openCamera = async () => {
  98. try {
  99. const stream = await navigator.mediaDevices.getUserMedia({
  100. video: true,
  101. });
  102. if (videoRef.current) {
  103. videoRef.current.srcObject = stream;
  104. }
  105. } catch (err) {
  106. console.error("Error accessing the camera", err);
  107. }
  108. };
  109. openCamera();
  110. }, []);
  111. // Capturing image from video stream
  112. const captureImage = () => {
  113. const video = videoRef.current;
  114. const canvas = canvasRef.current;
  115. if (video && canvas) {
  116. const context = canvas.getContext("2d");
  117. if (context) {
  118. context.drawImage(video, 0, 0, canvas.width, canvas.height);
  119. canvas.toBlob((blob) => {
  120. if (blob) {
  121. const file = new File([blob], "captured-image.png", {
  122. type: "image/png",
  123. });
  124. callApi(file);
  125. }
  126. }, "image/png");
  127. }
  128. }
  129. };
  130. // Handling image upload
  131. const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
  132. const file = event.target.files?.[0];
  133. if (file) {
  134. callApi(file); // Calling the API with the uploaded file
  135. }
  136. };
  137. // Calling API with the provided image file
  138. const callApi = async (file: File) => {
  139. const formData = new FormData();
  140. formData.append("image", file); // Appending the file to the form data
  141. try {
  142. const startTime = performance.now();
  143. const response = await fetch(
  144. `${process.env.NEXT_PUBLIC_BASE_URL}/search`,
  145. {
  146. method: "POST",
  147. body: formData, // Sending the form data
  148. }
  149. );
  150. // Ending measuring time
  151. const endTime = performance.now();
  152. const totalTime = (endTime - startTime) / 1000;
  153. if (response.ok) {
  154. const data = await response.json();
  155. console.log("API call successful", data);
  156. alert(
  157. `Search result: ${data.message}\nName: ${
  158. data?.name
  159. }\nTime taken: ${totalTime.toFixed(2)} seconds`
  160. );
  161. } else {
  162. console.error("API call failed");
  163. }
  164. } catch (error) {
  165. console.error("Error calling API", error);
  166. }
  167. };
  168. return (
  169. <div className="search-container">
  170. <h2>Search</h2>
  171. <div className="video-container">
  172. <video ref={videoRef} autoPlay playsInline className="video" />
  173. </div>
  174. <button type="button" onClick={captureImage} className="capture-button">
  175. Capture Image
  176. </button>
  177. <div>Or</div>
  178. <div className="upload-container">
  179. <label htmlFor="upload" className="upload-label">
  180. Upload Image
  181. </label>
  182. <input
  183. type="file"
  184. id="upload"
  185. accept="image/*"
  186. onChange={handleFileChange}
  187. className="upload-input"
  188. />
  189. </div>
  190. <canvas
  191. ref={canvasRef}
  192. style={{ display: "none" }}
  193. width="640"
  194. height="480"
  195. />
  196. </div>
  197. );
  198. };
  199. export default Search;