/*! * recogaReadStream() * * Input: stream * Return: recog, or null on error */ L_RECOGA * recogaReadStream(FILE *fp) { l_int32 version, i, nrec, ignore; L_RECOG *recog; L_RECOGA *recoga; PROCNAME("recogaReadStream"); if (!fp) return (L_RECOGA *)ERROR_PTR("stream not defined", procName, NULL); if (fscanf(fp, "\nRecog Version %d\n", &version) != 1) return (L_RECOGA *)ERROR_PTR("not a recog file", procName, NULL); if (version != RECOG_VERSION_NUMBER) return (L_RECOGA *)ERROR_PTR("invalid recog version", procName, NULL); if (fscanf(fp, "Number of recognizers = %d\n\n", &nrec) != 1) return (L_RECOGA *)ERROR_PTR("nrec not read", procName, NULL); recoga = recogaCreate(nrec); for (i = 0; i < nrec; i++) { ignore = fscanf(fp, "==============================\n"); if (fscanf(fp, "Recognizer %d\n", &ignore) != 1) return (L_RECOGA *)ERROR_PTR("malformed file", procName, NULL); if ((recog = recogReadStream(fp)) == NULL) { recogaDestroy(&recoga); L_ERROR("recog read failed for recog %d\n", procName, i); return NULL; } ignore = fscanf(fp, "\n"); recogaAddRecog(recoga, recog); } return recoga; }
/*! * \brief recogReadMem() * * \param[in] data serialization of recog (not ascii) * \param[in] size of data in bytes * \return recog, or NULL on error */ L_RECOG * recogReadMem(const l_uint8 *data, size_t size) { FILE *fp; L_RECOG *recog; PROCNAME("recogReadMem"); if (!data) return (L_RECOG *)ERROR_PTR("data not defined", procName, NULL); if ((fp = fopenReadFromMemory(data, size)) == NULL) return (L_RECOG *)ERROR_PTR("stream not opened", procName, NULL); recog = recogReadStream(fp); fclose(fp); if (!recog) L_ERROR("recog not read\n", procName); return recog; }
/*! * \brief recogRead() * * \param[in] filename * \return recog, or NULL on error * * <pre> * Notes: * (1) When a recog is serialized, a pixaa of the templates that are * actually used for correlation is saved in the pixaa_u array * of the recog. These can be different from the templates that * were used to generate the recog, because those original templates * can be scaled and turned into normalized lines. When recog1 * is deserialized to recog2, these templates are put in both the * unscaled array (pixaa_u) and the modified array (pixaa) in recog2. * Why not put it in only the unscaled array and let * recogTrainingFinalized() regenerate the modified templates? * The reason is that with normalized lines, the operation of * thinning to a skeleton and dilating back to a fixed width * is not idempotent. Thinning to a skeleton saves pixels at * the end of a line segment, and thickening the skeleton puts * additional pixels at the end of the lines. This tends to * close gaps. * </pre> */ L_RECOG * recogRead(const char *filename) { FILE *fp; L_RECOG *recog; PROCNAME("recogRead"); if (!filename) return (L_RECOG *)ERROR_PTR("filename not defined", procName, NULL); if ((fp = fopenReadStream(filename)) == NULL) return (L_RECOG *)ERROR_PTR("stream not opened", procName, NULL); if ((recog = recogReadStream(fp)) == NULL) { fclose(fp); return (L_RECOG *)ERROR_PTR("recog not read", procName, NULL); } fclose(fp); return recog; }