Skip to content

Commit d69e6f5

Browse files
author
salim laimeche
committed
Refactor code: Remove unused code and dependencies
1 parent 78d6c2d commit d69e6f5

File tree

7 files changed

+72
-105
lines changed

7 files changed

+72
-105
lines changed

components/History.tsx

Lines changed: 10 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client"
22

33
import { Card, CardContent, CardHeader } from "./ui/card"
4-
import { useEffect, useRef, useState } from "react"
4+
import { useEffect, useRef } from "react"
55
import { deletePictures } from "@/lib/send-detection/action"
66

77
import { UserView } from "@/lib/identity/definition"
@@ -12,34 +12,16 @@ import useHistory from "@/hooks/use-history"
1212
import HistorySelect from "./history-select"
1313
import ModelSelection from "./model-selection"
1414
import ModelLoader from "./model-loader"
15-
import { ModelComputerVision, ModelList, modelList } from "@/models/model-list"
15+
import { modelList } from "@/models/model-list"
1616
import { detect } from "@/lib/yolov8n/detect"
1717
import Image from "next/image"
18-
import { Popover, PopoverContent } from "@radix-ui/react-popover"
19-
import { PopoverTrigger } from "./ui/popover"
20-
import {
21-
Dialog,
22-
DialogContent,
23-
DialogDescription,
24-
DialogHeader,
25-
DialogTitle,
26-
DialogTrigger,
27-
} from "./ui/dialog"
28-
import { Badge } from "./ui/badge"
29-
import { BotIcon, PictureInPicture2Icon } from "lucide-react"
30-
import { Button } from "./ui/button"
3118

3219
interface IProps {
3320
user: UserView
3421
}
3522

