diff --git a/.gitignore b/.gitignore index ddf2a61..67dd9e4 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,8 @@ env/ .idea/ .claude/ +uploads/* + nn/dataset/toy/* nn/dataset/benchmark/* nn/models/*.pth diff --git a/app.go b/app.go index 0386aa3..c4ec5f7 100644 --- a/app.go +++ b/app.go @@ -2,7 +2,11 @@ package main import ( "context" + "encoding/base64" "fmt" + "os" + "path/filepath" + "time" ) // App struct @@ -36,3 +40,40 @@ func (a *App) Register() string { println("1111") return "" } + +func (a *App) UploadImage(data []byte, filename string) ImageResult { + uploadsDir := "./uploads" + err := os.MkdirAll(uploadsDir, 0755) + if err != nil { + return ImageResult{Code: 1, Message: "failed", Data: fmt.Sprintf("创建目录失败: %v", err)} + } + + ext := filepath.Ext(filename) + newFilename := fmt.Sprintf("%d%s", time.Now().UnixMilli(), ext) + filePath := filepath.Join(uploadsDir, newFilename) + + err = os.WriteFile(filePath, data, 0644) + if err != nil { + return ImageResult{Code: 1, Message: "failed", Data: fmt.Sprintf("保存文件失败: %v", err)} + } + + return ImageResult{Code: 0, Message: "success", Data: newFilename} +} + +func (a *App) GetImage(filename string) ImageResult { + filePath := filepath.Join("./uploads", filename) + fmt.Println("filePath", filePath) + data, err := os.ReadFile(filePath) + if err != nil { + return ImageResult{Code: 1, Message: "failed", Data: fmt.Sprintf("打开文件路径失败: %v", err)} + } + ext := filepath.Ext(filename) + mimeType := "image/jpeg" + if ext == ".png" { + mimeType = "image/png" + } + // app.go - GetImage + return ImageResult{Code: 0, Message: "success", Data: fmt.Sprintf("data:image/%s;base64,%s", mimeType, base64.StdEncoding.EncodeToString(data))} + + // return ImageResult{Code: 1, Message: "failed", Data: data} +} diff --git a/frontend/src/components/Main.tsx b/frontend/src/components/Main.tsx index 5e24c53..cd8279d 100644 --- a/frontend/src/components/Main.tsx +++ b/frontend/src/components/Main.tsx @@ -1,4 +1,71 @@ +import { useState, useEffect, useRef } from 'preact/hooks' + + const Main = () => { + const fileInputRef = useRef(null) + const [fileName, setFileName] = useState('') + const [fileSrc, setFileSrc] = useState('') + + const [step, setStep] = useState(0) + + const handleUploadClick = () => { + fileInputRef.current?.click() + } + + const handleFileChange = (e: Event) => { + const target = e.target as HTMLInputElement + const file = target.files?.[0] + if (file) { + const reader = new FileReader() + reader.onload = async function() { + const arrayBuffer = reader.result as ArrayBuffer + const uint8Array = new Uint8Array(arrayBuffer) + + const result = await (window as any).go.main.App.UploadImage( + Array.from(uint8Array), + file.name + ) + + if (result.code === 0) { + setFileName(result.data) + + const imgResult = await (window as any).go.main.App.GetImage(result.data) + if (imgResult.code === 0) { + setFileSrc(imgResult.data) + setStep(1) + } + } + } + reader.readAsArrayBuffer(file) + } + } + + const resetUpload = () => { + if (fileInputRef.current) { + fileInputRef.current.value = '' + } + setFileSrc('') + } + + const handleRemovePhoto = (e: Event) => { + e.stopPropagation() + setStep(0) + resetUpload() + } + + const handleDetect = () => { + setStep(2) + + setTimeout(() => { + setStep(3) + }, 1000) + } + + const handleTryOther = () => { + setStep(0) + resetUpload() + } + return (
@@ -11,100 +78,92 @@ const Main = () => {

基于深度残差网络
-
-
+
-
- - + {fileSrc.length > 0 && step == 1 ? +
+ + +
: null + } + {fileSrc.length == 0 && step == 0 ? + <> +
+ + + +

点击或拖拽上传

+

支持JPG、PNG、JPEG,最大不超过2MB

+
+ + : null + } + {fileSrc.length > 0 && step == 2 ? + <> +
+
+

正在检测...

+

请耐心等待~

+
+ : null + } +
+ {fileSrc.length > 0 && step == 1 ? +
- {/* - */} -
- - {/* */} + 开始检测 + : null + }
- - {/*