How can I tokenize CSV file with TokenizeBoost library? - machine-learning

I have a problem with converting any string from CSV into string (but not string of char) and then tokenize it.
There is my code here:
#include <iostream>
#include <math.h>
#include "NumCpp.hpp"
#include <cstdlib>
#include <python3.10/Python.h>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <boost/tokenizer.hpp>
using namespace std;
using namespace boost;
typedef tokenizer< escaped_list_separator<char> > Tokenizer;
//Take this advice from one site
int main()
{
string data("DATA.csv");
ifstream in(data.c_str());
while (getline(in, line))
{
Tokenizer tok(line);
for (tokenizer<>::iterator beg = tok.begin(); beg != tok.end(); ++beg) {
cout << *beg << "\n";
}
}
return 0;
}
It's just copy strings from CSV file one by one.
I don't know how to control the tokenize symbol of this function. In official documentation I had only found a little piece of code, which works only with your string variable..
#include<iostream>
#include<boost/tokenizer.hpp>
#include<string>
int main() {
using namespace std;
using namespace boost;
string s = "This is, a test";
tokenizer<> tok(s);
for (tokenizer<>::iterator beg = tok.begin(); beg != tok.end(); ++beg) {
cout << *beg << "\n";
}
}
The output from simple_example_1 is: Live
This
is
a
test
I accepting advice from you about different arguments of tokenizer, and how I can solve my tokenize reading from csv.

First off, don't (ever) do this:
using namespace std;
using namespace boost;
It leads to at least a dozen name conflicts. In general, avoid using namespace.
The Question
You're using Tokenizer as:
using Tokenizer = boost::tokenizer<boost::escaped_list_separator<char>>;
This means it uses escaped_list_separator as the separator. You can use other than the default constructors to pass initializers to it:
How can i add a delimeter to boost::escaped_list_separator?
E.g.
Tokenizer tok(line, {"\\", ",", "\""});
Full sample: Live
#include <iostream>
#include <fstream>
#include <boost/tokenizer.hpp>
using Sep = boost::escaped_list_separator<char>;
using Tokenizer = boost::tokenizer<Sep>;
int main() {
std::ifstream in("DATA.csv");
std::string line;
while (getline(in, line)) {
Tokenizer tok(line, {"\\", ",", "\""});
for (auto beg = tok.begin(); beg != tok.end(); ++beg)
std::cout << *beg << "\n";
}
}
If you don't want/need the escaping logic, use another separator class, e.g. https://www.boost.org/doc/libs/1_63_0/libs/tokenizer/char_separator.htm
Further Reading
Sometimes tokenizing is just not enough. Consider writing a parser:
Prevent escaped_list_separator from consuming quotes in quoted token
How to make my split work only on one real line and be capable to skip quoted parts of string?
or any of the other Spirit based CSV parsers I have made/explained in the past: https://stackoverflow.com/search?tab=votes&q=user%3a85371%20spirit%20csv

Related

How to track object using dlib(c++) from video stream passed from darknet(c)?

I am using darknet to detect objects from live video stream and want to pass each frame to dlib for tracking that object but i'm confused that how i pass frames from darknet's demo.c to dlib and do the tracking.
Do i need to use c c++ connector ? if yes, how? any explanation or clues would be helpful.
thanks...
Dlib file where i want to pass ipl image and want to track the object.
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/dir_nav.h>
#include "dlib/opencv.h"
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace dlib;
using namespace std;
extern "C"{
int enx = 203, int eny = 190, int enw = 98, int enh = 86;
void track(IplImage * ipl, int enx, int eny, int enw, int enh)
{
Mat frame = cvarrToMat(ipl);
image_window win;
correlation_tracker tracker;
array2d<rgb_pixel> img;
std::cout << "Starting" << std::endl;
assign_image(img, cv_image<bgr_pixel>(frame));
tracker.start_track(img, centered_rect(point(enx, eny), enw, enh));
win.set_image(img);
win.clear_overlay();
win.add_overlay(tracker.get_position());
while(ipl) {
Mat frame = cvarrToMat(ipl);
assign_image(img, cv_image<bgr_pixel>(frame));
tracker.update(img);
win.set_image(img);
win.clear_overlay();
win.add_overlay(tracker.get_position());
}
}
}
I would recommend you to look at https://github.com/AlexeyAB/darknet/blob/master/src/yolo_console_dll.cpp
where you will find many ideas about darknet data manipulation. Including tracker