3623
export default function History({ user }: IProps) {
37-
const ready = useTfjsBackendWeb({ backend: "webgl" })
38-
const { modelName } = useModelStore()
39-
const { model, loadModel, percentLoaded } = useModel({ ready })
4024
const { pictures, setPictures, date, setDate } = useHistory({ user })
41-
const [picture, setPicture] = useState<string>("")
42-
const canvasRef = useRef<HTMLCanvasElement>(null)
4325

4426
async function handleDeleteAllSelection() {
4527
const confirmation = window.confirm(
@@ -53,51 +35,14 @@ export default function History({ user }: IProps) {
5335
}
5436
}
5537

56-
function handleCreateCanvas() {
57-
const img = new window.Image()
58-
img.crossOrigin = "anonymous"
59-
img.src = picture
60-
img.onload = async () => {
61-
const canvas = canvasRef.current
62-
const context = canvas?.getContext("2d")
63-
if (context) {
64-
context.drawImage(img, 0, 0, canvas.width, canvas.height)
65-
}
66-
}
67-
}
68-
69-
async function handleRunDetection() {
70-
const img = new window.Image()
71-
img.crossOrigin = "anonymous"
72-
img.src = picture
73-
img.onload = async () => {
74-
const canvas = canvasRef.current
75-
const context = canvas?.getContext("2d")
76-
if (context) {
77-
detect(img, model, canvas)
78-
}
79-
}
80-
}
81-
8238
return (
83-
<div className="grid grid-cols-1 gap-4 lg:grid-cols-2 lg:gap-8">
39+
<div className="grid grid-cols-1 gap-4 lg:grid-cols-2 lg:gap-8 mt-16">
8440
<HistorySelect
8541
date={date}
8642
setDate={setDate}
8743
pictures={pictures}
8844
handleDeleteAllSelection={handleDeleteAllSelection}
89-
loadModel={loadModel}
9045
/>
91-
{loadModel ? (
92-
<ModelLoader
93-
percent={percentLoaded}
94-
model={
95-
modelList.find(model => model.title === modelName) as ModelList
96-
}
97-
/>
98-
) : (
99-
<ModelSelection />
100-
)}
10146

10247
<Card className="m-3 w-full lg:col-span-2 flex-grow bg-transparent border-none">
10348
<CardHeader className="flex flex-row items-start bg-muted/50">
@@ -134,50 +79,13 @@ export default function History({ user }: IProps) {
13479
<div className="grid grid-cols-1 gap-4 lg:grid-cols-2 lg:gap-8">
13580
{pictures.map((picture, index) => (
13681
<div key={index} className="relative">
137-
<Dialog>
138-
<DialogTrigger>
139-
<Image
140-
src={picture}
141-
alt={"Image de la detection"}
142-
width={200}
143-
height={200}
144-
className="rounded-lg w-full cursor-pointer"
145-
onClick={() => {
146-
setPicture(picture)
147-
handleCreateCanvas()
148-
}}
149-
/>
150-
</DialogTrigger>
151-
<DialogContent className="max-w-full max-h-full">
152-
<DialogHeader className="flex-row">
153-
<DialogTitle className="m-2">
154-
<Badge variant="default">
155-
<PictureInPicture2Icon className="mr-2 h-4 w-4" />
156-
<span className="text-white">{index + 1}</span>
157-
</Badge>
158-
</DialogTitle>
159-
<DialogDescription>
160-
<Button
161-
variant="outline"
162-
className="bg-blue-500 text-white hover:bg-blue-600 focus:bg-blue-700 active:bg-blue-800 hover:text-white focus:text-white active:text-white"
163-
onClick={() => {
164-
handleRunDetection()
165-
}}>
166-
<BotIcon className="mr-2 h-4 w-4" />
167-
Lancer la reconnaissance
168-
</Button>
169-
</DialogDescription>
170-
</DialogHeader>
171-
<div className="flex justify-center">
172-
<canvas
173-
ref={canvasRef}
174-
width={640}
175-
height={480}
176-
className="max-w-full max-h-full rounded-lg"
177-
/>
178-
</div>
179-
</DialogContent>
180-
</Dialog>
82+
<Image
83+
src={picture}
84+
alt={"Image de la détection"}
85+
width={300}
86+
height={300}
87+
className="rounded-lg w-full cursor-pointer text-center transition duration-500"
88+
/>
18189
</div>
18290
))}
18391
</div>

components/history-select.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ interface IProps {
1515
setDate: Dispatch<SetStateAction<DateRange>>
1616
pictures?: string[]
1717
handleDeleteAllSelection: () => void
18-
loadModel: boolean
1918
}
2019

2120
export default function HistorySelect({
2221
date,
2322
setDate,
2423
pictures,
2524
handleDeleteAllSelection,
26-
loadModel,
2725
}: IProps) {
2826
return (
2927
<Card className="bg-background">
@@ -37,7 +35,6 @@ export default function HistorySelect({
3735
<Popover>
3836
<PopoverTrigger asChild>
3937
<Button
40-
disabled={loadModel}
4138
id="date"
4239
variant={"outline"}
4340
className={cn(

components/model-selection.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ export default function ModelSelection() {
4242
</DropdownMenu>
4343
</div>
4444
</CardContent>
45+
<CardContent className="p-16">
46+
<Button
47+
disabled={!modelName}
48+
variant="outline"
49+
className="w-full"
50+
onClick={() => setModel("")}>
51+
Réinitialiser
52+
</Button>
53+
</CardContent>
4554
</Card>
4655
)
4756
}

hooks/use-yolo-tfjs.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { useModelStore } from "@/lib/store/model-store"
2+
import { yoloLabels } from "@/lib/yolov8n/label"
3+
import { modelList } from "@/models/model-list"
4+
import { useEffect, useState } from "react"
5+
import YOLOTf from "yolo-tfjs"
6+
7+
interface IProps {
8+
ready: boolean
9+
}
10+
11+
export default function useYoloTfjs({ ready }: IProps) {
12+
const { modelName } = useModelStore()
13+
const [model, setModel] = useState<YOLOTf>(null)
14+
const [loadModel, setLoadModel] = useState<boolean>(false)
15+
const [percentLoaded, setPercentLoaded] = useState<number>(0)
16+
17+
const modelDef = modelList.find(model => model.title === modelName)
18+
19+
async function fetchModel() {
20+
modelName &&
21+
(setLoadModel(true),
22+
await YOLOTf.loadYoloModel(modelDef?.url, yoloLabels, {
23+
yoloVersion: "v8",
24+
onProgress(fraction: number) {
25+
setPercentLoaded(fraction * 100)
26+
},
27+
})
28+
.then(model => {
29+
setModel(model)
30+
})
31+
.catch(error => {
32+
console.error(error)
33+
}))
34+
35+
setLoadModel(false)
36+
}
37+
38+
useEffect(() => {
39+
if (ready) {
40+
fetchModel()
41+
}
42+
}, [ready, modelName])
43+
44+
return { model, loadModel, percentLoaded }
45+
}

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"tailwind-merge": "^2.4.0",
5959
"tailwindcss-animate": "^1.0.7",
6060
"uuid": "^9.0.1",
61+
"yolo-tfjs": "^0.0.0",
6162
"zod": "^3.23.8",
6263
"zustand": "^4.5.4"
6364
},

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"compilerOptions": {
3+
"target": "es2016",
34
"lib": ["dom", "dom.iterable", "esnext"],
45
"allowJs": true,
56
"skipLibCheck": true,

0 commit comments

Comments
 (0)