1
1
// Import dependencies
2
2
import { useRef , useEffect , useState , useContext } from "react" ;
3
+ import { useNavigate } from "react-router-dom" ;
3
4
import Webcam from "react-webcam" ;
4
5
import * as tf from "@tensorflow/tfjs" ;
5
6
//import * as cpu from "@tensorflow/tfjs-backend-cpu";
@@ -10,8 +11,10 @@ import { GlobalContext } from "../context";
10
11
import { IoSearch } from "react-icons/io5" ;
11
12
12
13
export default function MainPage ( ) {
14
+ const navigate = useNavigate ( ) ;
13
15
const { videoConstraints } = useContext ( GlobalContext ) ;
14
16
const [ isLoading , setIsLoading ] = useState ( true )
17
+ const [ object , setObject ] = useState < any > ( [ ] ) ;
15
18
const webcamRef :any = useRef ( null ) ;
16
19
const canvasRef :any = useRef ( null ) ;
17
20
@@ -53,13 +56,21 @@ export default function MainPage() {
53
56
// Draw mesh
54
57
const ctx = canvasRef . current . getContext ( "2d" ) ;
55
58
drawRect ( obj , ctx ) ;
59
+ setObject ( obj )
56
60
}
57
61
} ;
58
62
59
63
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
+ }
63
74
}
64
75
65
76
useEffect ( ( ) => {
@@ -73,7 +84,7 @@ export default function MainPage() {
73
84
< p style = { { fontSize :14 , color :"white" , textAlign :"center" } } > Loading, please wait...</ p >
74
85
</ div >
75
86
) :(
76
- < div className = "App" >
87
+ < div className = "App h-screen " >
77
88
< header className = "App-header" >
78
89
< Webcam
79
90
ref = { webcamRef }
@@ -85,8 +96,11 @@ export default function MainPage() {
85
96
left : 0 ,
86
97
right : 0 ,
87
98
textAlign : "center" ,
88
- zIndex : 9
99
+ zIndex : 9 ,
100
+ height :videoConstraints . height ,
101
+ width :videoConstraints . width
89
102
} }
103
+ screenshotFormat = "image/png"
90
104
height = { videoConstraints . height }
91
105
width = { videoConstraints . width }
92
106
videoConstraints = { videoConstraints }
@@ -101,18 +115,18 @@ export default function MainPage() {
101
115
left : 0 ,
102
116
right : 0 ,
103
117
textAlign : "center" ,
104
- zIndex : 10
118
+ zIndex : 10 ,
119
+ height :videoConstraints . height ,
120
+ width :videoConstraints . width
105
121
} }
106
- height = { videoConstraints . height }
107
- width = { videoConstraints . width }
108
122
/>
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]" >
110
124
< div className = "h-fit w-full flex gap-5 flex-col items-center justify-center" >
111
125
< div className = "rounded-md bg-black p-2" >
112
126
< p className = "text-[15px]" > Tap shutter button to search</ p >
113
127
</ 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]" />
116
130
</ button >
117
131
</ div >
118
132
</ div >
0 commit comments