提交 a0cd182d 编写于 作者: L LKKlein

close unused gocv.Mat

上级 879172a5
...@@ -149,6 +149,7 @@ func (sys *TextPredictSystem) getRotateCropImage(img gocv.Mat, box [][]int) gocv ...@@ -149,6 +149,7 @@ func (sys *TextPredictSystem) getRotateCropImage(img gocv.Mat, box [][]int) gocv
func (sys *TextPredictSystem) Run(img gocv.Mat) []OCRText { func (sys *TextPredictSystem) Run(img gocv.Mat) []OCRText {
srcimg := gocv.NewMat() srcimg := gocv.NewMat()
defer srcimg.Close()
img.CopyTo(&srcimg) img.CopyTo(&srcimg)
boxes := sys.detector.Run(img) boxes := sys.detector.Run(img)
if len(boxes) == 0 { if len(boxes) == 0 {
...@@ -224,6 +225,7 @@ func (ocr *OCRSystem) predictHandler(w http.ResponseWriter, r *http.Request) { ...@@ -224,6 +225,7 @@ func (ocr *OCRSystem) predictHandler(w http.ResponseWriter, r *http.Request) {
io.Copy(&buf, file) io.Copy(&buf, file)
img, err2 := gocv.IMDecode(buf.Bytes(), gocv.IMReadColor) img, err2 := gocv.IMDecode(buf.Bytes(), gocv.IMReadColor)
defer img.Close()
if err2 != nil { if err2 != nil {
w.Write([]byte(err2.Error())) w.Write([]byte(err2.Error()))
return return
...@@ -252,6 +254,7 @@ func (ocr *OCRSystem) PredictDirImages(dirname string) map[string][]OCRText { ...@@ -252,6 +254,7 @@ func (ocr *OCRSystem) PredictDirImages(dirname string) map[string][]OCRText {
for i := 0; i < len(imgs); i++ { for i := 0; i < len(imgs); i++ {
imgname := imgs[i] imgname := imgs[i]
img := ReadImage(imgname) img := ReadImage(imgname)
defer img.Close()
res := ocr.PredictOneImage(img) res := ocr.PredictOneImage(img)
results[imgname] = res results[imgname] = res
} }
......
...@@ -24,7 +24,8 @@ func NewTextRecognizer(modelDir string, args map[string]interface{}) *TextRecogn ...@@ -24,7 +24,8 @@ func NewTextRecognizer(modelDir string, args map[string]interface{}) *TextRecogn
shapes[i] = s.(int) 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) labels := readLines2StringSlice(labelpath)
if getBool(args, "use_space_char", true) { if getBool(args, "use_space_char", true) {
labels = append(labels, " ") labels = append(labels, " ")
...@@ -38,7 +39,6 @@ func NewTextRecognizer(modelDir string, args map[string]interface{}) *TextRecogn ...@@ -38,7 +39,6 @@ func NewTextRecognizer(modelDir string, args map[string]interface{}) *TextRecogn
labels: labels, labels: labels,
} }
if checkModelExists(modelDir) { if checkModelExists(modelDir) {
home, _ := os.UserHomeDir()
modelDir, _ = downloadModel(home+"/.paddleocr/rec/ch", modelDir) modelDir, _ = downloadModel(home+"/.paddleocr/rec/ch", modelDir)
} else { } else {
log.Panicf("rec model path: %v not exist! Please check!", modelDir) 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 { ...@@ -75,6 +75,7 @@ func (rec *TextRecognizer) Run(imgs []gocv.Mat, bboxes [][][]int) []OCRText {
for k := i; k < j; k++ { for k := i; k < j; k++ {
data := crnnPreprocess(imgs[k], rec.shape, []float32{0.5, 0.5, 0.5}, 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) []float32{0.5, 0.5, 0.5}, 255.0, maxwhratio, rec.charType)
defer imgs[k].Close()
copy(normimgs[(k-i)*c*h*w:], data) copy(normimgs[(k-i)*c*h*w:], data)
} }
......
...@@ -92,6 +92,7 @@ func (d *DBPostProcess) boxScoreFast(array [][]float32, pred gocv.Mat) float64 { ...@@ -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) ymax := clip(int(math.Ceil(float64(maxf(boxY)))), 0, height-1)
mask := gocv.NewMatWithSize(ymax-ymin+1, xmax-xmin+1, gocv.MatTypeCV8UC1) mask := gocv.NewMatWithSize(ymax-ymin+1, xmax-xmin+1, gocv.MatTypeCV8UC1)
defer mask.Close()
ppt := make([][]image.Point, 1) ppt := make([][]image.Point, 1)
ppt[0] = make([]image.Point, 4) ppt[0] = make([]image.Point, 4)
ppt[0][0] = image.Point{int(array[0][0]) - xmin, int(array[0][1]) - ymin} ppt[0][0] = image.Point{int(array[0][0]) - xmin, int(array[0][1]) - ymin}
......
...@@ -59,7 +59,6 @@ func resizeByMaxLen(img gocv.Mat, maxLen int) (gocv.Mat, int, int) { ...@@ -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 { func normPermute(img gocv.Mat, mean []float32, std []float32, scaleFactor float32) []float32 {
img.ConvertTo(&img, gocv.MatTypeCV32F) img.ConvertTo(&img, gocv.MatTypeCV32F)
img.DivideFloat(scaleFactor) img.DivideFloat(scaleFactor)
defer img.Close()
c := gocv.Split(img) c := gocv.Split(img)
data := make([]float32, img.Rows()*img.Cols()*img.Channels()) data := make([]float32, img.Rows()*img.Cols()*img.Channels())
...@@ -154,7 +153,6 @@ func crnnPreprocess(img gocv.Mat, resizeShape []int, mean []float32, std []float ...@@ -154,7 +153,6 @@ func crnnPreprocess(img gocv.Mat, resizeShape []int, mean []float32, std []float
img.DivideFloat(scaleFactor) img.DivideFloat(scaleFactor)
img.SubtractScalar(gocv.NewScalar(float64(mean[0]), float64(mean[1]), float64(mean[2]), 0)) 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)) img.DivideScalar(gocv.NewScalar(float64(std[0]), float64(std[1]), float64(std[2]), 0))
defer img.Close()
if resizeW < imgW { if resizeW < imgW {
gocv.CopyMakeBorder(img, &img, 0, 0, 0, imgW-resizeW, gocv.BorderConstant, color.RGBA{0, 0, 0, 0}) gocv.CopyMakeBorder(img, &img, 0, 0, 0, imgW-resizeW, gocv.BorderConstant, color.RGBA{0, 0, 0, 0})
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册