Пример #1
0
RecognizerErrorStatus recognizerWrapperProcessImage(const RecognizerWrapper* wrapper, const RecognizerImage* image,
                                                    RecognizerErrorStatus (*processRecognizerResult)(const RecognizerResult* result)) {

    /* all API functions return RecognizerErrorStatus indicating the success or failure of operations */
    RecognizerErrorStatus status;

    /* this variable will contain list of scan results obtained from image scanning process. */
    RecognizerResultList* resultList;

    /* this variable will contain number of scan results obtained from image scanning process. */
    size_t numResults;

    /* this is a for loop counter for iteration over result list */
    size_t i;

    /* if you do not want to receive callbacks during simply set NULL as last parameter. If you only want to receive some callbacks,
     insert non-NULL function pointers only to those events you are interested in */
    status = recognizerRecognizeFromImage(wrapper->recognizer, &resultList, image, 0, &wrapper->recognizerCallback);
    if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
        printf("Error recognizing image: %s\n", recognizerErrorToString(status));
        return status;
    }

    /* get number of results */
    status = recognizerResultListGetNumOfResults(resultList, &numResults);
    if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
        printf("Error recognizing getting number of results: %s\n", recognizerErrorToString(status));
        return status;
    }

    /* Iterate over results */
    for (i = 0; i < numResults; ++i) {

        RecognizerResult* result;

        /* obtain i-th result from list */
        status = recognizerResultListGetResultAtIndex(resultList, i, &result);
        if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
            printf("Error recognizing getting %d-th result: %s\n", (int) i, recognizerErrorToString(status));
            return status;
        }

        /* process it */
        processRecognizerResult(result);
    }

    /** Clear result list */
    status = recognizerResultListDelete(&resultList);
    if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
        printf("Error deleting result list: %s\n", recognizerErrorToString(status));
        return status;
    }

    return RECOGNIZER_ERROR_STATUS_SUCCESS;
}
Пример #2
0
int main(int argc, char* argv[]) {
	/* path will contain path to image being recognized */
	const char* path = argv[1];
	/* this buffer will contain OCR model */
	char* ocrModel;
	/* this variable will contain OCR model buffer length in bytes */
	int ocrModelLength;
	/* this variable will contain all recognition settings (which recognizers are enabled, etc.) */
	RecognizerSettings* settings;
	/* this variable will contain MRTD recognition specific settings */
	MRTDSettings mrtdSettings;
	/* this variable will contain device information. On Mac/PC this is not usually necessary, but
	can information about available processor cores. If more than 1 processor is available, recognizers
	will try to use parallel algorithms as much as possible. */
	RecognizerDeviceInfo* deviceInfo;
	/* this variable is the global recognizer that internally contains a list of different recognizers.
	Each recognizer is an object that can perform object recognitions. For example, there are PDF417 barcode
	recognizer (Microblink's implementation for PDF417 barcodes), ZXing barcode recognizer (supports everything ZXing supports),
	Microblink's 1D barcode recognizer, etc. */
	Recognizer* recognizer;
	/* all API functions return RecognizerErrorStatus indicating the success or failure of operations */
	RecognizerErrorStatus status;
	/* recoginzer callback structure contains pointers to functions that will be called during the recognition process */
	RecognizerCallback recognizerCallback;
	/* this variable will contain list of scan results obtained from image scanning process. */
	RecognizerResultList* resultList;
	/* this variable will contain number of scan results obtained from image scanning process. */
	size_t numResults;
	/* this variable holds the image sent for image scanning process*/
	RecognizerImage* image;

	if (argc < 2) {
		printf("usage %s <img_path>\n", argv[0]);
		return -1;
	}

	/* load OCR model from file */
	status = recognizerLoadFileToBuffer("ocr_model.zzip", &ocrModel, &ocrModelLength);
	if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
		printf("Could not load file ocr_model.zzip\n");
		return -1;
	}

	/* create recognizer settings object. Do not forget to delete it after usage. */
	recognizerSettingsCreate(&settings);

	/* create device info object. Do not forget to delete it after usage. */
	recognizerDeviceInfoCreate(&deviceInfo);
	/* define that device has 4 processors (you can use any number here - this is used to define number
	of threads library will use for its parallel operations */
	recognizerDeviceInfoSetNumberOfProcessors(deviceInfo, 4);
	/* add device info object to recognizer settings object */
	recognizerSettingsSetDeviceInfo(settings, deviceInfo);
	/* set OCR model to recognizer settings object */
	recognizerSettingsSetZicerModel(settings, ocrModel, ocrModelLength);
	
	/* add Machine Readable Travel Document recognizer settings to global recognizer settings object */
	recognizerSettingsSetMRTDSettings(settings, &mrtdSettings);

	/* insert license key and licensee */	
	recognizerSettingsSetLicenseKeyForLicensee(settings, "Add licensee here", "Add license key here");
	/* OR insert license key for licensee obtained with LicenseRequestTool (**DO NOT USE BOTH**) */
	recognizerSettingsSetLicenseKey(settings, "Add license key here");

	/* create global recognizer with settings */
	status = recognizerCreate(&recognizer, settings);
	/* as said earlier, all API functions return RecognizerErrorStatus. You can check the status for error, or you can simply ignore it like earlier in this example. */
	if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
		printf("Error creating recognizer: %s\n", recognizerErrorToString(status));
		return -1;
	}

	/* build recognizer callback structure */
	recognizerCallback = buildRecognizerCallback();

	/* create the recognizer image object from video capture frame so we can send it to recognizer */
	status = recognizerImageCreateFromFile(&image, path);
	if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
		printf("Error creating image from frame: %s\n", recognizerErrorToString(status));
		return -1;
	}

	/* if you do not want to receive callbacks during simply set NULL as last parameter. If you only want to receive some callbacks,
	insert non-NULL function pointers only to those events you are interested in */
	status = recognizerRecognizeFromImage(recognizer, &resultList, image, 0, NULL);
	if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
		printf("Error recognizing file %s: %s\n", path, recognizerErrorToString(status));
		return -1;
	}

	recognizerResultListGetNumOfResults(resultList, &numResults);

	if (numResults != 1u) {
		/* number of results should be 1 as there is only one recognizer configured */
		printf("Wrong number of recognizer results:" JL_SIZE_T_SPECIFIER "\n", numResults);
		return -1;
	}

	RecognizerResult* result;
	/* obtain the first (and only) result from list */
	recognizerResultListGetResultAtIndex(resultList, 0u, &result);


	int isMrtd = 0;
	/* check if it is a MRTD result */
	status = recognizerResultIsMRTDResult(result, &isMrtd);
	if (status == RECOGNIZER_ERROR_STATUS_SUCCESS && isMrtd) {
		int valid = 0;
		/* check if MRTD result is valid */
		status = recognizerResultIsResultValid(result, &valid);
		if (status == RECOGNIZER_ERROR_STATUS_SUCCESS && valid) {
			const char* doe;
			const char* issuer;
			const char* docNum;
			const char* docCode;
			const char* dob;
			const char* primID;
			const char* secID;
			const char* sex;
			const char* nat;
			const char* opt1;
			const char* opt2;
			/* obtain all fields from result */
			recognizerResultGetMRTDDateOfExpiry(result, &doe);
			recognizerResultGetMRTDIssuer(result, &issuer);
			recognizerResultGetMRTDDocumentNumber(result, &docNum);
			recognizerResultGetMRTDDocumentCode(result, &docCode);
			recognizerResultGetMRTDDateOfBirth(result, &dob);
			recognizerResultGetMRTDPrimaryID(result, &primID);
			recognizerResultGetMRTDSecondaryID(result, &secID);
			recognizerResultGetMRTDSex(result, &sex);
			recognizerResultGetMRTDNationality(result, &nat);
			recognizerResultGetMRTDOpt1(result, &opt1);
			recognizerResultGetMRTDOpt2(result, &opt2);
			/* display obtained fields */
			printf("ID is of type %s issued by %s.\nExpiration date is %s.\n", docCode, issuer, doe);
			printf("ID number is %s.\n", docNum);
			printf("ID holder is %s %s.\nDate of birth is %s.\nSex is %s.\n", primID, secID, dob, sex);
			printf("Nationality is %s.\n", nat);
			printf("Optional fields are:\nOPT1: %s\nOPT2: %s\n", opt1, opt2);

			const char* raw;
			/* obtain raw lines from result */
			recognizerResultGetMRTDRawStringData(result, &raw);
			printf("Raw result lines:\n%s\n", raw);
		}
		else {
			printf("Invalid result!\n");
		}
	}
	else {
		/* this should never happen as there is only MRTD recognizer configured */
		printf("Invalid result type!\n");
	}

	/* cleanup memory */	
	recognizerImageDelete(&image);
	recognizerResultListDelete(&resultList);
	recognizerDeviceInfoDelete(&deviceInfo);
	recognizerSettingsDelete(&settings);
	recognizerDelete(&recognizer);
	recognizerFreeFileBuffer(&ocrModel);

	return 0;
}