Skip to content

Commit ed7b80e

Browse files
committed
search button update
1 parent 2e585e8 commit ed7b80e

File tree

2 files changed

+55
-36
lines changed

2 files changed

+55
-36
lines changed

src/components/utilities.ts

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
11

22
export const drawRect = (detections:any, ctx:any) =>{
3-
// Loop through each prediction
4-
detections.forEach((prediction:any) => {
3+
try{
4+
// Loop through each prediction
5+
detections.forEach((prediction:any) => {
6+
const lastElem=detections[detections.length-1]
7+
8+
// Extract boxes and classes
9+
const [x, y, width, height] = prediction['bbox'];
10+
const text = prediction['class'];
511

6-
// Extract boxes and classes
7-
const [x, y, width, height] = prediction['bbox'];
8-
const text = prediction['class'];
12+
// Create a new instance of SpeechSynthesisUtterance
13+
var speech = new SpeechSynthesisUtterance(lastElem['class']);
914

10-
// Create a new instance of SpeechSynthesisUtterance
11-
var speech = new SpeechSynthesisUtterance(text);
15+
// Set the voice, rate, pitch, and language if desired
16+
speech.lang = 'en-US'; // You can change the language
17+
speech.rate = 1; // Speed (default is 1, range: 0.1 to 10)
18+
speech.pitch = 1; // Pitch (default is 1, range: 0 to 2)
1219

13-
// Set the voice, rate, pitch, and language if desired
14-
speech.lang = 'en-US'; // You can change the language
15-
speech.rate = 1; // Speed (default is 1, range: 0.1 to 10)
16-
speech.pitch = 1; // Pitch (default is 1, range: 0 to 2)
20+
// Speak the text
21+
window.speechSynthesis.speak(speech);
1722

18-
// Speak the text
19-
window.speechSynthesis.speak(speech);
23+
// Set styling
24+
const color = Math.floor(Math.random()*16777215).toString(16);
25+
ctx.strokeStyle = '#' + color
26+
ctx.font = '18px Arial';
2027

21-
// Set styling
22-
const color = Math.floor(Math.random()*16777215).toString(16);
23-
ctx.strokeStyle = '#' + color
24-
ctx.font = '18px Arial';
25-
26-
// Draw rectangles and text
27-
ctx.beginPath();
28-
ctx.fillStyle = '#' + color
29-
ctx.fillText(text, x, y);
30-
ctx.rect(x, y, width, height);
31-
ctx.stroke();
32-
});
28+
// Draw rectangles and text
29+
ctx.beginPath();
30+
ctx.fillStyle = '#' + color
31+
ctx.fillText(text, x, y);
32+
ctx.rect(x, y, width, height);
33+
ctx.stroke();
34+
});
35+
}catch(error:any){
36+
console.log(error.message)
37+
}
3338
}

src/pages/MainPage.tsx

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Import dependencies
22
import { useRef, useEffect, useState, useContext } from "react";
3+
import { useNavigate } from "react-router-dom";
34
import Webcam from "react-webcam";
45
import * as tf from "@tensorflow/tfjs";
56
//import * as cpu from "@tensorflow/tfjs-backend-cpu";
@@ -10,8 +11,10 @@ import { GlobalContext } from "../context";
1011
import { IoSearch } from "react-icons/io5";
1112

1213
export default function MainPage() {
14+
const navigate=useNavigate();
1315
const { videoConstraints }=useContext(GlobalContext);
1416
const [isLoading, setIsLoading]=useState(true)
17+
const [object,setObject]=useState<any>([]);
1518
const webcamRef:any = useRef(null);
1619
const canvasRef:any = useRef(null);
1720

@@ -53,13 +56,21 @@ export default function MainPage() {
5356
// Draw mesh
5457
const ctx = canvasRef.current.getContext("2d");
5558
drawRect(obj, ctx);
59+
setObject(obj)
5660
}
5761
};
5862

5963
function capture(){
60-
const imageSrc:any = webcamRef.current.getScreenshot();
61-
localStorage.setItem("capture",imageSrc)
62-
console.log(imageSrc)
64+
try{
65+
const imageSrc:any = webcamRef.current.getScreenshot();
66+
if(object.length>0){
67+
localStorage.setItem("capture",imageSrc)
68+
localStorage.setItem("query",object[object.length-1]['class'])
69+
navigate("/search")
70+
}
71+
}catch(error:any){
72+
console.log(error.message)
73+
}
6374
}
6475

6576
useEffect(()=>{
@@ -73,7 +84,7 @@ export default function MainPage() {
7384
<p style={{fontSize:14, color:"white", textAlign:"center"}}>Loading, please wait...</p>
7485
</div>
7586
):(
76-
<div className="App">
87+
<div className="App h-screen">
7788
<header className="App-header">
7889
<Webcam
7990
ref={webcamRef}
@@ -85,8 +96,11 @@ export default function MainPage() {
8596
left: 0,
8697
right: 0,
8798
textAlign: "center",
88-
zIndex: 9
99+
zIndex: 9,
100+
height:videoConstraints.height,
101+
width:videoConstraints.width
89102
}}
103+
screenshotFormat="image/png"
90104
height={videoConstraints.height}
91105
width={videoConstraints.width}
92106
videoConstraints={videoConstraints}
@@ -101,18 +115,18 @@ export default function MainPage() {
101115
left: 0,
102116
right: 0,
103117
textAlign: "center",
104-
zIndex: 10
118+
zIndex: 10,
119+
height:videoConstraints.height,
120+
width:videoConstraints.width
105121
}}
106-
height={videoConstraints.height}
107-
width={videoConstraints.width}
108122
/>
109-
<div className="fixed bottom-0 left-0 right-0 z-12 h-[180px]">
123+
<div className="fixed bottom-0 left-0 right-0 z-20 h-[180px]">
110124
<div className="h-fit w-full flex gap-5 flex-col items-center justify-center">
111125
<div className="rounded-md bg-black p-2">
112126
<p className="text-[15px]">Tap shutter button to search</p>
113127
</div>
114-
<button className="bg-white rounded-[100px] hover:bg-gray-500 w-[50px] h-[50px] flex items-center justify-center">
115-
<IoSearch className="w-[22px] text-black h-[22px]"/>
128+
<button onClick={capture} className="bg-white rounded-[100px] hover:bg-blue-500 w-[50px] h-[50px] flex items-center justify-center">
129+
<IoSearch className="w-[22px] hover:text-white text-black h-[22px]"/>
116130
</button>
117131
</div>
118132
</div>

0 commit comments

Comments
 (0)