cv::Mat DecodeDatumToCVMatNative(const Datum& datum) { cv::Mat cv_img; CHECK(datum.encoded()) << "Datum not encoded"; const string& data = datum.data(); std::vector<char> vec_data(data.c_str(), data.c_str() + data.size()); cv_img = cv::imdecode(vec_data, -1); if (!cv_img.data) { LOG(ERROR) << "Could not decode datum "; } return cv_img; }
cv::Mat DecodeDatumToCVMat(const Datum& datum, bool is_color) { cv::Mat cv_img; CHECK(datum.encoded()) << "Datum not encoded"; const string& data = datum.data(); std::vector<char> vec_data(data.c_str(), data.c_str() + data.size()); int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR : CV_LOAD_IMAGE_GRAYSCALE); cv_img = cv::imdecode(vec_data, cv_read_flag); if (!cv_img.data) { LOG(ERROR) << "Could not decode datum "; } return cv_img; }
TagValueFunction* tnfa_match(TNFA* tnfa, char* input, int anchorStart, int anchorEnd){ Vector* reach, *reachNext, *tmp; TagValueFunction** reachTVFs, **reachNextTVFs, **tmpTVFs; int i; int leftmostMatchFound = 0; TagValueFunction* finalTVF = NULL; void** vd; TNFAMatcher m; /* Construct the matcher Object */ m.tnfa = tnfa; m.pos = 0; m.counts = ecalloc(tnfa->laststate+1,sizeof(int)); m.queue = ibqueue_create(tnfa->laststate+1); m.priorities = ecalloc(tnfa->laststate+1,sizeof(PriorityList*)); m.tmp = tvf_create(tnfa->tagcount); m.tvfpool = opool_createWithExt((void*(*)(void*))tvf_create,shared_free,NULL,NULL,(void*)tnfa->tagcount,10); m.plistpool = opool_createWithExt((void*(*)(void*))plist_create,shared_free,NULL,NULL,(void*)tnfa->maxpath,10); reach = vec_createDefault(); reachTVFs = ecalloc(tnfa->laststate+1,sizeof(TagValueFunction*)); reachNext = vec_createDefault(); reachNextTVFs = ecalloc(tnfa->laststate+1,sizeof(TagValueFunction*)); /* Initialize reach to consistentClosure({<s,v0>}); */ vec_add(reach,(void*)tnfa->start); reachTVFs[tnfa->start] = (TagValueFunction*)opool_grab(m.tvfpool); reach = tnfa_close(m,reach,reachTVFs); /* If this regex matches the empty string then we may already have a possible match :P */ if(reachTVFs[tnfa->finish]!=NULL && (!anchorEnd || (anchorEnd && !*input)) ){ leftmostMatchFound = 1; finalTVF = (TagValueFunction*)opool_grab(m.tvfpool); tvf_copyInto(finalTVF,reachTVFs[tnfa->finish],m.tnfa->tagcount); } /*{int i; printf("{"); for(i=0;i<vec_size(reach);i++) printf("%d,",vec_get(reach,i)); printf("}"); printf("{"); for(i=0;i<m.tnfa->laststate+1;i++){ int j;int found=0; if(reachTVFs[i] != NULL){ for(j=0;j<vec_size(reach);j++) if(vec_get(reach,j) == i) found =1; if(found) printf("*,"); else printf("?"); } else { printf("_,"); } } printf("}\n"); } */ //printf("\nclosure(s%d) = %s",tnfa->start,match_toString(reach,tnfa)); /* while pos < |input| */ while(*input && vec_size(reach) > 0){ /* fetch the next input symbol */ char c = *input; input ++; m.pos++; vec_clear(reachNext); memset(reachNextTVFs,'\0',(tnfa->laststate+1) * sizeof(TagValueFunction*)); /* For each item <q,v> in reach */ for(i=vec_size(reach)-1,vd=vec_data(reach);i>=0;i--){ TFATrans* t; TagValueFunction* v; int q = (int)vd[i]; v = reachTVFs[q]; /* For each transition on c out of q do */ t = tnfa->cStates[q]; while(t != NULL){ /* add <t->to,v> to reachNext */ if(t->c == c){ /* WARNING: ... dangerous : I believe that with NFAs constructed with the thompson construction there is a 1to1 mapping between 'from' states and 'to' states on a non-epsilon transition. If that were not the case then there might be two different ways to get to the same state from reach on c and I would need to add those seperately to reachNext prohibiting the use of a hash */ reachNextTVFs[t->to] = (TagValueFunction*)opool_grab(m.tvfpool); tvf_copyInto(reachNextTVFs[t->to],v,m.tnfa->tagcount); vec_add(reachNext,(void*)t->to); } t = t->next; } } /* Free reach */ for(i=vec_size(reach)-1,vd=vec_data(reach);i>=0;i--) opool_release(m.tvfpool,reachTVFs[(int)vd[i]]); if(!anchorStart && (!leftmostMatchFound || anchorEnd)){ if(reachNextTVFs[tnfa->start] == NULL){ reachNextTVFs[tnfa->start] = (TagValueFunction*)opool_grab(m.tvfpool); tvf_initialize(reachNextTVFs[tnfa->start],tnfa->tagcount); vec_add(reachNext,(void*)tnfa->start); } } reachNext = tnfa_close(m,reachNext,reachNextTVFs); if(reachNextTVFs[tnfa->finish] != NULL){ leftmostMatchFound = 1; if( (!anchorEnd && (finalTVF == NULL || reachNextTVFs[tnfa->finish][0] <= finalTVF[0])) || (anchorEnd && !*input) ){ if(finalTVF!=NULL) opool_release(m.tvfpool,finalTVF); finalTVF = (TagValueFunction*)opool_grab(m.tvfpool); tvf_copyInto(finalTVF,reachNextTVFs[tnfa->finish],m.tnfa->tagcount); // printf("\tFound potential match: %s\n",match_toString(reachNext,tnfa)); } } //printf("\nclosure = %s",match_toString(reachNext,tnfa)); // Swap reach and reachNext tmp = reach; tmpTVFs = reachTVFs; reach = reachNext; reachTVFs = reachNextTVFs; reachNext = tmp; reachNextTVFs = tmpTVFs; } /* Free reach */ for(i=vec_size(reach)-1,vd=vec_data(reach);i>=0;i--) opool_release(m.tvfpool,reachTVFs[(int)vd[i]]); vec_free(reach); free(reachTVFs); vec_free(reachNext); free(reachNextTVFs); free(m.tmp); free(m.counts); free(m.priorities); ibqueue_free(m.queue); if(finalTVF){ TagValueFunction* tmp = finalTVF; finalTVF = tvf_copyInto(tvf_create(tnfa->tagcount),finalTVF,tnfa->tagcount); opool_release(m.tvfpool,tmp); } else { finalTVF = tvf_create(tnfa->tagcount); } if(anchorEnd && *input) { TagValueFunction* tmp = finalTVF; finalTVF = tvf_create(tnfa->tagcount); free(tmp); } opool_free(m.tvfpool); opool_free(m.plistpool); return finalTVF; }
Vector* tnfa_close(TNFAMatcher m, Vector* states, TagValueFunction** tvfs){ int i; void** vd; /* Initialize the Priority lists */ memset(m.priorities,'\0',(m.tnfa->laststate+1) * sizeof(PriorityList*)); /* For each pair <q,v> in the input add q to the queue */ ibqueue_clear(m.queue); for(i=vec_size(states)-1,vd=vec_data(states);i>=0;i--) ibqueue_enqueue(m.queue,(int)vd[i]); /* For each state s initialize count(s) to the input order of s */ memcpy(m.counts,m.tnfa->cInputOrder,(m.tnfa->laststate+1)*sizeof(int)); /* While queue is not empty */ while(!ibqueue_isEmpty(m.queue)){ int s1 = ibqueue_dequeue(m.queue); TFATrans* t = m.tnfa->cStates[s1]; TagValueFunction* v1 = tvfs[s1],*curr_v2,*v2=m.tmp; PriorityList* p1 = m.priorities[s1],*curr_p2,*p2; /* For each transition out of s1 */ while(t){ /* If this is an epsilon transition */ if(t->c == '\0') { int s2 = t->to; curr_v2 = tvfs[s2]; v2 = tvf_copyInto(v2,v1,m.tnfa->tagcount); curr_p2 = m.priorities[s2]; /* v_2(x) = {pos if x==t->tag and t->tag != -1 , v_1(x) otherwise } */ if(t->tag != -1) tvf_setTagValue(v2,t->tag,m.pos,m.tnfa->tagcount); if(t->priority == NO_PRIORITY) if(p1 == NULL) p2 = NULL; else p2 = plist_copyInto((PriorityList*)opool_grab(m.plistpool),p1); else if(p1==NULL){ p2 = (PriorityList*)opool_grab(m.plistpool); p2->length = 1; p2->priority[0] = t->priority; } else { // Never cycle twice! if(t->c == CYCLE_PRIORITY) { int twoCycles = 0; for(i=0;i<p1->length;i++) if (p1->priority[i]==CYCLE_PRIORITY){ twoCycles = 1; break; } if(twoCycles) continue; } p2 = plist_copyIntoAndInsert((PriorityList*)opool_grab(m.plistpool),p1,t->priority); } /* if no result for s2 is defined OR v2 is of a greater priority than the current result for s2 */ // printf("Comparing priority of s%d:%s from s%d:%s with s%d:%s = %d\n", // s2,plist_toString(p2),s1,plist_toString(p1),s2,plist_toString(curr_p2),plist_priority(p2,curr_p2)); if(curr_v2 == NULL || curr_v2[0] > v2[0] || (curr_v2[0] == v2[0] && plist_priority(p2,curr_p2) >= 0 ) ){ int curCount = m.counts[s2]; /* set the current result for s2 */ if (curr_v2 != NULL) tvfs[s2] = tvf_copyInto(tvfs[s2],v2,m.tnfa->tagcount); else { tvfs[s2] = (TagValueFunction*)opool_grab(m.tvfpool); tvf_copyInto(tvfs[s2],v2,m.tnfa->tagcount); } m.priorities[s2]=p2; if (curr_v2 == NULL) vec_add(states,(void*)s2); if (curr_p2 != NULL) opool_release(m.plistpool, curr_p2); /* {int i; printf("{"); for(i=0;i<vec_size(states);i++) printf("%d,",vec_get(states,i)); printf("}"); printf("{"); for(i=0;i<m.tnfa->laststate+1;i++){ int j;int found=0; if(tvfs[i] != NULL){ for(j=0;j<vec_size(states);j++) if(vec_get(states,j) == i) found =1; if(found) printf("*,"); else printf("?"); } else { printf("_,"); } } printf("}\n"); } */ /* decrease count(s2) by 1 */ --curCount; /* if count(s2) ==0 */ if(curCount == 0) { /* put s2 at the front of the queue and reset its count to its input order */ ibqueue_prequeue(m.queue,s2); m.counts[s2]=m.tnfa->cInputOrder[s2]; } else { /* put s2 at the back of the queue and set its count to its old count -1 */ ibqueue_enqueue(m.queue,s2); m.counts[s2]=curCount; } } else { if (p2 != NULL) opool_release(m.plistpool,p2); } } t=t->next; } } /* Free the plists */ for(i=vec_size(states)-1,vd=vec_data(states);i>=0;i--){ int q= (int)vd[i]; if(m.priorities[q] != NULL) opool_release(m.plistpool,m.priorities[q]); } return states; }