Skip to content

Commit

Permalink
Release MAT object after performing OCR
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesChenX committed Jun 10, 2024
1 parent abd1149 commit aabe36f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,34 @@

package ai.djl.opencv;

import ai.djl.modality.cv.Image;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;

/**
* @author James Chen
*/
public final class OpenCVImageUtil {
public class ExtendedOpenCVImage extends OpenCVImage implements AutoCloseable {

private OpenCVImageUtil() {
public ExtendedOpenCVImage(Mat image) {
super(image);
}

public static Image create(String imagePath) {
Mat mat = Imgcodecs.imread(imagePath);
return new OpenCVImage(mat);
public ExtendedOpenCVImage(String imagePath) {
super(read(imagePath));
}

public static Image create(Mat image) {
return new OpenCVImage(image);
private static Mat read(String imagePath) {
Mat mat = Imgcodecs.imread(imagePath);
if (mat.empty()) {
throw new RuntimeException(
"Failed to read from the path: "
+ imagePath);
}
return mat;
}

@Override
public void close() {
getWrappedImage().release();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import ai.djl.modality.cv.Image;
import ai.djl.modality.cv.output.BoundingBox;
import ai.djl.modality.cv.output.DetectedObjects;
import ai.djl.opencv.OpenCVImageUtil;
import ai.djl.opencv.ExtendedOpenCVImage;
import ai.djl.paddlepaddle.engine.PpEngine;
import ai.djl.repository.zoo.Criteria;
import ai.djl.repository.zoo.ZooModel;
Expand All @@ -37,8 +37,6 @@
import io.netty.util.concurrent.FastThreadLocal;
import nu.pattern.OpenCV;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;

import im.turms.server.common.infra.lang.StringUtil;
import im.turms.server.common.infra.logging.core.logger.Logger;
Expand Down Expand Up @@ -165,19 +163,14 @@ public void close() {
}

public DetectedObjects ocr(String imagePath) {
Mat img = Imgcodecs.imread(imagePath);
if (img.empty()) {
throw new RuntimeException(
"Failed to read from the path: "
+ imagePath);
try (ExtendedOpenCVImage image = new ExtendedOpenCVImage(imagePath)) {
return ocr(image);
}
Image image = OpenCVImageUtil.create(img);
return ocr(image);
}

public DetectedObjects ocr(Image image) {
DetectedObjects detect = detect(image);
List<DetectedObjects.DetectedObject> detectedObjects = detect.items();
private DetectedObjects ocr(Image image) {
DetectedObjects detectResult = detect(image);
List<DetectedObjects.DetectedObject> detectedObjects = detectResult.items();
int size = detectedObjects.size();
List<String> names = new ArrayList<>(size);
List<Double> probabilities = new ArrayList<>(size);
Expand Down

0 comments on commit aabe36f

Please sign in to comment.