Exemple #1
0
    def __init__(self, Editor):
        """
            The Constructor. Loads the glade object, and initializes the GladeWindow object for signal connecting.
        """

        self.m_oEditor = Editor
        self.m_sDataDir = os.environ['HARPIA_DATA_DIR']

        ## Imports the Glade file
        filename = self.m_sDataDir + 'glade/preferences.ui'

        # The widget list
        widget_list = [
            'preferences', 'PREFGridInt', 'PREFShowGrid', 'PREFServer',
            'PREFPort'
        ]

        # Signal Handlers from the Buttons Confirm and Cancel
        handlers = [
            'on_preferences_confirm_clicked', 'on_preferences_cancel_clicked'
        ]

        # The Top window widget
        top_window = 'preferences'

        # Starts the GladeWindow, calling his __init__ method.
        GladeWindow.__init__(self, filename, top_window, widget_list, handlers)

        # Set the preferences Icon
        self.widgets['preferences'].set_icon_from_file(self.m_sDataDir +
                                                       "images/harpia_ave.png")

        ## The Homefolder where the preferences will be stored.
        if os.name == "nt":
            self.HomeFolder = os.path.join(os.path.expanduser("~"),
                                           "harpiaFiles\\")
        else:
            self.HomeFolder = os.path.expanduser("~/harpiaFiles/")
        # self.HomeFolder = "/tmp/.harpia/"

        ## The Config File Path
        self.configfile = self.HomeFolder + "harpia.conf"

        # If the path do not exist, creates one.
        if not (os.path.exists(self.HomeFolder)):
            os.makedirs(self.HomeFolder, mode=0700)

        # If the path do not exist, creates one copyint the default harpia.conf.
        if not (os.path.exists(self.configfile)):
            shutil.copy("harpia.conf", self.HomeFolder)

        print "TESTE" + self.configfile
        ## A binderytool object, with the preferences stored
        self.m_oPreferencesXML = XMLParser(self.configfile)

        # ---------------------------------
        self.preferences = self.m_oPropertiesXML.getTag("harpia").getTag(
            "editor").getChildTags("property")

        # Load Preferences
        for Preference in self.preferences:

            if Preference.name == "show-grid":
                if Preference.value == "false":
                    state = "False"
                else:
                    state = "True"
                self.widgets['PREFShowGrid'].set_active(eval(state))

            if Preference.name == "grid-int":
                self.widgets['PREFGridInt'].set_value(int(Preference.value))

            if Preference.name == "server":
                self.widgets['PREFServer'].set_text(unicode(Preference.value))

            if Preference.name == "port":
                self.widgets['PREFPort'].set_value(int(Preference.value))
Exemple #2
0
    def on_ProcessToolBar_clickedIneer(self):
        t_nPage = self.widgets['WorkArea'].get_current_page()
        t_bIsLive = False
        if self.m_oGcDiagrams.has_key(t_nPage):
            self.UpdateStatus(0)

            t_oGcDiagram = self.m_oGcDiagrams[t_nPage]
            #print "PROCESS CHAIN",t_oS2iDiagram.GetProcessChain()
            #t_oProcessXML = bt.bind_string("<harpia>" + \
            #                               str(t_oGcDiagram.GetProcessChain()) + \
            #                               "</harpia>")
            #print len(list(t_oProcessXML.harpia.properties.childNodes))


            t_oProcessXML = XMLParser("<harpia>" + \
                                           str(t_oGcDiagram.GetProcessChain()) + \
                                           "</harpia>", fromString=True)
            #print t_oProcessXML

            graph_size = len(list(t_oProcessXML.getTag("harpia").getTag("properties").getTagChildren()))

            if graph_size > 1:
                blocks = t_oProcessXML.getTag("harpia").getTag("properties").getChildTags("block")
                for t_oBlockProperties in blocks:
                    block_properties = t_oBlockProperties.getChildTags("property")
                    if int(t_oBlockProperties.type) == 00:  # 00 = acquisition block
                        inputType = 'file'
                        for t_oProperty in block_properties:
                            if t_oProperty.name == 'type':
                                print t_oProperty.name
                                inputType = t_oProperty.value

                            ###Just in case we need to know if we are dealing with a live feed or not this early
                            # if inputType == 'video' or inputType == 'live':
                            #	#if not t_bIsLive:
                            #		#t_bIsLive = True

                            # adoção do paradigma monolítico.. nada de ficar mandando imagens por sockets!!
                            if t_oProperty.name == 'filename' and inputType == 'file':
                                t_oProperty.value = os.path.expanduser(t_oProperty.value)
                                t_oProperty.value = os.path.realpath(t_oProperty.value)
                                if (not os.path.exists(t_oProperty.value)):
                                    errMsg = _("Bad Filename: ") + t_oProperty.value
                                    print(errMsg)
                                    self.SetStatusMessage(errMsg, 0)
                                    return

                    if int(t_oBlockProperties.type) == 01:  # 01 => save image
                        for t_oProperty in block_properties:
                            if t_oProperty.name == 'filename':
                                t_oProperty.value = os.path.realpath(t_oProperty.value)


                    # seguindo o paradigma de não mandar mais nada.. vamos testar com o haar =]
                    # não vamos mandar mais nada mas vamos traduzir o path do haarCascade pra algo real
                    if int(t_oBlockProperties.type) == 610:  # 610 => haar detector... passando a cascade .xml
                        for t_oProperty in block_properties:
                            if t_oProperty.name == 'cascade_name':
                                t_oProperty.value = os.path.realpath(t_oProperty.value)
                                if (not os.path.exists(t_oProperty.value)):
                                    errMsg = _("Bad Filename: ") + t_oProperty.value
                                    print(errMsg)
                                    self.SetStatusMessage(errMsg, 0)
                                    return

                # cpscotti standalone!!!
                t_lsProcessChain = []  # lista pra n precisar ficar copiando prum lado e pro otro o xml inteiro
                t_lsProcessChain.append(t_oProcessXML.getXML())

                t_Sm = s2iSessionManager.s2iSessionManager()

                ## pegando o novo ID (criado pela s2iSessionManager) e passando para o s2idiagram
                self.m_oGcDiagrams[t_nPage].SetIDBackendSession(t_Sm.m_sSessionId)

                # step sempre sera uma lista.. primeiro elemento eh uma mensagem, segundo eh o erro.. caso exista erro.. passar para o s2idiagram tb!
                self.m_oGcDiagrams[t_nPage].set_error_log('')
                t_bEverythingOk = True
                for step in t_Sm.NewInstance(t_lsProcessChain):
                    if len(step) > 1:
                        if step[1] != '' and step[1] != None:
                            self.m_oGcDiagrams[t_nPage].append_error_log(step[1])
                            t_bEverythingOk = False
                    self.SetStatusMessage(step[0], t_bEverythingOk)
                    # self.widgets['StatusLabel'].set_text()
                    # print t_bEverythingOk
                    print step[0]
                # yield step#util caso se resolva usar a interface "lenta" ou se descubra como atualizar rapidamente a GUI

        # falta pegar o retorno!!!!!!
        self.UpdateStatus(7)