Usage of FunctionPass over ModulePass when creating LLVM passes

I've seen quite a numerous amount of examples that go over creating functions passes (e.g. Brandon Holt and Adrian Sampson), but I am curious as to the difficulty in creating a module pass to do these very similar problems. I've tried to implement a module pass to display the global variable names using this example and llvm source code to understand how you have to iterate through members.
I am using a source compiled version of LLVM, and using the example from the above links to add the pass, and then running:
$ clang -Xclang -load -Xclang build/Skeleton/libSkeletonPass.so something.c
Which then returns this gibberish. However, if I implement a functionPass and just use Auto to determine the type to be initialized it's very straight forward and works. Am I just going about printing the global variables the wrong way?
This is a pastebin of the error output from the terminal. link
Skeleton.cpp
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/IR/LLVMContext.h"
using namespace llvm;
namespace {
// Helper method for converting the name of a LLVM type to a string
static std::string LLVMTypeAsString(const Type *T) {
std::string TypeName;
raw_string_ostream N(TypeName);
T->print(N);
return N.str();
}
struct SkeletonPass : public ModulePass {
static char ID;
SkeletonPass() : ModulePass(ID) {}
virtual bool runOnModule(Module &M) {
for (Module::const_global_iterator GI = M.global_begin(),
GE = M.global_end(); GI != GE; ++GI) {
errs() << "Found global named: " << GI->getName()
<< "\tType: " << LLVMTypeAsString(GI->getType()) << "!\n";
}
return false;
}
};
}
char SkeletonPass::ID = 0;
// Automatically enable the pass.
// http://adriansampson.net/blog/clangpass.html
static void registerSkeletonPass(const PassManagerBuilder &,
legacy::PassManagerBase &PM) {
PM.add(new SkeletonPass());
}
static RegisterStandardPasses
RegisterMyPass(PassManagerBuilder::EP_EarlyAsPossible,
registerSkeletonPass);
something.c
int value0 = 5;
int main(int argc, char const *argv[])
{
int value = 4;
value += 1;
return 0;
}
I was able to figure this out after some extensive github searching. Here is the answer from which I was following a tutorial to help others who may be curious how to implement a Module Pass.

Read a sequence of frames using OpenCV/C++

