使用Android视觉文本OCR构建名片识别器

4
我正在使用谷歌的Android Mobile Vision OCR Text构建一个安卓应用程序,以便将名片作为联系人输入到手机中。
到目前为止,我已经能够识别任何拉丁文生成的文本,并且已经能够在代码块上应用正则表达式。
我的做法是创建了一个包含五个变量(姓名、电子邮件、公司名称、网站、地址和电话号码)的联系人bean类。在对生成的实时数据应用正则表达式后,我过滤结果并将它们保存在一个bean类对象中,然后将该对象传递给活动,并提取存储在该对象中的数据并在我的文本视图中显示它。
OCR图形类检测方法>>>
List<? extends Text> textComponents = text.getComponents();
        for(final  Text currentText : textComponents) {
            float left = translateX(currentText.getBoundingBox().left);
            float bottom = translateY(currentText.getBoundingBox().bottom);
            canvas.drawText(currentText.getValue(), left, bottom, sTextPaint);
            if (currentText != null && currentText.getValue() != null) {
                //stringList.add(currentText.getValue());

                Log.e("OCrGraphic", "Text detected! " + currentText.getValue());

                if (isCompany== false && currentText.getValue().matches(".[A-Z].[^@$#/-<>!]+")) {
                    Log.e("currentTextcompanyName", currentText.getValue());
                    companyName = "";
                    companyName = currentText.getValue();
                    isCompany = true;
                    contactsBeans.setCompanyName(companyName);
                }

                if (isEmail == false && currentText.getValue().matches("^[_A-Za-z0-9-\\\\+]+(\\\\.[_A-Za-z0-9-]+)*@\"\n" +
                        "\t\t+ \"[A-Za-z0-9-]+(\\\\.[A-Za-z0-9]+)*(\\\\.[A-Za-z]{2,})$") || currentText.getValue().contains("@")) {
                    Log.e("currentTextemail", currentText.getValue());
                    email = "";
                    email = currentText.getValue();
                    isEmail = true;
                    contactsBeans.setEmail(email);

                }
               // Patterns.WEB_URL.matcher(currentText.getValue()).matches();
                if (isWebsite == false && currentText.getValue().matches("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]") || currentText.getValue().startsWith("www") || currentText.getValue().contains("Website") || currentText.getValue().contains("www")) {
                    Log.e("currentTextWebsite", currentText.getValue());
                    website = "";
                    website = currentText.getValue();
                    isWebsite = true;
                    contactsBeans.setWebsite(website);

                }
                if (isName== false && currentText.getValue().matches("[a-zA-z]+([ '-][a-zA-Z]+)*")) {
                    Log.e("name", currentText.getValue());
                    name = "";
                    name = currentText.getValue();
                    isName = true;
                    contactsBeans.setName(name);
                }

                if (isPhone == false && !currentText.getValue().contains("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") && currentText.getValue().startsWith("+") || currentText.getValue().startsWith("0") && currentText.getValue().contains("+-0123456789/-#") ) {
                    Log.e("currentTextphone", currentText.getValue());
                    phone = "";
                    phone = currentText.getValue();
                    isPhone = true;
                    contactsBeans.setPhone(phone);
                }

                if (isAdrs == false &&currentText.getValue().matches("[a-zA-z]+([ '-][a-zA-Z]+)*") && currentText.getValue().contains("Address") || currentText.getValue().contains("Office") || currentText.getValue().contains("Floor") || currentText.getValue().contains("Plaza") || currentText.getValue().contains("office") || currentText.getValue().contains("Floor")|| currentText.getValue().contains("Floors")|| currentText.getValue().contains("floors")|| currentText.getValue().contains("floor")|| currentText.getValue().contains("Street")|| currentText.getValue().contains("Road")) {
                    address = "";
                    address = currentText.getValue();
                    isAdrs = true;
                    contactsBeans.setAddress(address);
                    Log.e("currentTextaddress", currentText.getValue());
                }

                timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        context = ApplicationController.getContext();
                        Intent intent = new Intent(context,ContactsEditActivity.class);
                 /*       Log.e("CBname",contactsBeans.getName());
                        Log.e("CBemail",contactsBeans.getEmail());
                        Log.e("CBadrs",contactsBeans.getAddress());
                        Log.e("CBwebsite",contactsBeans.getWebsite());
                        Log.e("CBcomp",contactsBeans.getCompanyName());
                        Log.e("CBphone",contactsBeans.getPhone());*/
                        intent.putExtra("contactsList",contactsBeans);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
                        // intent.putStringArrayListExtra("contactsList",stringList);
                        context.startActivity(intent);
                    }
                },6000,6000);

             /*



           */
} 

