-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlickr.cpp
154 lines (127 loc) · 4.52 KB
/
Flickr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2017 Annotator Team
#define Annotator_AnnotatorLib_Flickr_BODY
/************************************************************
ImageFolder class body
************************************************************/
// include associated header file
#include "Flickr.h"
#include <set>
#include <sstream>
#include <Poco/Net/DNS.h>
#include <Poco/URI.h>
#include <opencv2/opencv.hpp>
#include <Poco/JSON/Handler.h>
#include <Poco/JSON/JSON.h>
#include <Poco/JSON/Object.h>
#include <Poco/JSON/Parser.h>
#include <Poco/JSON/Stringifier.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/HTTPSClientSession.h>
#include <Poco/Net/SocketAddress.h>
#include <Poco/Net/SocketStream.h>
#include <Poco/Net/StreamSocket.h>
#include <Poco/StreamCopier.h>
// Derived includes directives
namespace AnnotatorLib {
namespace ImageSet {
Flickr::Flickr(std::string path) {
std::string prefix = "https://";
if (!std::equal(prefix.begin(), prefix.end(), path.begin())) {
this->path =
"https://api.flickr.com/services/rest/"
"?method=flickr.photos.search&api_key=3f2473a4aa48d3a7376136d347fab3db&"
"per_page=100&format=json&nojsoncallback=1&text=" +
path;
} else {
this->path = path;
}
initFlickr();
}
ImageSetType Flickr::getType() { return ImageSetType::UNKNOWN; }
Image Flickr::getImage(unsigned long position) {
Image img;
try {
if (position < urls.size()) img = urlToImage(urls[position]);
} catch (std::exception &e) {
}
return img;
}
std::string Flickr::getImagePath(unsigned long) { return ""; }
bool Flickr::gotoPosition(unsigned long position) {
if (urls.size() <= position) return false;
this->position = position;
return true;
}
long Flickr::getPosition() { return position; }
bool Flickr::hasNext() { return position + 1 < urls.size(); }
Image Flickr::next() {
Image img;
if (hasNext()) {
img = getImage(position);
position++;
}
return img;
}
unsigned int Flickr::size() { return this->urls.size(); }
unsigned int Flickr::getFPS() { return 10; }
std::string Flickr::getPath() { return path; }
bool Flickr::equals(std::shared_ptr<AbstractImageSet> other) {
if (this == other.get()) return true;
if (other->getType() != ImageSetType::CAMERA) return false;
if (this->getPath() != other->getPath()) return false;
return true;
}
void Flickr::initFlickr() { search(); }
void Flickr::search() {
Poco::URI uri(path);
Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort());
Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_GET,
uri.getPathAndQuery());
session.sendRequest(req);
Poco::Net::HTTPResponse res;
std::istream &iStr = session.receiveResponse(res);
std::string jsonStr;
Poco::StreamCopier::copyToString(iStr, jsonStr);
parseSearchResponse(jsonStr);
}
void Flickr::parseSearchResponse(std::string jsonStr) {
Poco::JSON::Parser parser;
parser.parse(jsonStr);
Poco::Dynamic::Var result = parser.result();
Poco::JSON::Object::Ptr json = result.extract<Poco::JSON::Object::Ptr>();
Poco::JSON::Object::Ptr photos = json->getObject("photos");
Poco::JSON::Array::Ptr array = photos->getArray("photo");
for (std::size_t i = 0; i < array->size(); ++i) {
Poco::JSON::Object::Ptr value = array->getObject(i);
int farm = value->get("farm").extract<int>();
std::string server = value->get("server").extract<std::string>();
std::string id = value->get("id").extract<std::string>();
std::string secret = value->get("secret").extract<std::string>();
std::stringstream imageUrl;
imageUrl << "http://farm" << farm << ".staticflickr.com/" << server << "/"
<< id << "_" << secret << "_n.jpg";
this->urls.push_back(imageUrl.str());
}
}
Image Flickr::urlToImage(std::string url) {
Poco::URI uri(url);
Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort());
Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_GET,
uri.getPathAndQuery());
session.sendRequest(req);
Poco::Net::HTTPResponse res;
std::istream &iStr = session.receiveResponse(res);
std::string imageStr;
Poco::StreamCopier::copyToString(iStr, imageStr);
std::vector<uchar> vec(imageStr.begin(), imageStr.end());
Image img;
img = cv::imdecode(vec, cv::IMREAD_ANYCOLOR);
return img;
}
// static attributes (if any)
} // of namespace ImageSet
} // of namespace AnnotatorLib
/************************************************************
End of ImageFolder class body
************************************************************/