添加了通过识别图片导入课表的模块
This commit is contained in:
44
main.go
44
main.go
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -16,6 +17,7 @@ import (
|
||||
"jwts/config"
|
||||
grpcClient "jwts/grpc"
|
||||
"jwts/parser"
|
||||
"jwts/recognizer"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -93,6 +95,48 @@ func runHTTPServer() {
|
||||
})
|
||||
})
|
||||
|
||||
// 课表图片识别接口
|
||||
r.POST("/recognize", func(c *gin.Context) {
|
||||
// 读取上传的图片文件
|
||||
file, header, err := c.Request.FormFile("image")
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "请上传图片文件", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
log.Printf("收到图片上传: %s, 大小: %d bytes", header.Filename, header.Size)
|
||||
|
||||
// 读取图片数据
|
||||
imageData, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "读取图片失败", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 调用识别模块
|
||||
courses, err := recognizer.RecognizeFromBytes(imageData)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "识别失败", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 打印识别结果
|
||||
recognizer.PrintCourses(courses)
|
||||
|
||||
// 保存到JSON文件
|
||||
err = recognizer.SaveToJSON(courses, "schedule_result.json")
|
||||
if err != nil {
|
||||
log.Printf("保存JSON失败: %v", err)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "识别成功",
|
||||
"courses": courses,
|
||||
"count": len(courses),
|
||||
})
|
||||
})
|
||||
|
||||
// 任务处理接口
|
||||
r.POST("/task", func(c *gin.Context) {
|
||||
// 这里可以根据请求体参数决定执行什么任务
|
||||
|
||||
Reference in New Issue
Block a user