From 6cc29a24e832b9d5613043e3e0f67d866c8fe442 Mon Sep 17 00:00:00 2001 From: siar Date: Mon, 12 Feb 2024 13:30:29 +0000 Subject: [PATCH] It just works --- frontend/src/api.ts | 36 ++++++++++-------------------- frontend/src/components/Canvas.tsx | 10 ++++----- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/frontend/src/api.ts b/frontend/src/api.ts index 8bf513ef7..713e31d8e 100644 --- a/frontend/src/api.ts +++ b/frontend/src/api.ts @@ -1,34 +1,22 @@ import axios from "axios"; -import { useState } from "react"; const baseURL = "http://localhost:8000/" -interface GetResponse { +interface PostState { response: string | undefined, - error: string | undefined, - postData: (url: string, body: any) => Promise + error: string | undefined } - -export function useAPIPost(): GetResponse { - interface State { - response: string | undefined, - error: string | undefined - } - const [state, setState] = useState({ +export async function APIPost(url: string, body: any): Promise { + let state: PostState = { response: undefined, error: undefined - }); - async function postData(url: string, body: any) { - try { - let response = await axios.post(baseURL + url, { data: body }); - setState({ ...state, response: response.data }); - } catch (error: any) { - setState({ ...state, error: error }); - } - } + }; - return { - response: state.response, - error: state.error, - postData: postData + try { + let response = await axios.post(baseURL + url, { data: body }); + state = { ...state, response: response.data }; + } catch (error: any) { + state = { ...state, error: error }; + } finally { + return state } } \ No newline at end of file diff --git a/frontend/src/components/Canvas.tsx b/frontend/src/components/Canvas.tsx index c6d04637f..3f353d9a4 100644 --- a/frontend/src/components/Canvas.tsx +++ b/frontend/src/components/Canvas.tsx @@ -3,7 +3,7 @@ import { Hands } from "@mediapipe/hands"; import { ReactElement, useRef, CanvasHTMLAttributes, useEffect } from 'react'; import { drawConnectors, drawLandmarks, NormalizedLandmark } from '@mediapipe/drawing_utils'; import { HAND_CONNECTIONS, InputImage } from '@mediapipe/holistic'; -import { useAPIPost } from "../api"; +import { APIPost } from "../api"; interface Landmark { x: string; @@ -15,7 +15,6 @@ export default function Canvas(props?: CanvasHTMLAttributes): const canvasRef = useRef(null); const videoRef = useRef(null); - const apiGet = useAPIPost(); useEffect(() => { const canvas = canvasRef.current ?? undefined; @@ -60,7 +59,6 @@ export default function Canvas(props?: CanvasHTMLAttributes): canvasCtx.drawImage( results.image, 0, 0, canvas.width, canvas.height); if (results.multiHandLandmarks) { - // console.log(results.multiHandLandmarks) for (const landmarks of results.multiHandLandmarks) { let newLandmarks: Landmark[] = [] landmarks.forEach((element, i) => { @@ -72,9 +70,9 @@ export default function Canvas(props?: CanvasHTMLAttributes): }); drawConnectors(canvasCtx, landmarks, HAND_CONNECTIONS, { color: '#00FF00', lineWidth: 2 }); drawLandmarks(canvasCtx, landmarks, { color: '#FF0000', lineWidth: 2 }); - await apiGet.postData(`annotation`, newLandmarks); - if (apiGet.response && !apiGet.error) console.log(apiGet.response) - else if (apiGet.error) console.log(apiGet.error) + let postState = await APIPost(`annotation`, newLandmarks); + if (postState.response && !postState.error) console.log(postState.response) + else if (postState.error) console.log(postState.error) } } canvasCtx.restore();