联系人 Bean 可序列化类

public class ContactsBeans implements Parcelable {
    String name;
    String phone;String email;String companyName;
    String address; String website;
    public List<ContactsBeans> selectedContactsAttribute;

    public ContactsBeans() {
    }

    public ContactsBeans(List<ContactsBeans> selectedContactsAttribute) {
        this.selectedContactsAttribute = selectedContactsAttribute;
    }

    public ContactsBeans(String name, String phone, String email, String companyName, String address, String website) {

        this.name = name;
        this.phone = phone;
        this.email = email;
        this.companyName = companyName;
        this.address = address;
        this.website = website;
    }

    protected ContactsBeans(Parcel in) {
        name = in.readString();
        phone = in.readString();
        email = in.readString();
        companyName = in.readString();
        address = in.readString();
        website = in.readString();
        selectedContactsAttribute = in.createTypedArrayList(ContactsBeans.CREATOR);
    }

    public static final Creator<ContactsBeans> CREATOR = new Creator<ContactsBeans>() {
        @Override
        public ContactsBeans createFromParcel(Parcel in) {
            return new ContactsBeans(in);
        }

        @Override
        public ContactsBeans[] newArray(int size) {
            return new ContactsBeans[size];
        }
    };

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public List<ContactsBeans> getSelectedContactsAttribute() {
        return selectedContactsAttribute;
    }

    public void setSelectedContactsAttribute(List<ContactsBeans> selectedContactsAttribute) {
        this.selectedContactsAttribute = selectedContactsAttribute;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(phone);
        dest.writeString(email);
        dest.writeString(companyName);
        dest.writeString(address);
        dest.writeString(website);
        dest.writeTypedList(selectedContactsAttribute);
    }
}

https://developers.google.com/android/reference/com/google/android/gms/vision/text/Text

https://codelabs.developers.google.com/codelabs/mobile-vision-ocr/#6

我已经按照上述教程进行了操作,但还有以下问题:

a-) 如何使用文本行而不是文本块?

b-) 我在Graphic类中使用了Timer Task,当它完成后如何终止它,或者应该使用其他方法?

c-) 是否有任何应用程序可以使用视觉OCR进行名片输入?虽然他们说可以,但我没有找到一个。

d-) 我的正则表达式在Java的单独IDE中测试过是正确的,有什么建议吗?

e-) 我正在使用意图额外功能来获取存储在联系人bean对象中的数据并在活动中显示它,但它一直像雪球一样滚动,从未停止,尽管我在我的IF语句中加入了标志。

f-) 在所有标志都变为真之后,我们是否可以在某个时候停止OCR库检测任何进一步的文本,或者只有任何其他方法?

g-) 它一直覆盖我的变量,无论条件是否为真?

非常感谢您提供的所有帮助。

2个回答

0
我可以帮忙解决其中的一些问题。

a-) 如何使用文本行而不是文本块?

List<Line> lines = (List<Line>) textBlock.getComponents();

您可能需要迭代TextBlock SparseArray以获取每个块的行。此外,这种方法也适用于从每行获取每个元素。getComponents()方法在所有文本项实现的Text接口中。

b-) 我在Graphic类中使用Timer Task,完成后如何终止它?或者我应该使用其他方法?

您可以在OcrDetectorProcessor中计算接收到的检测数量,并在达到一定数量时终止它。

f-) 在所有标志都变为true后,我们可以在某个时刻停止OCR库继续检测任何进一步的文本吗?或者有其他方法吗?

您可以通过停止CameraSource来停止管道的检测。在OcrCaptureActivity的CodeLabs示例中,在onPause和onDestroy中执行此操作。通过停止和释放mPreview,应用程序停止并清除与相机的连接。

希望这可以帮助您。


你对问题a-)的回答很接近,我正在处理。对于b-),问题是如何在计数后终止它。f-) 我不得不使用可运行线程,并使用线程池执行器将其终止,因为必须在所有文本都被读取后才能完成。@amanuel 谢谢伙计。 - Nofal Bin Idrees

0

对于a)点,你也可以使用:

 List<Line> lines = (List<Line>) text.getComponents();
        for(Line elements : lines){
            Log.i("current lines ", ": " + elements.getValue());
        }

嗨Meraj,我使用了这个链接https://dev59.com/0VoT5IYBdhLWcg3wtBRz作为参考。我只需要打印图像中的所有行,但它没有扫描所有行。请尽快帮助我。先谢谢了。 - Naveen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接