Exemple #3
0
    def __init__(self, PropertiesXML, S2iBlockProperties):

        self.m_sDataDir = os.environ['HARPIA_DATA_DIR']

        filename = self.m_sDataDir + 'glade/findColor.ui'
        self.m_oPropertiesXML = PropertiesXML
        self.m_oS2iBlockProperties = S2iBlockProperties

        widget_list = [
            'Properties', '_R', '_G', '_B', '_R_T', '_G_T', '_B_T',
            'BackgroundColor', 'BorderColor', 'HelpView', 'prop_confirm'
        ]

        handlers = [
            'on_cancel_clicked', 'on_prop_confirm_clicked',
            'on_BackColorButton_clicked', 'on_BorderColorButton_clicked'
        ]

        top_window = 'Properties'

        GladeWindow.__init__(self, filename, top_window, widget_list, handlers)

        # load properties values
        self.block_properties = self.m_oPropertiesXML.getTag(
            "properties").getTag("block").getChildTags("property")

        for Property in self.block_properties:
            name = Property.getAttr("name")
            value = Property.getAttr("value")

            if name == "_R":
                self.widgets['_R'].set_value(float(value))
            if name == "_G":
                self.widgets['_G'].set_value(float(value))
            if name == "_B":
                self.widgets['_B'].set_value(float(value))
            if name == "_R_T":
                self.widgets['_R_T'].set_value(float(value))
            if name == "_G_T":
                self.widgets['_G_T'].set_value(float(value))
            if name == "_B_T":
                self.widgets['_B_T'].set_value(float(value))

                # if Property.name == "isFilling":
                # if Property.value == "true":
                # self.widgets['isFilling'].set_active( True );
                # else:
                # self.widgets['isFilling'].set_active( False );

                # if Property.name == "isScalling":
                # if Property.value == "true":
                # self.widgets['isScalling'].set_active( True );
                # else:
                # self.widgets['isScalling'].set_active( False );

                # if Property.name == "isCenter":
                # if Property.value == "true":
                # self.widgets['isAtCenter'].set_active( True );
                # else:
                # self.widgets['isAtPoint'].set_active( True );

        self.configure()

        # load help text
        t_oS2iHelp = XMLParser(self.m_sDataDir + 'help/findColor' +
                               _('_en.help'))

        t_oTextBuffer = gtk.TextBuffer()

        t_oTextBuffer.set_text(
            unicode(
                str(
                    t_oS2iHelp.getTag("help").getTag(
                        "content").getTagContent())))

        self.widgets['HelpView'].set_buffer(t_oTextBuffer)