I want to read a sequence of frames from any folder using openCV. All frames are in sequence i.e. (1).jpg,(2).jpg,....
I tried
VideoCapture cap;
cap.open("Directory/");
for(;;)
{
Mat frame;
cap >> frame;
}
but it doesn't work.
This question has been asked before but i don't know why this answer doesn't work for me.
OpenCV: Reading image series from a folder
do i need to rename the images?.
cap open should be cap.open("Directory/(%02d).jpg"); and you have to rename your images so that they look like (01).jpg,(02).jpg etc so that they have fixed length. if the images are like (001).jpg then you should use `cap.open("Directory/(%03d).jpg");
edit
#include "opencv2/opencv.hpp"
using namespace cv;
int main()
{
VideoCapture cap;
cap.open("imgs/(%02d).jpg");
int i=0;
for(;;)
{
if(i++%37==0)cap=VideoCapture("imgs/(%02d).jpg");//there are 37 frames in the dir
Mat frame;
cap >> frame;
imshow("frame",frame);
if(waitKey(1)==27)
exit(0);
}
return 0;
}
Try to prepare an xml/yaml file with list of names with path to the images, in the desired order. Then load the list as a vector or some resembling structure, and then open them one by one in a loop.
Here the full code to do read a sequence of frames with five zeros in name "frame00000.jpg, frame00001.jpg,.....,frame00010.jpg...) using string concatination idea just like matlab.
#include "stdafx.h"
#include <stdlib.h>
#include <math.h>
#include <opencv/cv.h> // include it to used Main OpenCV functions.
#include <opencv/highgui.h> //include it to use GUI functions.
using namespace std;
using namespace cv;
string intToStr(int i,string path){
string bla = "00000";
stringstream ss;
ss<<i;
string ret ="";
ss>>ret;
string name = bla.substr(0,bla.size()-ret.size());
name = path+name+ret+".jpg";
return name;
}
int main(int, char**)
{
string previous_window = "Previous frame";
string current_window = "Current frame ";
int i=0;
for(int i = 1 ; i< 10 ; i++)
{
Mat Current, Previous;
string Curr_name = intToStr(i,"D:/NU/Junior Scientist/Datasets/egtest04/frame");
string Prev_name = intToStr(i-1,"D:/NU/Junior Scientist/Datasets/egtest04/frame");
Current = imread(Curr_name,1);
Previous = imread(Prev_name,1);
namedWindow(current_window,WINDOW_AUTOSIZE);
namedWindow(current_window,WINDOW_AUTOSIZE);
imshow(current_window,Current);
imshow(previous_window,Previous);
waitKey(0);
}
}
Where "D:/NU/Junior Scientist/Datasets/egtest04/frame" is the path sting.

OpenCV 2.4.2 imshow causes crash

I have this nasty problem with opencv 2.4.2.
I use VS 2012 to compile this short test programm.
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
using namespace cv;
int main()
{
Mat sudoku = imread("sudoku.jpg",0);
namedWindow("Lines", CV_WINDOW_AUTOSIZE);
imshow("Lines", sudoku);
}
Imshow is the problem. When I remove it, it runs without any problem. I found a tip here which said to use debug libs instead but it didn't help.
First of all, you have to check if image is loaded correctly. To do this just check if image.data is NULL or not.
Secondly, after calling imshow you have to call waitKey to show image:
http://opencv.willowgarage.com/documentation/cpp/user_interface.html#cv-waitkey
Here's the whole code:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
Mat sudoku = imread("sudoku.jpg",0);
if (sudoku.data == NULL)
{
cout << "No image found! Check path." << endl;
return 1;//ERROR
}
else
{
namedWindow("Lines", CV_WINDOW_AUTOSIZE);
imshow("Lines", sudoku);
waitKey();//without this image won't be shown
return 0;//OK
}
}

Lua does not load libs

I decided to add scripting with Lua. I've downloaded and compiled interpreter. It works fine, but when I want to use any functions from os.* or string.* libs, it says, that "attemt to index global 'os' (a nil value)"
Here is my code and should work, but it does not:
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
extern "C" {
#include "..\liblua\lua.h"
#include "..\liblua\lualib.h"
#include "..\liblua\lauxlib.h"
}
int main(int argc, TCHAR* argv[])
{
lua_State *LuaVM = luaL_newstate();
lua_pushcfunction(LuaVM,luaopen_base);
lua_call(LuaVM,0,0);
lua_pushcfunction(LuaVM,luaopen_math);
lua_call(LuaVM,0,0);
lua_pushcfunction(LuaVM,luaopen_string);
lua_call(LuaVM,0,0);
lua_pushcfunction(LuaVM,luaopen_table);
lua_call(LuaVM,0,0);
int error;
lua_pushstring(LuaVM,"Ver 0.525.5");
lua_setglobal(LuaVM,"Version");
while (true)
{
string strCode;
getline(cin,strCode);
error = luaL_loadbuffer(LuaVM,strCode.c_str(),strCode.length(),"") ||
lua_pcall(LuaVM,0,0,0);
if (error)
{
cout<< lua_tostring(LuaVM,-1)<<endl;
lua_pop(LuaVM,1);
}
}
lua_close(LuaVM);
return 0;
}
What's wrong with it?
In Lua 5.2 the standard luaopen_* functions do not set the corresponding global variables.
Why not copy and adapt the code in linit.c or just call luaL_openlibs?
Otherwise, do what they do: call luaL_requiref for each luaopen_* function.
See http://www.lua.org/source/5.2/linit.c.html#luaL_openlibs.

Resources