diff --git a/src/App.tsx b/src/App.tsx index d3de668..5bd6f94 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,107 +1,33 @@ -// Import dependencies -import { useRef, useEffect, useState } from "react"; -import Webcam from "react-webcam"; -import * as tf from "@tensorflow/tfjs"; -//import * as cpu from "@tensorflow/tfjs-backend-cpu"; -//import * as webgl from "@tensorflow/tfjs-backend-webgl"; -import * as cocossd from "@tensorflow-models/coco-ssd"; import "./App.css"; -import { drawRect } from "./utilities"; - -function App() { - const [isLoading, setIsLoading]=useState(true) - const webcamRef:any = useRef(null); - const canvasRef:any = useRef(null); - - tf.setBackend("webgl"); - // Main function - const runCoco = async () => { - const net = await cocossd.load(); - console.log("cocossd model loaded."); - setIsLoading(false) - // Loop and detect hands - setInterval(() => { - detect(net); - }, 10); - }; - - const detect = async (net:any) => { - // Check data is available - if ( - typeof webcamRef.current !== "undefined" && - webcamRef.current !== null && - webcamRef.current.video.readyState === 4 - ) { - // Get Video Properties - const video:any = webcamRef.current.video; - const videoWidth = webcamRef.current.video.videoWidth; - const videoHeight = webcamRef.current.video.videoHeight; - - // Set video width - webcamRef.current.video.width = videoWidth; - webcamRef.current.video.height = videoHeight; - - // Set canvas height and width - canvasRef.current.width = videoWidth; - canvasRef.current.height = videoHeight; - - // Make Detections - const obj = await net.detect(video); - // console.log(obj) - // Draw mesh - const ctx = canvasRef.current.getContext("2d"); - drawRect(obj, ctx); +import { useEffect, useState } from "react"; +import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom"; +import MainPage from "./pages/MainPage.tsx"; +import SearchPage from "./pages/SearchPage.tsx"; +import { GlobalContext } from "./context"; + +export default function App(){ + const API_URL=`http://127.0.0.1:5000` + const [videoConstraints, setVideoConstraints]=useState({ + /*width: 1280,*/ + height: 720, + facingMode: "user" + }) + + window.onresize=function(){ + screen.width>1080?"":setVideoConstraints({height:720, facingMode:"environment"}) } - }; - - useEffect(()=>{ - runCoco() - },[]); - return ( - <> - {isLoading&&isLoading?( -
-

Loading, please wait...

-
- ):( -
-
- - - -
-
- )} - - ); + useEffect(()=>{ + screen.width>1080?"":setVideoConstraints({height:720, facingMode:"environment"}) + },[screen.width]) + return( + + + + }/> + }/> + + + + ) } - -export default App; diff --git a/src/utilities.ts b/src/components/utilities.ts similarity index 100% rename from src/utilities.ts rename to src/components/utilities.ts diff --git a/src/context/index.ts b/src/context/index.ts new file mode 100644 index 0000000..004850e --- /dev/null +++ b/src/context/index.ts @@ -0,0 +1,19 @@ +import { createContext } from 'react' + +type ContextType={ + videoConstraints:{ + height?:number, + weigth?:number, + facingMode:string + }, + API_URL:string +} + +export const GlobalContext=createContext({ + videoConstraints:{ + /*width: 1280,*/ + height: 720, + facingMode: "user" + }, + API_URL:"" +}) diff --git a/src/pages/MainPage.tsx b/src/pages/MainPage.tsx new file mode 100644 index 0000000..d9c0c8c --- /dev/null +++ b/src/pages/MainPage.tsx @@ -0,0 +1,108 @@ +// Import dependencies +import { useRef, useEffect, useState, useContext } from "react"; +import Webcam from "react-webcam"; +import * as tf from "@tensorflow/tfjs"; +//import * as cpu from "@tensorflow/tfjs-backend-cpu"; +//import * as webgl from "@tensorflow/tfjs-backend-webgl"; +import * as cocossd from "@tensorflow-models/coco-ssd"; +import { drawRect } from "../components/utilities"; +import { GlobalContext } from "../context"; + +export default function MainPage() { + const { videoConstraints }=useContext(GlobalContext); + const [isLoading, setIsLoading]=useState(true) + const webcamRef:anyy = useRef(null); + const canvasRef:any = useRef(null); + + tf.setBackend("webgl"); + // Main function + const runCoco = async () => { + const net = await cocossd.load(); + console.log("cocossd model loaded."); + setIsLoading(false) + // Loop and detect hands + setInterval(() => { + detect(net); + }, 10); + }; + + const detect = async (net:any) => { + // Check data is available + if ( + typeof webcamRef.current !== "undefined" && + webcamRef.current !== null && + webcamRef.current.video.readyState === 4 + ) { + // Get Video Properties + const video:any = webcamRef.current.video; + const videoWidth = webcamRef.current.video.videoWidth; + const videoHeight = webcamRef.current.video.videoHeight; + + // Set video width + webcamRef.current.video.width = videoWidth; + webcamRef.current.video.height = videoHeight; + + // Set canvas height and width + canvasRef.current.width = videoWidth; + canvasRef.current.height = videoHeight; + + // Make Detections + const obj = await net.detect(video); + // console.log(obj) + // Draw mesh + const ctx = canvasRef.current.getContext("2d"); + drawRect(obj, ctx); + } + }; + + useEffect(()=>{ + runCoco() + },[]); + + return ( + <> + {isLoading&&isLoading?( +
+

Loading, please wait...

+
+ ):( +
+
+ + + +
+
+ )} + + ); +} diff --git a/src/pages/SearchPage.tsx b/src/pages/SearchPage.tsx new file mode 100644 index 0000000..84083c1 --- /dev/null +++ b/src/pages/SearchPage.tsx @@ -0,0 +1,8 @@ + +export default function SearchPage(){ + return( + <> +

Search page

+ + ) +}