Exemple #4
0
def parseAndGenerate(dirName, XMLChain, installDirName):
    cleanGenerator()
    global g_bSaveVideo  # Passando todas as variaveis globais devolta ao default
    global g_bLive
    global g_bVideo
    global g_bCameras
    global g_bFrameRate
    global g_ShowCount
    g_ShowCount = 0
    g_bSaveVideo = []
    g_bVideo = []
    g_bCameras = []
    g_bFrameRate = 0.1
    g_bLive = False  # this shall be a list containing the "blockNumbers" for each live acquisition block; len(g_bLive) works just like it is now..
    yield [_("Starting Up Generator")]
    #doc = binderytools.bind_file(XMLChain)
    doc = XMLParser(XMLChain)
    ########################Create the blocks from XMLChain############################

    ##################################################################################
    #				in the future we may want to show some kind of progress bar..
    #							the next few comented lines implement a progress counter.. this could be thrown up to the GUI via yields =]
    ##################################################################################
    # t_nBlockCount = 0.0
    # for block in (doc.harpia.properties.block):
    # t_nBlockCount += 1.0
    # t_nItCount = 0.0
    yield [_("Generating Code")]
    blocks = doc.getTag("harpia").getTag("properties").getChildTags("block")
    for blockIter in blocks:
        # print str(100.0*(t_nItCount/t_nBlockCount)) + "%"
        # t_nItCount += 1.0
        tmpBlock = blockTemplate()
        tmpBlock.blockType = blockIter.type
        tmpBlock.blockNumber = blockIter.id
        tmpBlock.properties = []
        tmpBlock.myConnections = []
        tmpBlock.outputsToSave = []
        try:
            block_properties = blockIter.getChildTags("property")
            for propIter in block_properties:
                tmpBlock.properties.append((propIter.name, propIter.value))
        except AttributeError:
            pass
        ID = tmpBlock.blockNumber
        tmpBlock.getBlockOutputTypes()

        net_blocks = doc.getTag("harpia").getTag("network").getChildTags(
            "block")
        for block in net_blocks:
            if (block.id == ID and int(block.type) <> 10):
                portCount = -1
                outputs = block.getTag("outputs").getChildTags("output")
                for output in outputs:
                    tmpConnection = connection()
                    portCount += 1
                    if output.inBlock != '--':
                        tmpConnection.sourceOutput = output.id
                        tmpConnection.destinationInput = output.input
                        tmpConnection.destinationNumber = output.inBlock
                        # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                        # !!!!!!cpscotti type-oriented connections...!!!!!
                        tmpConnection.connType = tmpBlock.outTypes[
                            int(tmpConnection.sourceOutput) - 1]
                        # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                        tmpBlock.myConnections.append(tmpConnection)
                    else:
                        tmpConnection.destinationNumber = '--'
                        tmpBlock.myConnections.append(tmpConnection)
                    try:
                        if output.grab == 'True':
                            tmpBlock.outputsToSave.append(output.id)
                    except:
                        pass
                    ###################################################################################
                    # ADDING TO EACH BLOCK OBJECT THE RESULTING CODE , THEN ADDING THE BLOCK IN A LIST #
                    ###################################################################################
                    ##Please, do not change the sequence
        tmpBlock.blockCodeWriter()
        tmpBlock.connectorCodeWriter()
        tmpBlock.saverCodeWriter()
        blockList.append(tmpBlock)
    ###################################################################################

    weights = []

    # Apply the weights on each connection of each block in listOfBlocks, then return a list with its connections
    def applyWeightsOnConnections(listOfBlocks):
        # def applyWeightsOnConnections(listOfBlocks,RollinList):
        ##For each block on listt:
        returnList = []
        for block in listOfBlocks:
            ##Put the connections on returnList
            for connection in block.myConnections:
                ##and apply the weight on this connection
                for tmpBlock in blockList:
                    if tmpBlock.blockNumber == connection.destinationNumber:
                        tmpBlock.weight += block.weight
                        if tmpBlock not in returnList:
                            # if tmpBlock not in RollinList:
                            returnList.append(tmpBlock)
                        # RollinList.append(tmpBlock)
        return returnList

    for block in blockList:
        # cpscotti..
        # if block.blockType == '00':
        if len(s2idirectory.block[int(
                block.blockType)]["InTypes"]) == 0 and len(
                    s2idirectory.block[int(block.blockType)]["OutTypes"]) <> 0:
            tmpList = []
            # RollinPathList = []
            tmpList.append(block)
            # RollinPathList.append(block)
            organizedChain = applyWeightsOnConnections(
                tmpList)  # ,RollinPathList)
            while organizedChain <> []:
                organizedChain = applyWeightsOnConnections(
                    organizedChain)  # ,RollinPathList)
            ###Recursive tests... future functionality
            # print "Start: "
            # for block in organizedChain:
            # print "\t"+str(block.blockNumber)

    biggestWeight = -1
    for block in blockList:
        if block.weight >= biggestWeight:
            biggestWeight = block.weight

    for activeWeight in range(biggestWeight):
        activeWeight += 1
        for block in blockList:
            if block.weight == activeWeight:
                arguments.append(block.functionArguments)
                images.append(block.imagesIO)
                functionCalls.append("//Weight: " + str(block.weight) + "\n")
                functionCalls.append(block.functionCall)
                deallocations.append(block.dealloc)
                outDeallocations.append(block.outDealloc)

    header = r"""// Auto-generated C Code - S2i Harpia
/*
*	In order to compile this source code run, in a terminal window, the following command:
*	gcc sourceCodeName.c `pkg-config --libs --cflags opencv` -o outputProgramName
*	
*	the `pkg-config ... opencv` parameter is a inline command that returns the path to both 
*	the libraries and the headers necessary when using opencv. The command also returns other necessary compiler options.
*/
// header:

#ifdef _CH_
#pragma package <opencv>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <opencv/cv.h>
#include <opencv/cxmisc.h>
#include <opencv/cxcore.h>
#include <opencv/ml.h>
#include <opencv/cvaux.h>
#include <opencv/cvwimage.h>
#include <opencv/highgui.h>
#include <math.h>

#define PI 3.1415926535898
double rads(double degs)
{
	return (PI/180 * degs);
}
		"""
    global usesFindSquares
    if usesFindSquares == 1:
        header += r"""

//Routines to findSquares
double angle( CvPoint* pt1, CvPoint* pt2, CvPoint* pt0 )
{
		double dx1 = pt1->x - pt0->x;
		double dy1 = pt1->y - pt0->y;
		double dx2 = pt2->x - pt0->x;
		double dy2 = pt2->y - pt0->y;
		return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}

CvSeq* findSquares4( IplImage* img, CvMemStorage* storage, int minArea, int maxArea)
{
		CvSeq* contours;
		int i, c, l, N = 11;
		int thresh = 50;
		CvSize sz = cvSize( img->width & -2, img->height & -2 );
		IplImage* timg = cvCloneImage( img ); // make a copy of input image
		IplImage* gray = cvCreateImage( sz, 8, 1 ); 
		IplImage* pyr = cvCreateImage( cvSize(sz.width/2, sz.height/2), 8, 3 );
		IplImage* tgray;
		CvSeq* result;
		double s, t;

		if(minArea == -1)
			minArea = 0;
		if(maxArea == -1)
			maxArea = (img->width * img->height);

		CvSeq* squares = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvPoint), storage );
		

		cvSetImageROI( timg, cvRect( 0, 0, sz.width, sz.height ));
		
		// down-scale and upscale the image to filter out the noise
		cvPyrDown( timg, pyr, CV_GAUSSIAN_5x5 );
		cvPyrUp( pyr, timg, CV_GAUSSIAN_5x5 );
		tgray = cvCreateImage( sz, 8, 1 );
		
		// find squares in every color plane of the image
		for( c = 0; c < 3; c++ )



		{
				// extract the c-th color plane
				cvSetImageCOI( timg, c+1 );
				cvCopy( timg, tgray, 0 );
				for( l = 0; l < N; l++ )
				{
						if( l == 0 )
						{
								cvCanny( tgray, gray, 0, thresh, 5 );
								cvDilate( gray, gray, 0, 1 );
						}
						else
						{
								cvThreshold( tgray, gray, (l+1)*255/N, 255, CV_THRESH_BINARY );
						}
						cvFindContours( gray, storage, &contours, sizeof(CvContour),
								CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
						while( contours )
						{
								result = cvApproxPoly( contours, sizeof(CvContour), storage,
										CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0 );
								if( result->total == 4 &&
										fabs(cvContourArea(result,CV_WHOLE_SEQ)) > minArea &&
										fabs(cvContourArea(result,CV_WHOLE_SEQ)) < maxArea &&
										cvCheckContourConvexity(result) )
								{
										s = 0;
										
										for( i = 0; i < 5; i++ )
										{
												if( i >= 2 )
												{
														t = fabs(angle(
														(CvPoint*)cvGetSeqElem( result, i ),
														(CvPoint*)cvGetSeqElem( result, i-2 ),
														(CvPoint*)cvGetSeqElem( result, i-1 )));
														s = s > t ? s : t;
												}
										}
										if( s < 0.3 )
												for( i = 0; i < 4; i++ )
														cvSeqPush( squares,
																(CvPoint*)cvGetSeqElem( result, i ));
								}
								contours = contours->h_next;
						}
				}
		}
		cvReleaseImage( &gray );
		cvReleaseImage( &pyr );
		cvReleaseImage( &tgray );
		cvReleaseImage( &timg );
		return squares;
}

double drawSquares( IplImage* cpy, CvSeq* squares )
{
		CvSeqReader reader;
		int i;
		cvStartReadSeq( squares, &reader, 0 );
		for( i = 0; i < squares->total; i += 4 )
		{
				CvPoint pt[4], *rect = pt;
				int count = 4;
				CV_READ_SEQ_ELEM( pt[0], reader );
				CV_READ_SEQ_ELEM( pt[1], reader );
				CV_READ_SEQ_ELEM( pt[2], reader );
				CV_READ_SEQ_ELEM( pt[3], reader );
				cvPolyLine( cpy, &rect, &count, 1, 1, CV_RGB(0,255,0), 3, CV_AA, 0 );
		}
	return (double)squares->total;
}

//End of routines to findSquares

			"""
    global usesFindColor
    if usesFindColor == 1:
        header += r"""

int GetColor(IplImage * imagem, int x, int y)
{
	return   (int)(((uchar*)(imagem->imageData + imagem->widthStep*y))[x]);
}

void SetColor(IplImage * imagem, int x, int y, uchar color)
{
	((uchar*)(imagem->imageData + imagem->widthStep*y))[x] = color;
}


void CheckImg(IplImage * img, uchar c_value, uchar tolerance)
{
	uchar min,max;
	int y_It,x_It;
	if((int)c_value < (int)tolerance)
		tolerance = c_value;

	if(((int)c_value+(int)tolerance) > 255)
		tolerance = 255 - c_value;

	min = c_value - tolerance;
	max = c_value + tolerance;

	for(y_It=0;y_It<(img->height);y_It++)
		for(x_It=0;x_It<(img->width);x_It++)
		{
				uchar val;
				val = GetColor(img,x_It,y_It);
				if(val >= min && val <= max)
					SetColor(img,x_It,y_It,255);
				else
					SetColor(img,x_It,y_It,0);
		}
}

CvPoint GetCenter(IplImage * src, long int * nOfPts)//, long int * numOfPoints)
{
	long int numOfMatchingPoints;
	long int posXsum;
	long int posYsum;
	int x_It, y_It;
	CvPoint Center;
	
	posXsum = 0;
	posYsum = 0;
	numOfMatchingPoints = 0;

	for(y_It=0;y_It<(src->height);y_It++)
		for(x_It=0;x_It<(src->width);x_It++)
			if(GetColor(src,x_It,y_It))
			{
				posXsum += x_It;
				posYsum += y_It;
				numOfMatchingPoints++;
			}

	if(numOfMatchingPoints > 0)
	{
		Center.x = (int)(posXsum/numOfMatchingPoints);
		Center.y = (int)(posYsum/numOfMatchingPoints);
	}
	else
		numOfMatchingPoints = -1;
// 	(*numOfPoints) = numOfMatchingPoints;
	if(nOfPts)
		*nOfPts = numOfMatchingPoints;

	return Center;
}


double dist22Points(CvPoint a, CvPoint b)
{
	int xD,yD;
	xD = a.x - b.x;
	yD = a.y - b.y;

	xD = (xD>0)?xD:-xD;
	yD = (yD>0)?yD:-yD;

	return (double)(xD*xD + yD*yD);
}

double GetVariance(IplImage * src,CvPoint center)//, long int * numOfPoints)
{
	long int numOfMatchingPoints;
	double distSquaresSum;
	double variance;
	int x_It,y_It;

	numOfMatchingPoints = 0;
	distSquaresSum = 0.0;


	for(y_It=0;y_It<(src->height);y_It++)
		for(x_It=0;x_It<(src->width);x_It++)
			if(GetColor(src,x_It,y_It))
			{
				numOfMatchingPoints++;
				distSquaresSum += dist22Points(center,cvPoint(x_It,y_It));
			}

	if(numOfMatchingPoints)
		variance = distSquaresSum/numOfMatchingPoints;
	else
		variance = -1;


	return variance;
}

long int CheckForColor(IplImage * src, IplImage * dst, uchar * c_value, uchar * tolerance, CvPoint * pointCenter, double * variance)
{
	uchar B,B_T,G,G_T,R,R_T;
	int i;
	long int numOfPoints;
	CvPoint centro;
	IplImage * m_pChans[3] = {NULL,NULL,NULL};

	numOfPoints = 0;

	B = c_value[0];
	G = c_value[1];
	R = c_value[2];

	B_T = tolerance[0];
	G_T = tolerance[1];
	R_T = tolerance[2];

	for(i=0;i<3;i++)
		m_pChans[i] = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U, 1);

	cvSplit(src,m_pChans[0],m_pChans[1],m_pChans[2], NULL);

	CheckImg(m_pChans[0],B,B_T);
	CheckImg(m_pChans[1],G,G_T);
	CheckImg(m_pChans[2],R,R_T);

	cvAnd(m_pChans[0], m_pChans[1], dst, NULL );
	cvAnd(m_pChans[2], dst, dst, NULL );

	centro = GetCenter(dst,&numOfPoints);//,&numOfPoints);

	if(numOfPoints != -1)
		*variance = GetVariance(dst,centro);

	pointCenter->x = centro.x;
	pointCenter->y = centro.y;

	cvReleaseImage( &m_pChans[0] );
	cvReleaseImage( &m_pChans[1] );
	cvReleaseImage( &m_pChans[2] );

	return numOfPoints;
}
			"""
    header += "\nint main(int argc, char ** argv)\n{"

    declaration = "\n\t//declaration block\n"

    for x in arguments:
        declaration = declaration + x

    for x in images:
        declaration = declaration + x

    if g_bLive:
        declaration += \
            'int end;  end = 0; int key; \n'
        for aCapture in g_bVideo:
            declaration += 'CvCapture * block' + aCapture[
                0] + '_capture = NULL; \n IplImage * block' + aCapture[
                    0] + '_frame = NULL; \n block' + aCapture[
                        0] + '_capture = cvCreateFileCapture("' + aCapture[
                            1] + '"); \n'
        for aCamera in g_bCameras:
            declaration += 'CvCapture * block' + aCamera[
                0] + '_capture = NULL; \n IplImage * block' + aCamera[
                    0] + '_frame = NULL; \n block' + aCamera[
                        0] + '_capture = cvCaptureFromCAM(' + aCamera[
                            1] + '); \n'
        declaration += 'while(!end) \n {\t \n'

        for aCapture in g_bVideo:
            declaration += 'cvGrabFrame (block' + aCapture[
                0] + '_capture); \n block' + aCapture[
                    0] + '_frame = cvRetrieveFrame (block' + aCapture[
                        0] + '_capture); \n'

        for aCamera in g_bCameras:
            declaration += 'cvGrabFrame (block' + aCamera[
                0] + '_capture); \n block' + aCamera[
                    0] + '_frame = cvRetrieveFrame (block' + aCamera[
                        0] + '_capture); \n'

    execution = "\n\t//execution block\n"
    for x in functionCalls:
        execution += x
    if g_ShowCount == 0:
        execution += '\n\tcvNamedWindow("Control Window",CV_WINDOW_AUTOSIZE );'
    if g_bLive:
        execution += '\n\tkey = cvWaitKey (' + str(
            int((1.0 / g_bFrameRate) *
                1000.0)) + ');\n if(key != -1)\n end = 1;'

        deallocating = "\n\t//deallocation block\n"
        for x in deallocations:
            deallocating += x

        deallocating += "}"

    else:
        deallocating = "\n\t//deallocation block\n"

        for x in deallocations:
            deallocating += x

    closing = ""
    closing += "\n"
    for outDea in outDeallocations:
        closing += outDea
    if g_bLive:
        for aCapture in g_bVideo:
            closing += 'cvReleaseCapture(&block' + aCapture[0] + '_capture);\n'
        for aCamera in g_bCameras:
            closing += 'cvReleaseCapture(&block' + aCamera[0] + '_capture);\n'
    for vWriter in g_bSaveVideo:
        closing += 'cvReleaseVideoWriter(&block' + vWriter + '_vidWriter);\n'
    closing += "return 0;\n } //closing main()\n"

    # Final code assembly
    entireCode = header + declaration + execution + deallocating + closing

    yield [_("Saving Code")]
    # saving code file
    os.chdir(tmpDir + dirName)
    codeFilename = dirName + '.c'
    codeFile = open(codeFilename, 'w')
    codeFile.write(entireCode)
    codeFile.close()

    yield [_("Building Makefile")]
    # Assembly of "necessary" makefiles
    # ...windows..
    makeFilename = 'Makefile' + dirName + '.bat'
    makeFileEntry = '"' + installDirName + '\\bin\\gcc.exe" ' + codeFilename + " -o " + codeFilename[:
                                                                                                     -2] + ".exe -lcv -lcxcore -lhighgui"
    makeFile = open(makeFilename, 'w')
    makeFile.write(makeFileEntry)
    makeFile.close()

    # ...posix..
    makeFilename = 'Makefile.' + dirName
    makeFileEntry = "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:" + installDirName + "/lib/; export PKG_CONFIG_PATH=" + installDirName + "/lib/pkgconfig/;g++ " + codeFilename + " -o " + codeFilename[:
                                                                                                                                                                                                    -2] + " `pkg-config --cflags --libs opencv`"
    makeFile = open(makeFilename, 'w')
    makeFile.write(makeFileEntry)
    makeFile.close()

    yield [_("Compiling ...")]
    if os.name == "nt":
        i, o = os.popen4('Makefile' + dirName + '.bat')

        o.readlines()
        o.close()
        i.close()
        yield [_("Running ...")]
        i, o = os.popen4(codeFilename[:-2] + '.exe')

        ## ERROR LOG
        Error = ''
        errorList = o.readlines()
        for element in errorList:
            Error = Error + element

        SetErrorLog(Error)

        o.readlines()
        o.close()
        i.close()
    else:
        i, o = os.popen4("sh " + makeFilename)

        ## appending compile errors too.. helps finding bugs! =]
        CompilingErrors = ''
        CerrorList = o.readlines()

        if len(CerrorList) <> 0:
            CompilingErrors += "Something was detected while compiling the source code.\n" + \
                               "There is a huge chance you've found a bug, please report to [email protected] \n" + \
                               "sending the processing chain (.hrp), this error message and some description on what you were doing.\n" + \
                               "We appreciate your help!\n"
        for element in CerrorList:
            CompilingErrors += element

        o.close()
        i.close()
        if g_bLive:
            yield [
                _("Running, press any key (on the video output window) to terminate."
                  ), CompilingErrors
            ]
        else:
            yield [_("Running ..."), CompilingErrors]

        # cpscotti, xunxo bunitinho pra nao travar a interface qndo tive rodando o live =]
        t_oPrg = RunPrg("LD_LIBRARY_PATH=" + installDirName + "/lib/ ./" +
                        codeFilename[:-2])
        t_oPrg.start()
        while t_oPrg.isAlive():
            t_oPrg.join(0.4)
            while gtk.events_pending():
                gtk.main_iteration(False)

        # fim do "xunx"

        ## ERROR LOG
        o = open("RunErrorLog", "r")
        Error = ''
        errorList = o.readlines()
        for element in errorList:
            Error += element

        yield [_("Leaving.."), Error]
        SetErrorLog(CompilingErrors + Error)

        o.close()
        i.close()
