Skip to content

Commit

Permalink
compiles, start pipeline refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rrhan0 committed Nov 2, 2024
1 parent 55adc03 commit 9d9bac4
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 95 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ set(CMAKE_CXX_STANDARD 20)

add_executable(${PROJECT_NAME}
${CMAKE_SOURCE_DIR}/src/main.cpp
${CMAKE_SOURCE_DIR}/src/CameraController.cpp
${CMAKE_SOURCE_DIR}/src/ArenaCamera.cpp
${CMAKE_SOURCE_DIR}/src/HttpTransmitter.cpp
${CMAKE_SOURCE_DIR}/src/CprHTTP.cpp)
# ${CMAKE_SOURCE_DIR}/src/CprHTTP.cpp
${CMAKE_SOURCE_DIR}/src/Pipeline.cpp)


add_executable(curltest
Expand Down
78 changes: 24 additions & 54 deletions src/HttpTransmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

#include <opencv2/opencv.hpp>

#include "Pipeline.hpp"


#define MAX_ATTEMPTS 100

// Callback function to handle the response
Expand Down Expand Up @@ -140,65 +143,38 @@ bool HttpTransmitter::send_imgfile(const std::string &url,
}

bool HttpTransmitter::send_imen(const std::string &url,
std::unique_ptr<std::vector<uchar>> buf_ptr,
int64_t timestamp) {
std::unique_ptr<EncodedData> encoded) {
CURLcode res;
char error[CURL_ERROR_SIZE];
curl_mime *form = NULL;
curl_mimepart *field = NULL;
std::string timestamp_str = std::to_string(timestamp) + ".jpg";
std::string timestamp_str = std::to_string(encoded->timestamp) + ".jpg";

curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);

int attempts = 0;
while ((form = curl_mime_init(curl)) == NULL && attempts < MAX_ATTEMPTS) {
fprintf(stderr,
"curl_mime_init() failed, attempt %d of %d: %s\n",
attempts + 1,
MAX_ATTEMPTS,
error);
attempts++;
}
form = curl_mime_init(curl);

if (form == NULL) {
fprintf(stderr, "Maximum attempts exceeded, skipping\n");
fprintf(stderr, "curl_mime_init() failed\n");
return false;
}
curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);

