diff --git a/deploy/paddleocr-go/ocr/core.go b/deploy/paddleocr-go/ocr/core.go index 11986fdf8862e94787cb7a0ff3866e94c41b7a8f..6674f237df0423f4bc47cb995baf34ddce8443b0 100644 --- a/deploy/paddleocr-go/ocr/core.go +++ b/deploy/paddleocr-go/ocr/core.go @@ -149,6 +149,7 @@ func (sys *TextPredictSystem) getRotateCropImage(img gocv.Mat, box [][]int) gocv func (sys *TextPredictSystem) Run(img gocv.Mat) []OCRText { srcimg := gocv.NewMat() + defer srcimg.Close() img.CopyTo(&srcimg) boxes := sys.detector.Run(img) if len(boxes) == 0 { @@ -224,6 +225,7 @@ func (ocr *OCRSystem) predictHandler(w http.ResponseWriter, r *http.Request) { io.Copy(&buf, file) img, err2 := gocv.IMDecode(buf.Bytes(), gocv.IMReadColor) + defer img.Close() if err2 != nil { w.Write([]byte(err2.Error())) return @@ -252,6 +254,7 @@ func (ocr *OCRSystem) PredictDirImages(dirname string) map[string][]OCRText { for i := 0; i < len(imgs); i++ { imgname := imgs[i] img := ReadImage(imgname) + defer img.Close() res := ocr.PredictOneImage(img) results[imgname] = res } diff --git a/deploy/paddleocr-go/ocr/ocr_rec.go b/deploy/paddleocr-go/ocr/ocr_rec.go index d0d0ffa5521044b126d9ea4349f076a83a89f5d2..4a62cf077bc9f38db742e0cdafb536700eae605e 100644 --- a/deploy/paddleocr-go/ocr/ocr_rec.go +++ b/deploy/paddleocr-go/ocr/ocr_rec.go @@ -24,7 +24,8 @@ func NewTextRecognizer(modelDir string, args map[string]interface{}) *TextRecogn shapes[i] = s.(int) } } - labelpath := getString(args, "rec_char_dict_path", "./config/ppocr_keys_v1.txt") + home, _ := os.UserHomeDir() + labelpath := getString(args, "rec_char_dict_path", home+"/.paddleocr/rec/ppocr_keys_v1.txt") labels := readLines2StringSlice(labelpath) if getBool(args, "use_space_char", true) { labels = append(labels, " ") @@ -38,7 +39,6 @@ func NewTextRecognizer(modelDir string, args map[string]interface{}) *TextRecogn labels: labels, } if checkModelExists(modelDir) { - home, _ := os.UserHomeDir() modelDir, _ = downloadModel(home+"/.paddleocr/rec/ch", modelDir) } else { log.Panicf("rec model path: %v not exist! Please check!", modelDir) @@ -75,6 +75,7 @@ func (rec *TextRecognizer) Run(imgs []gocv.Mat, bboxes [][][]int) []OCRText { for k := i; k < j; k++ { data := crnnPreprocess(imgs[k], rec.shape, []float32{0.5, 0.5, 0.5}, []float32{0.5, 0.5, 0.5}, 255.0, maxwhratio, rec.charType) + defer imgs[k].Close() copy(normimgs[(k-i)*c*h*w:], data) } diff --git a/deploy/paddleocr-go/ocr/postprocess.go b/deploy/paddleocr-go/ocr/postprocess.go index ea79d8841bab15890866d184551087ec2c283c40..53a8184b192262555a226a33f613dd89a42d8a2a 100644 --- a/deploy/paddleocr-go/ocr/postprocess.go +++ b/deploy/paddleocr-go/ocr/postprocess.go @@ -92,6 +92,7 @@ func (d *DBPostProcess) boxScoreFast(array [][]float32, pred gocv.Mat) float64 { ymax := clip(int(math.Ceil(float64(maxf(boxY)))), 0, height-1) mask := gocv.NewMatWithSize(ymax-ymin+1, xmax-xmin+1, gocv.MatTypeCV8UC1) + defer mask.Close() ppt := make([][]image.Point, 1) ppt[0] = make([]image.Point, 4) ppt[0][0] = image.Point{int(array[0][0]) - xmin, int(array[0][1]) - ymin} diff --git a/deploy/paddleocr-go/ocr/preprocess.go b/deploy/paddleocr-go/ocr/preprocess.go index a0939e76d6b289b7b90759fd1c79695d956afbbc..19b335b5aa4205ad27c71673543076c652abde20 100644 --- a/deploy/paddleocr-go/ocr/preprocess.go +++ b/deploy/paddleocr-go/ocr/preprocess.go @@ -59,7 +59,6 @@ func resizeByMaxLen(img gocv.Mat, maxLen int) (gocv.Mat, int, int) { func normPermute(img gocv.Mat, mean []float32, std []float32, scaleFactor float32) []float32 { img.ConvertTo(&img, gocv.MatTypeCV32F) img.DivideFloat(scaleFactor) - defer img.Close() c := gocv.Split(img) data := make([]float32, img.Rows()*img.Cols()*img.Channels()) @@ -154,7 +153,6 @@ func crnnPreprocess(img gocv.Mat, resizeShape []int, mean []float32, std []float img.DivideFloat(scaleFactor) img.SubtractScalar(gocv.NewScalar(float64(mean[0]), float64(mean[1]), float64(mean[2]), 0)) img.DivideScalar(gocv.NewScalar(float64(std[0]), float64(std[1]), float64(std[2]), 0)) - defer img.Close() if resizeW < imgW { gocv.CopyMakeBorder(img, &img, 0, 0, 0, imgW-resizeW, gocv.BorderConstant, color.RGBA{0, 0, 0, 0})