Exemple #5
0
def parseAndGenerate(dirName, XMLChain, installDirName):
    blockList = []
    outDeallocations = []
    functionCalls = []
    headers = []
    images = []
    deallocations = []
    global g_bLive
    g_bLive = []
    global g_bFrameRate
    g_bFrameRate = 0.1

    yield [_("Starting Up Generator")]
    doc = XMLParser(XMLChain)
    ########################Create the blocks from XMLChain############################
    ##################################################################################
    # in the future we may want to show some kind of progress bar..
    # the next few comented lines implement a progress counter.. this could be thrown up to the GUI via yields =]
    ##################################################################################
    # t_nBlockCount = 0.0
    # for block in (doc.harpia.properties.block):
    # t_nBlockCount += 1.0
    # t_nItCount = 0.0
    yield [_("Generating Code")]
    blocks = doc.getTag("harpia").getTag("properties").getChildTags("block")
    for blockIter in blocks:
        tmpBlock = blockTemplate(blockIter.type, blockIter.id)
        try:
            block_properties = blockIter.getChildTags("property")
            for propIter in block_properties:
                tmpBlock.properties.append((propIter.name, propIter.value))
        except AttributeError:
            pass
        tmpBlock.getBlockOutputTypes()

        net_blocks = doc.getTag("harpia").getTag("network").getChildTags(
            "block")
        for block in net_blocks:
            if (block.id != blockIter.id):
                continue
            outputs = block.getTag("outputs").getChildTags("output")
            for output in outputs:
                tmpConnection = connection()
                if output.inBlock != '--':
                    tmpConnection.sourceOutput = output.id
                    tmpConnection.destinationInput = output.input
                    tmpConnection.destinationNumber = output.inBlock
                    tmpConnection.connType = tmpBlock.outTypes[
                        int(tmpConnection.sourceOutput) - 1]
                    tmpBlock.myConnections.append(tmpConnection)
                else:
                    tmpConnection.destinationNumber = '--'
                    tmpBlock.myConnections.append(tmpConnection)
        #######################################################################
        ##Please, do not change the sequence
        tmpBlock.blockCodeWriter()
        tmpBlock.connectorCodeWriter()
        blockList.append(tmpBlock)
        #######################################################################

    weights = []

    for block in blockList:
        if len(s2idirectory.block[int(block.blockType)]["InTypes"]) != 0:
            continue
        if len(s2idirectory.block[int(block.blockType)]["OutTypes"]) == 0:
            continue
        tmpList = []
        tmpList.append(block)
        organizedChain = __apply_weights_on_connections(tmpList, blockList)
        while organizedChain != []:
            organizedChain = __apply_weights_on_connections(
                organizedChain, blockList)

    biggestWeight = -1
    for block in blockList:
        if block.weight >= biggestWeight:
            biggestWeight = block.weight

    for activeWeight in range(biggestWeight):
        activeWeight += 1
        for block in blockList:
            if block.weight == activeWeight:
                headers.append(block.header)
                images.append(block.imagesIO)
                functionCalls.append("//Weight: " + str(block.weight) + "\n")
                functionCalls.append(block.functionCall)
                deallocations.append(block.dealloc)
                outDeallocations.append(block.outDealloc)

    header = r"""// Auto-generated C Code - S2i Harpia
/*
*	In order to compile this source code run, in a terminal window, the following command:
*	gcc sourceCodeName.c `pkg-config --libs --cflags opencv` -o outputProgramName
*	
*	the `pkg-config ... opencv` parameter is a inline command that returns the path to both 
*	the libraries and the headers necessary when using opencv. The command also returns other necessary compiler options.
*/
// header:

#ifdef _CH_
#pragma package <opencv>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <opencv/cv.h>
#include <opencv/cxmisc.h>
#include <opencv/cxcore.h>
#include <opencv/ml.h>
#include <opencv/cvaux.h>
#include <opencv/cvwimage.h>
#include <opencv/highgui.h>
#include <math.h>

		"""

    # Adds only if it does not contains
    temp_header = []
    for header_code in headers:
        if header_code not in temp_header:
            temp_header.append(header_code)

    for header_code in temp_header:
        header += header_code

    header += "\nint main(int argc, char ** argv)\n{"

    declaration = "\n//declaration block\n"

    for image in images:
        declaration += image

    if len(g_bLive) > 0:
        declaration += 'int end = 0;\n'
        declaration += 'int key;\n'
        declaration += 'while(!end) \n {\t \n'

        for value in g_bLive:
            declaration += 'cvGrabFrame(block' + value[0] + '_capture);\n' + \
                'block' + value[0] + '_frame = cvRetrieveFrame (block' + value[0] + '_capture);\n'

    execution = "\n\t//execution block\n"
    for x in functionCalls:
        execution += x

    if len(g_bLive) > 0:
        execution += 'key = cvWaitKey (' + str(
            int((1.0 / g_bFrameRate) * 1000.0)) + ');\n'
        execution += 'if(key != -1)\n'
        execution += 'end = 1;\n'

    deallocating = "\n\t//deallocation block\n"
    for x in deallocations:
        deallocating += x

    if len(g_bLive) > 0:
        deallocating += "}"

    closing = ""
    closing += "\n"
    for outDea in outDeallocations:
        closing += outDea

    closing += "return 0;\n } //closing main()\n"

    # Final code assembly
    entireCode = header + declaration + execution + deallocating + closing

    yield [_("Saving Code")]
    # saving code file
    os.chdir(tmpDir + dirName)
    codeFilename = dirName + '.c'
    codeFile = open(codeFilename, 'w')
    codeFile.write(entireCode)
    codeFile.close()

    yield [_("Building Makefile")]
    # Assembly of "necessary" makefiles
    # ...windows..
    makeFilename = 'Makefile' + dirName + '.bat'
    makeFileEntry = '"' + installDirName + '\\bin\\gcc.exe" ' + codeFilename + " -o " + codeFilename[:
                                                                                                     -2] + ".exe -lcv -lcxcore -lhighgui"
    makeFile = open(makeFilename, 'w')
    makeFile.write(makeFileEntry)
    makeFile.close()

    # ...posix..
    makeFilename = 'Makefile.' + dirName
    makeFileEntry = "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:" + installDirName + "/lib/;"
    makeFileEntry += "export PKG_CONFIG_PATH=" + installDirName + "/lib/pkgconfig/;"
    makeFileEntry += "g++ " + codeFilename + " -o " + codeFilename[:-2] + " `pkg-config --cflags --libs opencv`"
    makeFile = open(makeFilename, 'w')
    makeFile.write(makeFileEntry)
    makeFile.close()

    yield [_("Compiling ...")]
    if os.name == "nt":
        i, o = os.popen4('Makefile' + dirName + '.bat')

        o.readlines()
        o.close()
        i.close()
        yield [_("Running ...")]
        i, o = os.popen4(codeFilename[:-2] + '.exe')

        ## ERROR LOG
        Error = ''
        errorList = o.readlines()
        for element in errorList:
            Error = Error + element

        __set_error_log(Error)

        o.readlines()
        o.close()
        i.close()
    else:
        i, o = os.popen4("sh " + makeFilename)

        ## appending compile errors too.. helps finding bugs! =]
        CompilingErrors = ''
        CerrorList = o.readlines()

        if len(CerrorList) != 0:
            CompilingErrors += "Something was detected while compiling the source code.\n" + \
                               "There is a huge chance you've found a bug, please report to [email protected] \n" + \
                               "sending the processing chain (.hrp), this error message and some description on what you were doing.\n" + \
                               "We appreciate your help!\n"
        for element in CerrorList:
            CompilingErrors += element

        o.close()
        i.close()

        if len(g_bLive) > 0:
            yield [
                _("Running, press any key (on the video output window) to terminate."
                  ), CompilingErrors
            ]
        else:
            yield [_("Running ..."), CompilingErrors]

        t_oPrg = RunPrg("LD_LIBRARY_PATH=" + installDirName + "/lib/ ./" +
                        codeFilename[:-2])
        t_oPrg.start()
        while t_oPrg.isAlive():
            t_oPrg.join(0.4)
            while gtk.events_pending():
                gtk.main_iteration(False)

        ## ERROR LOG
        o = open("RunErrorLog", "r")
        Error = ''
        errorList = o.readlines()
        for element in errorList:
            Error += element

        yield [_("Leaving.."), Error]
        __set_error_log(CompilingErrors + Error)

        o.close()
        i.close()
