Skip to content
This repository has been archived by the owner on Jul 7, 2024. It is now read-only.

Commit

Permalink
It just works
Browse files Browse the repository at this point in the history
  • Loading branch information
silasarildsen committed Feb 12, 2024
1 parent 8e686ba commit 6cc29a2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
36 changes: 12 additions & 24 deletions frontend/src/api.ts
Original file line number Diff line number Diff line change
@@ -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<void>
error: string | undefined
}

export function useAPIPost(): GetResponse {
interface State {
response: string | undefined,
error: string | undefined
}
const [state, setState] = useState<State>({
export async function APIPost(url: string, body: any): Promise<PostState> {
let state: PostState = {
response: undefined,
error: undefined
});
async function postData(url: string, body: any) {
try {
let response = await axios.post<string>(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<string>(baseURL + url, { data: body });
state = { ...state, response: response.data };
} catch (error: any) {
state = { ...state, error: error };
} finally {
return state
}
}
10 changes: 4 additions & 6 deletions frontend/src/components/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,7 +15,6 @@ export default function Canvas(props?: CanvasHTMLAttributes<HTMLCanvasElement>):

const canvasRef = useRef<HTMLCanvasElement | null>(null);
const videoRef = useRef<HTMLVideoElement | null>(null);
const apiGet = useAPIPost();

useEffect(() => {
const canvas = canvasRef.current ?? undefined;
Expand Down Expand Up @@ -60,7 +59,6 @@ export default function Canvas(props?: CanvasHTMLAttributes<HTMLCanvasElement>):
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) => {
Expand All @@ -72,9 +70,9 @@ export default function Canvas(props?: CanvasHTMLAttributes<HTMLCanvasElement>):
});
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();
Expand Down

0 comments on commit 6cc29a2

Please sign in to comment.