void ImageSequence::loadMetadata(string host) { // Curl out the metadata for this Id // Invoke the parser // Grab the metadata fields // URL: host/imageseq/id/describe char url[host.length() + 17 + 10]; sprintf(url, "http://%s/imageseq/%u", host.c_str(), id); const char* metadataJSON = curlGet(url).c_str(); // Parse metadata Document document; if(document.Parse<0>(metadataJSON).HasParseError()) { // Failed to parse fprintf(stderr, "Error Parsing metadata JSON"); return; } if(document.IsObject() && document.HasMember("delta") && document.HasMember("count") && document.HasMember("images") && document.HasMember("description")) { // We have all the necessary fields length = document["count"].GetUint(); delta = document["delta"].GetInt() / 1000.0; if(!document["description"].IsNull()) { description = document["description"].GetString(); } else { description = ""; } // Pull out the image list if(document["images"].IsArray()) { for(uint32_t i=0; i<length; i++) { // Take the ID and make an object LabeledImage *newImage = new LabeledImage(document["images"][i].GetUint()); // Take the object and load its metadata newImage->load(host); images.push_back(newImage); } } } else { fprintf(stderr, "Image metadata is missing params"); } }
void LabeledImage::loadImage(string host) { // URL: host/image/id if(image == NULL) { char url[host.length() + 14 + 10]; sprintf(url, "http://%s/image/%u", host.c_str(), id); string curlBuffer = curlGet(url); // Make an imageheader CvMat* matbuf = cvCreateMat(1,curlBuffer.size(),CV_8UC1); memcpy(matbuf->data.ptr, curlBuffer.data(), curlBuffer.size()); // Assign this to an image header image = cvDecodeImage(matbuf); } }
void Roi::loadTags(string host) { char url[host.length() + 17 + 10]; sprintf(url, "http://%s/roi/%u/tags", host.c_str(), id); const char* tagJSON = curlGet(url).c_str(); // Parse metadata Document document; if(document.Parse<0>(tagJSON).HasParseError() || !document.IsArray()) { // Failed to parse fprintf(stderr, "Error Parsing metadata JSON"); return; } for(SizeType i = 0; i < document.Size(); i++) { tags.push_back(document[i].GetUint()); } }
// Helpers void LabeledImage::loadMetadata(string host) { // URL: host/image/id/describe char url[host.length() + 23 + 10]; sprintf(url, "http://%s/image/%u/describe", host.c_str(), id); const char* metadataJSON = curlGet(url).c_str(); // Parse metadata Document document; if(document.Parse<0>(metadataJSON).HasParseError()) { // Failed to parse fprintf(stderr, "Error Parsing metadata JSON"); return; } if(document.IsObject() && document.HasMember("filename") && document.HasMember("height") && document.HasMember("width") && document.HasMember("description")) { // We have all the necessary fields filename = document["filename"].GetString(); height = document["height"].GetUint(); width = document["width"].GetUint(); if(!document["description"].IsNull()) { description = document["description"].GetString(); } else { description = ""; } } else { fprintf(stderr, "Image metadata is missing params"); } }
void LabeledImage::loadRois(string host) { // URL: host/image/id/rois rois.clear(); char url[host.length() + 19 + 10]; sprintf(url, "http://%s/image/%u/rois", host.c_str(), id); const char* roiJSON = curlGet(url).c_str(); // Parse ROIs Document document; if(document.Parse<0>(roiJSON).HasParseError() || !document.IsArray()) { fprintf(stderr, "Error Parsing ROI JSON"); return; } for(uint32_t i = 0; i < document.Size(); i++) { // Build an ROI Roi *roi = new Roi(); roi->loadFromDocument(document[i]); rois.push_back(roi); } }
/* NOTICE: Remember to call the program by passing the URL as first (and unique) parameter! */ int main(int argc, char const *argv[]) { FILE *keyfile = NULL; int result = 0; long http_reply = 0; CURLcode curl_exit; char *get_result = NULL, *encoded_url = NULL; char apikey[KSIZE], request_url[BSIZE], request_reply[BSIZE]; // Checking arguments. if (argc < 2) { fprintf(stderr, "Error: please supply a valid URL.\n"); return EXIT_FAILURE; } // Checking if the key is already stored in file "categorization.key". memset(apikey, 0, sizeof(apikey)); keyfile = fopen(KEY_FILENAME, "r"); if (keyfile) { // File already exists. Reading the key from the file. if (!fgets(apikey, sizeof(apikey), keyfile)) { fprintf(stderr, "Something went wrong while reading from file.\n"); return EXIT_FAILURE; } fclose(keyfile); } else { /* File does not exist. We create the file and ask for the key to be stored in it. */ fprintf(stdout, "Please insert your categorization key below.\n"); fscanf(stdin, "%s", apikey); keyfile = fopen(KEY_FILENAME, "w+"); if (keyfile) { result = fputs(apikey, keyfile); fclose(keyfile); if (result < 0) { fprintf(stderr, "Something went wrong while writing to file.\n"); return EXIT_FAILURE; } } else { fprintf(stderr, "Something went wrong while creating the file.\n"); return EXIT_FAILURE; } } // Creating request URL. if (!(encoded_url = urlEncode((char *) argv[1]))) { fprintf(stderr, "Something went wrong while encoding the URL.\n"); return EXIT_FAILURE; } snprintf(request_url, sizeof(request_url), "%s?client=%s&apikey=%s&appver=%s&pver=%s&url=%s", CATEGORIZATION_URL, CLIENT, apikey, APPVER, PVER, encoded_url); free(encoded_url); // Performing request and checking result. get_result = curlGet(request_url, &http_reply, &curl_exit); if (!get_result) { // curlGet function failed. fprintf(stderr, "Error: curl request failed.\n"); return EXIT_FAILURE; } if (curl_exit != CURLE_OK) { // curlGet didn't fail, but returned a failure code. fprintf(stderr, "Error: curl request failed with code %s.\n", curl_easy_strerror(curl_exit)); free(get_result); return EXIT_FAILURE; } // Copying result and cleaning up memory. memcpy(request_reply, get_result, sizeof(request_reply)); free(get_result); // Checking if request reply is empty. if (request_reply[0] == '\0') { snprintf(request_reply, sizeof(request_reply), REPLY_SAFE); } // Checking reply code. if (!http_reply) { fprintf(stderr, "Something went wrong while performing your request.\n"); return EXIT_FAILURE; } else { fprintf(stdout, "GET request performed correctly with URL: %s\n\n", request_url); switch (http_reply) { case 200: { fprintf(stdout, "%s\n\n%s %s %s %s.\n\n", "Your code is: 200 OK.", "The website", argv[1], "seems to be", request_reply); }; break; case 204: { fprintf(stdout, "%s\n\n%s %s %s %s.\n\n", "Your code is: 204 NO CONTENT.", "The website", argv[1], "seems to be", request_reply); }; break; case 400: { fprintf(stderr, "%s %s\n", "Your code is: 400 BAD REQUEST.", "(Please check the syntax of your URL!)"); }; break; default: { fprintf(stdout, "Your code is: %ld\n", http_reply); }; break; } } return EXIT_SUCCESS; }