Skip to content

Instantly share code, notes, and snippets.

@ivder
Created February 19, 2019 03:53
Show Gist options
  • Save ivder/41aaa4a387827bd28c3bd1c3d57d425c to your computer and use it in GitHub Desktop.
Save ivder/41aaa4a387827bd28c3bd1c3d57d425c to your computer and use it in GitHub Desktop.
Extract GPS information from CSV file (rendered from GoPro). Match the video frame and gps coords, so in 1 second of video has 1 assigned coordinate. Play the video
int main(int argc, char** argv) {
int frameidx, frametotal,fs;
int count = 0;
int idx = 0;
int idxrecord = 0;
int framecount = 1;
int gpsskip = 5;
int divrange = 0;
double firstsec, tempsec = 0;
cout << fixed;
ifstream file_reader("GOPR0632.csv");
file_reader.ignore(341);
vector<vector<string>> record;
vector<int> diff;
vector<int> frameskip;
//extract csv file
string line;
while (getline(file_reader,line)) {
stringstream ss(line);
string token;
vector<string> row;
while (getline(ss, token, ',')) {
row.push_back(token);
}
record.push_back(row);
}
//get total of gps coordinates in 1 sec
firstsec = atof(record[0][0].c_str());
for (int i = 0; i < record.size(); i++) {
tempsec=atof(record[i][0].c_str());
if (tempsec - firstsec < 1) {
count += 1;
}
else {
diff.push_back(count);
count = 1;
firstsec = atof(record[i][0].c_str());
}
}
///print total of gps coordinates in 1 sec and total of frameskip per sec
for (int i = 0; i < diff.size(); i++) {
fs = int(((double(1) / diff[i])*gpsskip) / (double(1) / 60));
divrange = int(double(60) / fs);
for (int j = 0; j < divrange; j++) {
frameskip.push_back(fs);
}
//cout << "range per sec = " << diff[i];
//cout << " total frameskip = "<<fs << endl;
}
VideoCapture cap("GOPR0632.MP4");
frametotal = cap.get(CV_CAP_PROP_FRAME_COUNT);
if (!cap.isOpened()) { return -1; }
while (1) {
Mat frame;
cap >> frame;
if (frame.empty())
break;
imshow("Frame", frame);
char c = (char)waitKey(30);
frameidx=cap.get(CV_CAP_PROP_POS_FRAMES);
if (framecount <= frameskip[idx]) {
cout << "frame = " << frameidx << " coordinates = ";
for (int x = 0; x < record[idxrecord].size(); x++) {
cout <<record[idxrecord][x] << " ";
}
cout << endl;
framecount = framecount + 1;
}
else if(framecount > frameskip[idx]) {
idxrecord = idxrecord + gpsskip;
framecount = 1;
cout << "frame = " << frameidx << " coordinates = ";
for (int x = 0; x < record[idxrecord].size(); x++) {
cout << record[idxrecord][x] << " ";
}
cout << endl;
framecount = framecount + 1;
idx = idx + 1;
}
if (c == 27)
break;
}
cap.release();
destroyAllWindows();
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment