bool BagOfWordsExtractor::dictionarize()
{
    //adjust the shape of the sample matrix in case it was not completely filled.
    if(current_row<max_bow_features)
        return false;


    fprintf(stdout,"dictionarizing!\n");

    cv::Mat cluster_labels(n_centers,1,CV_32S);
    cv::kmeans(samples,n_centers,cluster_labels,cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1),n_attempts,cv::KMEANS_RANDOM_CENTERS,&centers);

    fprintf(stdout,"dictionarized!\n");

    //save the dictionary
    fstream f_dic;
    f_dic.open((context_path+"/"+name+"-"+time_tag+".dict").c_str(),fstream::out | ios_base::binary);
    float *tmp_centers=new float[centers.rows*centers.cols];
    for(int i=0; i<centers.rows; i++)
        for(int j=0; j<centers.cols; j++)
            tmp_centers[i*centers.cols+j]=centers.at<float>(i,j);
    f_dic.write((char*)tmp_centers,sizeof(float)*centers.rows*centers.cols);
    f_dic.close();

    fstream f_dic_ini;
    f_dic_ini.open((context_path+"/"+name+"-"+time_tag+".ini").c_str(),fstream::out);
    f_dic_ini<<"dictionary_type"<<TABS<<type<<endl;
    f_dic_ini<<"n_words"<<TABS<<n_centers<<endl;
    f_dic_ini<<"word_dimension"<<TABS<<bow_feature_size<<endl;
    f_dic_ini<<"dictionary"<<TABS<<name+"-"+time_tag<<endl;
    f_dic_ini.close();

    update_ini();

    dictionarized=true;

    return true;
}
Exemple #2
0
int
do_install(void)
{
int rc=0;
char buf[MAXSTR];
char gsviewdir[MAXSTR];
char gstoolsdir[MAXSTR];
    install_init();

    gs_addmess("Destination ");
    gs_addmess(destdir);
    gs_addmess("\n");
    gs_addmess("Installing GSview: ");
    if (install_gsview)
        gs_addmess("true\n");
    else
        gs_addmess("false\n");
    gs_addmess("Installing Ghostscript: ");
    if (install_gs)
        gs_addmess("true\n");
    else
        gs_addmess("false\n");

    if (!rc) {
	char logname[MAXSTR];
	char *p;

	strcpy(gsviewdir, destdir);
	if (strlen(gsviewdir) == 2)
	    strcat(gsviewdir, "\\");	/* is root directory */
	strcpy(gstoolsdir, gsviewdir);
	if (strlen(gsviewdir) && (gsviewdir[strlen(gsviewdir)-1] != '\\'))
	    strcat(gsviewdir, "\\");
	strcat(gsviewdir, gsviewbase);
	mkdirall(gsviewdir);

	strcpy(logname, gsviewdir);
	strcat(logname, "\\");
	strcat(logname, GSVIEW_ZIP);
	p = strrchr(logname, '.');
	strcpy(p, ".log");
	unziplogfile = fopen(logname, "w");	/* don't append */
	if (unziplogfile == (FILE *)NULL) {
	    load_string(IDS_CANTOPENWRITE, buf, sizeof(buf)); 
	    sprintf(error_message, buf, logname);
	    rc = 1;
	}
    }

    if (install_gs || install_gsview) {
	if (!rc) {
	    /* copy unzip DLL so we don't demand load it from floppy */
	    strcpy(unzipname, gsviewdir);
	    strcat(unzipname, "\\");
	    strcat(unzipname, szUnzipDll);
	    strcpy(buf, sourcedir);
	    strcat(buf, szUnzipDll);
	    rc = copyfile(unzipname, buf);
	}

	if (cancelling)
	    rc = 1;

	/* unzip GSview and Ghostscript */
	if (!rc) {
	    char zipname[MAXSTR];
	    load_unzip(unzipname, phInstance, 
		find_page_from_id(IDD_TEXTWIN)->hwnd, (HWND)NULL);

	    if (!rc && install_gsview) {
		rc = unzip_to_dir(GSVIEW_ZIP, gsviewdir);
		if (cancelling)
		    rc = 1;
	    }
#ifdef OS2
	    if (!rc) {
		/* install EMX if needed */
	        char emxbuf[MAXSTR];
		sprintf(emxbuf, "%s\\", bootdrive);
		if (!emx)
		    rc = unzip_to_dir(EMXZIP, emxbuf);
		if (cancelling)
		    rc = 1;
	    }
#endif

	    if (!rc && install_gs) {
		if (gsver < 600) {
		    sprintf(zipname, "%sini.zip", gs_zipprefix);
		    if (!rc)
			rc = unzip_to_dir(zipname, gstoolsdir);
		    if (cancelling)
			rc = 1;
		}
#ifdef OS2
		sprintf(zipname, "%sos2.zip", gs_zipprefix);
#else
		sprintf(zipname, "%sw32.zip", gs_zipprefix);
#endif
		if (!rc)
		    rc = unzip_to_dir(zipname, gstoolsdir);
		if (cancelling)
		    rc = 1;
		if (gsver < 600) {
		    sprintf(zipname, "%sfn1.zip", gs_zipprefix);
		    if (!rc) {
			strcpy(buf, gstoolsdir);
			if (strlen(buf) && (buf[strlen(buf)-1] != '\\'))
			    strcat(buf, "\\");
			strcat(buf, gs_basedir);
			rc = unzip_to_dir(zipname, buf);
			if (cancelling)
			    rc = 1;
		    }
		}
	    }

	    gs_chdir(workdir);
	    free_unzip();
	}

	/* remove unneeded unzip DLL */
	unlink(unzipname);
    }

    if (!rc && install_autoexec)
	rc = update_config();
    if (cancelling)
	rc = 1;

    if (!rc && install_group)
	rc = create_object();
    if (cancelling)
	rc = 1;

    if (!rc) {
#ifdef OS2
	char szIniName[MAXSTR];
	strcpy(szIniName, bootdrive);
	strcat(szIniName, "\\os2\\gvpm.ini");
#endif
	/* This doesn't work for Windows per user profiles */
	rc = update_ini(szIniName);
    }
    if (cancelling)
	rc = 1;

    if (unziplogfile!=(FILE *)NULL)
	fclose(unziplogfile);

    return rc;
}