From: <tum...@li...> - 2012-02-05 18:32:45
|
Revision: 989 http://tum-ros-pkg.svn.sourceforge.net/tum-ros-pkg/?rev=989&view=rev Author: moesenle Date: 2012-02-05 18:32:38 +0000 (Sun, 05 Feb 2012) Log Message: ----------- Author: Dejan Pangercic <dej...@cs...> Date: Sun Feb 5 19:32:04 2012 +0100 commit 0d6623d44c2cb9056ee62606f63b79be374f2dfe Author: Dejan Pangercic <dej...@cs...> Date: Sun Feb 5 19:31:38 2012 +0100 refactored the code commit 2b90812f874db614252cc411ea9f6581e05cc527 Author: Dejan Pangercic <dej...@gm...> Date: Tue Jan 31 17:56:00 2012 +0100 public export git-commit Modified Paths: -------------- perception/zbar_barcode_reader_node/launch/barcode_reader.launch perception/zbar_barcode_reader_node/rosdep.yaml perception/zbar_barcode_reader_node/src/zbar_barcode_reader_node.cpp Modified: perception/zbar_barcode_reader_node/launch/barcode_reader.launch =================================================================== --- perception/zbar_barcode_reader_node/launch/barcode_reader.launch 2012-01-31 16:56:47 UTC (rev 988) +++ perception/zbar_barcode_reader_node/launch/barcode_reader.launch 2012-02-05 18:32:38 UTC (rev 989) @@ -1,5 +1,12 @@ <launch> - <node pkg="zbar_barcode_reader_node" type="zbar_barcode_reader_node" name="barcode_reader_node" output="screen" respawn="true"> + <node pkg="zbar_barcode_reader_node" type="zbar_barcode_reader_node" name="barcode_reader_node" output="screen" respawn="false"> <param name="input_image_topic" type="string" value="/image_raw" /> + <param name="link1" type="string" value="http://www.barcoo.com/api/get_product_complete?pi=" /> + <param name="link2" type="string" value="&pins=ean&format=xml&source=ias-tum" /> + <param name="tag1" type="string" value="answer" /> + <param name="tag2" type="string" value="picture_high" /> + <param name="tag3" type="string" value="picture_low" /> + <param name="image_pattern" type="string" value="<meta property="og:image" content="" /> + </node> </launch> Modified: perception/zbar_barcode_reader_node/rosdep.yaml =================================================================== --- perception/zbar_barcode_reader_node/rosdep.yaml 2012-01-31 16:56:47 UTC (rev 988) +++ perception/zbar_barcode_reader_node/rosdep.yaml 2012-02-05 18:32:38 UTC (rev 989) @@ -1,7 +1,11 @@ libcurl: ubuntu: 11.04: libcurl3 + oneiric: libcurl3 libtinyxml: ubuntu: 11.04: libtinyxml-dev + oneiric: libtinyxml-dev + + Modified: perception/zbar_barcode_reader_node/src/zbar_barcode_reader_node.cpp =================================================================== --- perception/zbar_barcode_reader_node/src/zbar_barcode_reader_node.cpp 2012-01-31 16:56:47 UTC (rev 988) +++ perception/zbar_barcode_reader_node/src/zbar_barcode_reader_node.cpp 2012-02-05 18:32:38 UTC (rev 989) @@ -22,6 +22,13 @@ using namespace zbar; class BarcodeReaderNode { + std:: string link1_;// = "http://www.barcoo.com/api/get_product_complete?pi="; + std:: string link2_;// = "&pins=ean&format=xml&source=ias-tum"; + std:: string tag1_;// = "answer"; + std:: string tag2_;// = "picture_high"; + std:: string tag3_;// = "picture_low"; + std:: string pattern_;// = "<meta property=\"og:image\" content=\""; + TiXmlDocument doc; public: struct memoryStruct { @@ -39,8 +46,19 @@ BarcodeReaderNode(ros::NodeHandle &n) : n_(n), it_(n_) { - n_.param ("input_image_topic", input_image_topic_, std::string("/stereo/left/image_rect")); + //link1 = "http://www.barcoo.com/api/get_product_complete?pi="; + //link2 = "&pins=ean&format=xml&source=ias-tum"; + //tag1 = "answer"; + //tag2 = "picture_high"; + //tag3 = "picture_low"; + + n_.param ("input_image_topic", input_image_topic_, std::string("/image_raw")); n_.param ("outout_barcode_topic", output_barcode_topic_, std::string("barcode")); + n_.param ("link1", link1_, std::string("http://www.barcoo.com/api/get_product_complete?pi=")); + n_.param ("link2", link2_, std::string("&pins=ean&format=xml&source=ias-tum")); + n_.param ("tag2", tag2_, std::string("picture_high")); + n_.param ("tag3", tag3_, std::string("picture_low")); + n_.param ("image_pattern", pattern_, std::string("<meta property=\"og:image\" content=\"")); image_sub_ = it_.subscribe( input_image_topic_, 1, &BarcodeReaderNode::imageCallback, this); barcode_pub_ = @@ -67,7 +85,7 @@ { UserData *userdata = reinterpret_cast<UserData *>(data); struct memoryStruct *mem = userdata->memory; - BarcodeReaderNode *self = userdata->self; + //BarcodeReaderNode *self = userdata->self; size_t realsize = size * nmemb; mem->memory = (char *) @@ -100,11 +118,10 @@ return result; } - int getImage(std::string & image) + int getImage(std::string image, cv::Mat * imgTmp) { CURL *curl; // CURL objects CURLcode res; - cv::Mat imgTmp; // image object memoryStruct buffer; // memory buffer UserData userdata(&buffer, this); @@ -129,16 +146,7 @@ // get the image from the specified URL res = curl_easy_perform(curl); // decode memory buffer using OpenCV - imgTmp = cv::imdecode(cv::Mat(1, buffer.size, CV_8UC1, buffer.memory), CV_LOAD_IMAGE_UNCHANGED); - // display image (if we got / decoded it correctly) - - - if (!(imgTmp.empty())) - { - imshow("Barcoo img", imgTmp); - } - cv::waitKey(3); - + *imgTmp = cv::imdecode(cv::Mat(1, buffer.size, CV_8UC1, buffer.memory), CV_LOAD_IMAGE_UNCHANGED); // always cleanup curl_easy_cleanup(curl); @@ -147,52 +155,60 @@ return 1; } - void findElement( TiXmlNode* pParent, std::string & picture) + void findElement( TiXmlNode* pParent, std::string & picture, std:: string tag) { - if ( !pParent ) return; + //if ( !pParent ) return; TiXmlNode* pChild; - //std::string picture ("Picture Not Found"); std::string pstring = pParent->Value(); - // std::cerr << "pstring: " << pstring << std::endl; - size_t found=pstring.find("picture_high"); + + //Search for tag + size_t found=pstring.find(tag); if (found!=std::string::npos) - { - ROS_INFO_STREAM("First child: " << pParent->FirstChild()->Value()); - picture = pParent->FirstChild()->Value(); - return; - } - size_t found2=pstring.find("picture_low"); - if (found2!=std::string::npos && picture == "") - { - ROS_INFO_STREAM("First child: " << pParent->FirstChild()->Value()); - picture = pParent->FirstChild()->Value(); - return; - } - for ( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) - { - findElement(pChild, picture); - // std::cerr << "in for " << std::endl; - } + { + ROS_INFO_STREAM("First child: " << pParent->FirstChild()->Value()); + picture = pParent->FirstChild()->Value(); + return; + } + + for( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) + { + findElement(pChild, picture,tag); + } } - int visBarcooPicture(std::string buffer) + int getPictureLink(std::string buffer,std::string & picture) { - // std::cerr << "buffer: " << buffer << std::endl; - TiXmlDocument doc; doc.Parse((const char*)buffer.c_str(), 0, TIXML_ENCODING_UTF8); - std::string picture; - findElement (&doc, picture); + + //Check if there is a result + std:: string result; + cout << "ohhh my goooooood\n\n"; + findElement(&doc,result,tag1_); + if(result.compare("0") == 0) //This condition checks if there is a result in the XML file if not returns 0 + { + cout << "heloooooo barcode does not correspond to any object in the barcoo database"; + return 0; + } + else + cout << "ELSE ELSE ELSE\n"; + + //Search for first tag + findElement (&doc, picture,tag2_); + + if(picture == "") + findElement (&doc, picture,tag3_); //Search for second tag + ROS_INFO_STREAM ("Picture link: " << picture); if (picture == "") return -1; - getImage (picture); + return 1; } - int callBarcoo(std::string bar_code, std::string & buffer) + int getBarcooXML(std::string bar_code, std::string & buffer) { char errorBuffer[CURL_ERROR_SIZE]; // Our curl objects @@ -200,7 +216,7 @@ CURLcode result; // Create our curl handle curl = curl_easy_init(); - std::string full_url = "http://www.barcoo.com/api/get_product_complete?pi=" + bar_code + "&pins=ean&format=xml&source=ias-tum"; + std::string full_url = link1_ + bar_code + link2_; ROS_INFO_STREAM("full_url: " << full_url); if (curl) @@ -236,6 +252,45 @@ return -1; } + int getHTMLpage(std::string url, std::string & buffer) + { + char errorBuffer[CURL_ERROR_SIZE]; + // Our curl objects + CURL *curl; + CURLcode result; + // Create our curl handle + curl = curl_easy_init(); + if (curl) + { + // Now set up all of the curl options + curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer); + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); + curl_easy_setopt(curl, CURLOPT_HEADER, 0); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &BarcodeReaderNode::writer); + // curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); + + // Attempt to retrieve the remote page + result = curl_easy_perform(curl); + + // Always cleanup + curl_easy_cleanup(curl); + + // Did we succeed? + if (result == CURLE_OK) + { + + return 1; + } + else + { + return -1; + } + } + return -1; + } + void imageCallback(const sensor_msgs::ImageConstPtr& msg_ptr) { @@ -288,26 +343,70 @@ barcode_pub_.publish(msg); } if (n == 0) - { - ROS_WARN("Barcode not found"); - return; - } + { + ROS_WARN("Barcode not found"); + return; + } if (n < 0) - { - ROS_ERROR("Error occured while finding barcode"); - return; - } + { + ROS_ERROR("Error occured while finding barcode"); + return; + } std::string buffer; - if (callBarcoo(ss.str(), buffer) == 1) - { - // std::cerr << "buffer after callBarcoo " << buffer << std::endl; - visBarcooPicture (buffer); - } + //Get xml file from barcoo database + int res = getBarcooXML(ss.str(), buffer); + if(res != 1) + return; + // std::cerr << "buffer after callBarcoo " << buffer << std::endl; + //Search for info in the xml file + std::string pictureLink; + res = getPictureLink (buffer,pictureLink); + + + if(res == 0) //If barcode does not exist in the database return + { + cout << "Barcode does not exist in the barcoo datbase\n"; + return; + } + if(res == -1) //This condition is true when the image link is not given + { + //Look for an image in the HTML file + + //First find the link to the barcoo website of the file + std::string htmlPage; + std::string htmlLink; + findElement(&doc,htmlLink,"back_link"); + cout << "html link: "+htmlLink+"\n"; + //Download HTML page + getHTMLpage(htmlLink,htmlPage); + std::string ss = pattern_; + long position = htmlPage.find(ss, ss.length()) + ss.length(); + + std::string link; + while(htmlPage.at(position) !='"') + { + link += htmlPage.at(position); + position++; + } + pictureLink = link; + + } + //Get Image + if(!pictureLink.empty()) + { + cv::Mat imgTmp; // image object + getImage (pictureLink ,& imgTmp); + // display image + if (!(imgTmp.empty())) + { + imshow("Barcoo img", imgTmp); + } + } + cv::waitKey(3); // clean up image.set_data(NULL, 0); - } protected: This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |