From a0cd182de987cdc18aca995a894e53b567521e71 Mon Sep 17 00:00:00 2001 From: LKKlein <1172977438@qq.com> Date: Fri, 6 Nov 2020 11:42:15 +0800 Subject: [PATCH] close unused gocv.Mat --- deploy/paddleocr-go/ocr/core.go | 3 +++ deploy/paddleocr-go/ocr/ocr_rec.go | 5 +++-- deploy/paddleocr-go/ocr/postprocess.go | 1 + deploy/paddleocr-go/ocr/preprocess.go | 2 -- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/deploy/paddleocr-go/ocr/core.go b/deploy/paddleocr-go/ocr/core.go index 11986fdf..6674f237 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 d0d0ffa5..4a62cf07 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 ea79d884..53a8184b 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 a0939e76..19b335b5 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}) -- GitLab