attempts = 0;
while ((field = curl_mime_addpart(form)) == NULL && attempts < MAX_ATTEMPTS) {
fprintf(stderr,
"curl_mime_addpart() failed, attempt %d of %d: %s\n",
attempts + 1,
MAX_ATTEMPTS,
error);
attempts++;
field = curl_mime_addpart(form);
if (field == NULL) {
fprintf(stderr, "curl_mime_addpart() failed\n");
return false;
}

attempts = 0;
while ((res = curl_mime_name(field, "image")) != CURLE_OK &&
attempts < MAX_ATTEMPTS) {
fprintf(stderr,
"curl_mime_name() failed, attempt %d of %d: %s\n",
attempts + 1,
MAX_ATTEMPTS,
error);
attempts++;
res = curl_mime_name(field, "image");
if (res != CURLE_OK) {
fprintf(stderr, "curl_mime_name() failed\n");
return false;
}
// std::cout << (*buf_ptr).size() << "\n";
attempts = 0;
while ((res = curl_mime_data(field,
reinterpret_cast<const char *>(buf_ptr->data()),
(*buf_ptr).size())) != CURLE_OK &&
attempts < MAX_ATTEMPTS) {
fprintf(stderr,
"curl_mime_filedata() failed, attempt %d of %d: %s\n",
attempts + 1,
MAX_ATTEMPTS,
error);
attempts++;
res = curl_mime_data(field,reinterpret_cast<const char *>(encoded->buf.data()), encoded->buf.size());
if (res != CURLE_OK) {
fprintf(stderr, "curl_mime_data() failed\n");
return false;
}

curl_mime_filename(field, timestamp_str.c_str());
Expand All @@ -214,22 +190,16 @@ bool HttpTransmitter::send_imen(const std::string &url,
// timestamp_str = std::to_string(timestamp); curl_mime_data(field,
// timestamp_str.c_str(), CURL_ZERO_TERMINATED);

attempts = 0;
while ((res = curl_easy_perform(curl)) != CURLE_OK &&
attempts < MAX_ATTEMPTS) {
fprintf(stderr,
"curl_easy_perform() failed, attempt %d of %d: %s\n",
attempts + 1,
MAX_ATTEMPTS,
error);
attempts++;
std::this_thread::sleep_for(std::chrono::seconds(1));
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_mime_data() failed\n");
return false;
}

/* then cleanup the form */
curl_mime_free(form);

std::cout << "Sent " << timestamp << " to " << url << '\n';
std::cout << "Sent " << encoded->timestamp << " to " << url << '\n';

return true;
}
5 changes: 3 additions & 2 deletions src/HttpTransmitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <vector>

#include <opencv2/opencv.hpp>
#include "Pipeline.hpp"


class HttpTransmitter {
public:
Expand All @@ -20,8 +22,7 @@ class HttpTransmitter {
const std::string& file_path,
int64_t timestamp);
bool send_imen(const std::string& url,
std::unique_ptr<std::vector<uchar>> buf_ptr,
int64_t timestamp);
std::unique_ptr<EncodedData> encoded);

private:
CURL* curl;
Expand Down
Empty file added src/Pipeline.cpp
Empty file.
19 changes: 19 additions & 0 deletions src/Pipeline.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <string>
#include <vector>

#include <opencv2/opencv.hpp>

#ifndef PIPELINE_HPP
#define PIPELINE_HPP

struct ImagePath {
std::string path;
int64_t timestamp;
};

struct EncodedData {
std::vector<uchar> buf;
int64_t timestamp;
};

#endif // PIPELINE_HPP
54 changes: 17 additions & 37 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,11 @@
#include "CprHTTP.hpp"
#include "HttpTransmitter.hpp"
#include "TSQueue.hpp"

struct ImagePath {
std::string path;
int64_t timestamp;
};

struct EncodedData {
std::unique_ptr<std::vector<uchar>> buf_ptr;
int64_t timestamp;
};
#include "Pipeline.hpp"

static TSQueue<std::unique_ptr<ImageData>> data_queue;
static TSQueue<ImagePath> path_queue;
static TSQueue<EncodedData> encoded_queue;
static TSQueue<std::unique_ptr<EncodedData>> encoded_queue;

std::atomic<bool> stop_flag(false);

Expand Down Expand Up @@ -92,64 +83,53 @@ void image_processor() {
std::string extension = ".jpg";

while (!stop_flag) {
ImageData element;
std::unique_ptr<ImageData> element;
try {
// std::cout << id << " getting image\n";
element = data_queue.pop();
// std::cout << id << " got image\n";
} catch (const AbortedPopException& e) {
break;
}
Arena::IImage* pImage = element.pImage;
int64_t timestamp = element.timestamp;

cv::Mat mSource = element->image;
// cv::Mat img = cv::Mat((int)pImage->GetHeight(), (int)pImage->GetWidth(),
// CV_8UC1, const_cast<uint8_t*>(pImage->GetData()));
cv::Mat mSource_Bayer(static_cast<int>(pImage->GetHeight()),
static_cast<int>(pImage->GetWidth()),
CV_8UC1,
const_cast<uint8_t*>(pImage->GetData()));
cv::Mat mSource_Bgr(static_cast<int>(pImage->GetHeight()),
static_cast<int>(pImage->GetWidth()),
CV_8UC3);
// cv::Mat mSource_Bayer(static_cast<int>(pImage->GetHeight()),
// static_cast<int>(pImage->GetWidth()),
// CV_8UC1,
// const_cast<uint8_t*>(pImage->GetData()));
// cv::Mat mSource_Bgr(static_cast<int>(pImage->GetHeight()),
// static_cast<int>(pImage->GetWidth()),
// CV_8UC3);
// cvtColor(mSource_Bayer, mSource_Bgr, cv::COLOR_BayerRG2BGR);//Perform
// demosaicing process
std::unique_ptr<EncodedData> encoded = std::make_unique<EncodedData>();

std::vector<uchar> buf;
std::shared_ptr<std::vector<uchar>> buf_ptr =
std::make_shared<std::vector<uchar>>();

cv::imencode(".jpg", mSource_Bayer, *buf_ptr, compression_params);
Arena::ImageFactory::Destroy(pImage);
// works till here lol
cv::imencode(".jpg", mSource, encoded->buf, compression_params);

// encoded_queue.push({std::move(buf_ptr), timestamp});
std::cout << "Processed " << timestamp << "\n";
encoded_queue.push(std::move(encoded));
std::cout << "Processed " << element->timestamp << "\n";
}
}

void image_sender_imen(const std::string& url) {
// HttpTransmitter http_transmitter;
HttpTransmitter http_transmitter;
std::vector<int> compression_params;
compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
compression_params.push_back(100); // Change the quality value (0-100)

std::string extension = ".jpg";

while (!stop_flag) {
EncodedData element;
std::unique_ptr<EncodedData> element;
try {
element = encoded_queue.pop();
} catch (const AbortedPopException& e) {
break;
}

// Transfers ownership send_imen
http_transmitter.send_imen(
url, std::move(element.buf_ptr), element.timestamp);
// (void) std::async(std::launch::async, &HttpTransmitter::send_imen,
// &http_transmitter, url, &buf, timestamp);
http_transmitter.send_imen(url, std::move(element));
}
}

Expand Down

0 comments on commit 9d9bac4

Please sign in to comment.