蓗乐 2022-03-01 10:22 采纳率: 66.7%
浏览 445
已结题

java 实战 多线程无法实现的问题

我已经解决了冲突问题,但是我发现多线程并没有缩短时间啊,和我设想的不一样呢?
单线程和双线程、四线程运行的时间都一样,想哭了!
我的目标是5s左右识别一张1920*1080大小的图片,这样我才能用到识别结果,否则光是识别图片就用了20来秒太慢了。

img

img

我在学习多线程时仿照例子把单线程改为多线程,却始终无法成功。
在示例中是可以实现多线程结果共享的,但是在实战例子中就不行了。小女子刚学多线程,求指点一下具体怎么改。
以下是例子:


class  hello implements  Runnable {
    public  void  run() {
        for  ( int  i = 0 ; i < 7 ; i++) {
            if  (count > 0 ) {
                System.out.println( Thread.currentThread().getName() + "count= "  + count--);
            }
        }
    }

    public  static  void  main(String[] args) {
        hello he= new  hello();
        new  Thread(he, "A").start();
        new  Thread(he, "B").start();

    }

    private  int  count = 5 ;
}

例子结果:

img

这是我在修改的实战例子:


public class imageUrl implements  Runnable {

    public void run(){
        BytePointer outtext;

        TessBaseAPI api = new TessBaseAPI();

        api.Init("tessdata", "chi_sim") ;//chi_sim在tessdata中位置

        File file = new File("tupian/3.png");//图片地址

        PIX image = pixRead(file.getAbsolutePath());

        api.SetImage(image);

        int[] blockIds = {};

        long starttime = System.currentTimeMillis();

        BOXA boxes = api.GetComponentImages(RIL_WORD, true, null, blockIds);


        for (int i = 0; i < boxes.n(); i++) {

            BOX box = boxes.box(i);

            api.SetRectangle(box.x(), box.y(), box.w(), box.h());

            outtext = api.GetUTF8Text();

            String ocrresult = outtext.getString();
            //去空格
            ocrresult = ocrresult.replace(" ","");

            int conf = api.MeanTextConf();

            String boxinformation = String.format("Box[%d]:x=%d,y=%d,w=%d,h=%d,confidence:%d,text:%s", i, box.x(),
                    box.y(), box.w(), box.h(), conf, ocrresult);
            System.out.println(boxinformation);

            outtext.deallocate();
        }
        api.End();
        pixDestroy(image);
        long invertaltime = System.currentTimeMillis() - starttime;
        System.out.println("识别用时:" + invertaltime);
    }


    public static void main(String[] args) {

        imageUrl ds1 = new imageUrl();

        imageUrl ds2 = new imageUrl();

        new  Thread(ds1,"A").start();

        new  Thread(ds2,"B").start();

    }

}

这是实例运行结果:

img

这个结果不是我想要的,我想要的是BOX识别多线程共享,而不是识别两次。

  • 写回答

11条回答 默认 最新

  • wyz0923 2022-03-01 19:45
    关注
    获得1.80元问题酬金

    根据你的代码进行修改的

    import org.bytedeco.javacpp.BytePointer;
    import org.bytedeco.leptonica.BOX;
    import org.bytedeco.leptonica.BOXA;
    import org.bytedeco.leptonica.PIX;
    import org.bytedeco.tesseract.TessBaseAPI;
    
    import java.io.File;
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.atomic.AtomicBoolean;
    import java.util.concurrent.atomic.AtomicInteger;
    
    import static org.bytedeco.leptonica.global.lept.pixDestroy;
    import static org.bytedeco.leptonica.global.lept.pixRead;
    import static org.bytedeco.tesseract.global.tesseract.RIL_WORD;
    
    public class imageUrl implements  Runnable {
        private AtomicInteger index = new AtomicInteger(0);
        private  BOXA boxes;
        private  TessBaseAPI api;
        private CountDownLatch latch;
    
        public  AtomicBoolean flag = new AtomicBoolean(true);
        public imageUrl(){}
        public imageUrl(CountDownLatch latch){
            this.latch = latch;
            init();
        }
    
        /**
         *  初始化处理
         */
        public void init(){
            api = new TessBaseAPI();
            //chi_sim在tessdata中位置
            api.Init("tessdata", "chi_sim") ;
            //图片地址
            File file = new File("tupian/3.png");
            PIX image = pixRead(file.getAbsolutePath());
            api.SetImage(image);
            int[] blockIds = {};
            boxes = api.GetComponentImages(RIL_WORD, true, null, blockIds);
        }
    
        /**
         *  执行任务
         */
        @Override
        public void run(){
            long startTime = System.currentTimeMillis() ;
            int num = 0;
            //cas 原子操作
            while ( (num =index.getAndAdd(1)) < boxes.n()) {
                BOX box = boxes.box(index.get());
                if (box == null){
                    continue;
                }
                int conf;
                String ocrresult;
                synchronized(imageUrl.class){
                    api.SetRectangle(box.x(), box.y(), box.w(), box.h());
                    BytePointer outtext = api.GetUTF8Text();
                    conf = api.MeanTextConf();
                    ocrresult = outtext.getString();
                    outtext.deallocate();
                }
                
                //去空格
                ocrresult = ocrresult.replace(" ","");
                String boxinformation = String.format("Box[%d]:x=%d,y=%d,w=%d,h=%d,confidence:%d,text:%s", num, box.x(),
                        box.y(), box.w(), box.h(), conf, ocrresult);
                System.out.println(boxinformation);
    
            }
    
            //结束 防止重复结束
            if (flag.getAndSet(false)){
                end();
            }
            long totalTime = System.currentTimeMillis() - startTime;
            System.out.println("====》识别用时:" + totalTime);
            latch.countDown();
        }
    
        /**
         *  结束处理
         */
        public void end(){
            api.End();
            PIX image = new PIX();
            pixDestroy(image);
            System.out.println("结束释放资源");
        }
    
    
    }
    
    class Test{
        public static void main(String[] args) throws InterruptedException {
           CountDownLatch latch = new CountDownLatch(2);
            imageUrl ds = new imageUrl(latch);
            new  Thread(ds,"A").start();
            new  Thread(ds,"B").start();
            latch.await();
        }
    }
    
    评论 编辑记录
    1人已打赏

报告相同问题?

问题事件

  • 系统已结题 3月9日
  • 修改了问题 3月2日
  • 创建了问题 3月1日

悬赏问题

  • ¥15 寻找了解qq家园纵横四海的程序猿。
  • ¥15 optisystem
  • ¥15 VB.NET画图时的撤销编程
  • ¥15 C# webservice 参数特殊字符处理
  • ¥15 关于控制时间更新的一个操作
  • ¥15 JAVA中使用poi创建word文件,实现页面边框环绕页眉
  • ¥15 matlibplot绘图图形切线问题
  • ¥15 flink 深度优先搜索
  • ¥15 python for everybody 使用Beautifulsoup构建soup对象的时候疯狂报错
  • ¥15 mac playcover上原神在登录后的加载页面闪退,重新下载原神IPA和playcover均无效