Exemple #6
0
    def on_ProcessToolBar_clickedIneer(self):
        page = self.widgets['WorkArea'].get_current_page()
        if self.diagrams.has_key(page):
            self.UpdateStatus(0)

            diagram = self.diagrams[page]

            process_XML = XMLParser("<harpia>" + \
                                           str(DiagramControl(diagram).get_process_chain()) + \
                                           "</harpia>", fromString=True)

            graph_size = len(list(process_XML.getTag("harpia").getTag("properties").getTagChildren()))

            if graph_size > 1:
                blocks = process_XML.getTag("harpia").getTag("properties").getChildTags("block")
                for t_oBlockProperties in blocks:
                    block_properties = t_oBlockProperties.getChildTags("property")
                    if int(t_oBlockProperties.type) == 00:  # 00 = acquisition block
                        inputType = 'file'
                        for block_property in block_properties:
                            if block_property.name == 'type':
                                print block_property.name
                                inputType = block_property.value

                            # adoção do paradigma monolítico.. nada de ficar mandando imagens por sockets!!
                            if block_property.name == 'filename' and inputType == 'file':
                                block_property.value = os.path.expanduser(block_property.value)
                                block_property.value = os.path.realpath(block_property.value)
                                if (not os.path.exists(block_property.value)):
                                    errMsg = _("Bad Filename: ") + block_property.value
                                    print(errMsg)
                                    self.__set_status_message(errMsg, 0)
                                    return

                    if int(t_oBlockProperties.type) == 01:  # 01 => save image
                        for block_property in block_properties:
                            if block_property.name == 'filename':
                                block_property.value = os.path.realpath(block_property.value)


                    # seguindo o paradigma de não mandar mais nada.. vamos testar com o haar =]
                    # não vamos mandar mais nada mas vamos traduzir o path do haarCascade pra algo real
                    if int(t_oBlockProperties.type) == 610:  # 610 => haar detector... passando a cascade .xml
                        for block_property in block_properties:
                            if block_property.name == 'cascade_name':
                                block_property.value = os.path.realpath(block_property.value)
                                if (not os.path.exists(block_property.value)):
                                    errMsg = _("Bad Filename: ") + block_property.value
                                    print(errMsg)
                                    self.__set_status_message(errMsg, 0)
                                    return

                # cpscotti standalone!!!
                process_chain = []  # lista pra n precisar ficar copiando prum lado e pro otro o xml inteiro
                process_chain.append(process_XML.getXML())

                session_manager = s2iSessionManager.s2iSessionManager()

                ## pegando o novo ID (criado pela s2iSessionManager) e passando para o s2idiagram
                self.diagrams[page].set_session_id(session_manager.session_id)

                # step sempre sera uma lista.. primeiro elemento eh uma mensagem, segundo eh o erro.. caso exista erro.. passar para o s2idiagram tb!
                self.diagrams[page].set_error_log('')
                t_bEverythingOk = True
                for step in session_manager.new_instance(process_chain):
                    if len(step) > 1:
                        if step[1] != '' and step[1] != None:
                            self.diagrams[page].append_error_log(step[1])
                            t_bEverythingOk = False
                    self.__set_status_message(step[0], t_bEverythingOk)
                    # self.widgets['StatusLabel'].set_text()
                    # print t_bEverythingOk
                    print step[0]
                # yield step#util caso se resolva usar a interface "lenta" ou se descubra como atualizar rapidamente a GUI

        # falta pegar o retorno!!!!!!
        self.UpdateStatus(7)
Exemple #7
0
 def ReturnResponse(self):
     t_oResponse = XMLParser(self.HARPIARESPONSE, fromString=True)
     return t_oResponse