我已经解决了冲突问题,但是我发现多线程并没有缩短时间啊,和我设想的不一样呢?
单线程和双线程、四线程运行的时间都一样,想哭了!
我的目标是5s左右识别一张1920*1080大小的图片,这样我才能用到识别结果,否则光是识别图片就用了20来秒太慢了。
我在学习多线程时仿照例子把单线程改为多线程,却始终无法成功。
在示例中是可以实现多线程结果共享的,但是在实战例子中就不行了。小女子刚学多线程,求指点一下具体怎么改。
以下是例子:
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 ;
}
例子结果:
这是我在修改的实战例子:
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();
}
}
这是实例运行结果:
这个结果不是我想要的,我想要的是BOX识别多线程共享,而不是识别两次。