sml2h3 / ddddocr-fastapi

使用ddddocr的最简api搭建项目,支持docker
802 stars 358 forks source link

调用ocr接口返回500错误码 #40

Open minyongbing opened 1 month ago

minyongbing commented 1 month ago

使用表单提交参数为: images:"/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8U。。。。。。。" probability:false png_fix:false

返回: { "code": 500, "message": "'NoneType' object has no attribute 'size'", "data": null } 谁知道什么原因么,这个错误是什么意思?

wyourname commented 1 month ago

是代码逻辑有点问题,你需要修改一下这个接口的逻辑,具体什么接口你需要提供出来才能为你解答

NuyoahCx330 commented 1 month ago

func OcrServer(imagepath string) { apiURL := "http://127.0.0.1:8000/ocr" imageData, err := os.ReadFile(imagepath) if err != nil { log.Panicln("读取图片失败") return } base64Image := base64.StdEncoding.EncodeToString(imageData) log.Println(base64Image) data := url.Values{} data.Set("image", base64Image) data.Set("probability", "false") data.Set("png_fix", "false") resp, err := http.PostForm(apiURL, data) if err != nil { log.Println("请求OCR失败", err) return } defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
    log.Println("获取OCR返回结果失败", err)
    return
}
fmt.Println(string(body))

} 请求返回了同样的错误

NuyoahCx330 commented 1 month ago

file.size 会抛异常去掉就好了 @app.post("/ocr", response_model=APIResponse) async def ocr_endpoint( file: Optional[UploadFile] = File(None), image: Optional[str] = Form(None), probability: bool = Form(False), charsets: Optional[str] = Form(None), png_fix: bool = Form(False) ): try:

去除了 file.size == 0 判断

    if  image is  None:
        return APIResponse(code=400, message="Either file or image must be provided")
    image_bytes = await decode_image(file or image)
    result = ocr_service.ocr_classification(image_bytes, probability, charsets, png_fix)
    return APIResponse(code=200, message="Success", data=result)
except Exception as e:
    return APIResponse(code=500, message=str(e))