Frames are now saved to the file, added FramesFileWriterReader class.

This commit is contained in:
JacekNaruniec
2017-01-14 18:48:27 +01:00
parent e068058868
commit e453d927d3
8 changed files with 202 additions and 14 deletions
+2
View File
@@ -13,6 +13,7 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="..\include\LiveScanClient\calibration.h" /> <ClInclude Include="..\include\LiveScanClient\calibration.h" />
<ClInclude Include="..\include\LiveScanClient\filter.h" /> <ClInclude Include="..\include\LiveScanClient\filter.h" />
<ClInclude Include="..\include\LiveScanClient\framesFileWriterReader.h" />
<ClInclude Include="..\include\LiveScanClient\iCapture.h" /> <ClInclude Include="..\include\LiveScanClient\iCapture.h" />
<ClInclude Include="..\include\LiveScanClient\imageRenderer.h" /> <ClInclude Include="..\include\LiveScanClient\imageRenderer.h" />
<ClInclude Include="..\include\LiveScanClient\iMarker.h" /> <ClInclude Include="..\include\LiveScanClient\iMarker.h" />
@@ -28,6 +29,7 @@
<ItemGroup> <ItemGroup>
<ClCompile Include="..\src\LiveScanClient\calibration.cpp" /> <ClCompile Include="..\src\LiveScanClient\calibration.cpp" />
<ClCompile Include="..\src\LiveScanClient\filter.cpp" /> <ClCompile Include="..\src\LiveScanClient\filter.cpp" />
<ClCompile Include="..\src\LiveScanClient\framesFileWriterReader.cpp" />
<ClCompile Include="..\src\LiveScanClient\iCapture.cpp" /> <ClCompile Include="..\src\LiveScanClient\iCapture.cpp" />
<ClCompile Include="..\src\LiveScanClient\imageRenderer.cpp" /> <ClCompile Include="..\src\LiveScanClient\imageRenderer.cpp" />
<ClCompile Include="..\src\LiveScanClient\iMarker.cpp" /> <ClCompile Include="..\src\LiveScanClient\iMarker.cpp" />
@@ -54,6 +54,9 @@
<ClInclude Include="..\include\LiveScanClient\utils.h"> <ClInclude Include="..\include\LiveScanClient\utils.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\include\LiveScanClient\framesFileWriterReader.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\src\socketCS.cpp"> <ClCompile Include="..\src\socketCS.cpp">
@@ -86,6 +89,9 @@
<ClCompile Include="..\src\LiveScanClient\utils.cpp"> <ClCompile Include="..\src\LiveScanClient\utils.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\src\LiveScanClient\framesFileWriterReader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Image Include="app.ico"> <Image Include="app.ico">
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,41 @@
#pragma once
#include <stdio.h>
#include <string>
#include <vector>
#include <chrono>
#include "utils.h"
class FramesFileWriterReader
{
public:
FramesFileWriterReader();
void openNewFileForWriting();
void openCurrentFileForReading();
// leave filename blank if you want the filename to be generated from the date
void setCurrentFilename(std::string filename = "");
void writeFrame(std::vector<Point3s> points, std::vector<RGB> colors);
bool readFrame(std::vector<Point3s> &outPoints, std::vector<RGB> &outColors);
bool openedForWriting() { return m_bFileOpenedForWriting; }
bool openedForReading() { return m_bFileOpenedForReading; }
void closeFileIfOpened();
~FramesFileWriterReader();
private:
void resetTimer();
int getRecordingTimeMilliseconds();
FILE *m_pFileHandle = nullptr;
bool m_bFileOpenedForWriting = false;
bool m_bFileOpenedForReading = false;
std::string m_sFilename = "";
std::chrono::steady_clock::time_point recording_start_time;
};
+6 -1
View File
@@ -20,6 +20,7 @@
#include "calibration.h" #include "calibration.h"
#include "utils.h" #include "utils.h"
#include "KinectCapture.h" #include "KinectCapture.h"
#include "framesFileWriterReader.h"
#include <thread> #include <thread>
#include <mutex> #include <mutex>
@@ -54,7 +55,11 @@ private:
bool m_bShowDepth; bool m_bShowDepth;
bool m_bFrameCompression; bool m_bFrameCompression;
int m_iCompressionLevel; int m_iCompressionLevel;
bool m_bCaptureToFile; // false if capture frames to memory
FramesFileWriterReader m_framesFileWriterReader;
SocketClient *m_pClientSocket; SocketClient *m_pClientSocket;
std::vector<float> m_vBounds; std::vector<float> m_vBounds;
@@ -0,0 +1,109 @@
#include "framesFileWriterReader.h"
#include <ctime>
FramesFileWriterReader::FramesFileWriterReader()
{
}
void FramesFileWriterReader::closeFileIfOpened()
{
if (m_pFileHandle == nullptr)
return;
fclose(m_pFileHandle);
m_pFileHandle = nullptr;
m_bFileOpenedForReading = false;
m_bFileOpenedForWriting = false;
}
void FramesFileWriterReader::resetTimer()
{
recording_start_time = std::chrono::steady_clock::now();
}
int FramesFileWriterReader::getRecordingTimeMilliseconds()
{
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
return static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds >(end - recording_start_time).count());
}
void FramesFileWriterReader::openCurrentFileForReading()
{
closeFileIfOpened();
m_pFileHandle = fopen(m_sFilename.c_str(), "rb");
m_bFileOpenedForReading = true;
m_bFileOpenedForWriting = false;
}
void FramesFileWriterReader::openNewFileForWriting()
{
closeFileIfOpened();
char filename[1024];
time_t t = time(0);
struct tm * now = localtime(&t);
sprintf(filename, "recording_%04d_%02d_%02d_%02d_%02d.bin", now->tm_year, now->tm_mon, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
m_sFilename = filename;
m_pFileHandle = fopen(filename, "wb");
m_bFileOpenedForReading = false;
m_bFileOpenedForWriting = true;
resetTimer();
}
bool FramesFileWriterReader::readFrame(std::vector<Point3s> &outPoints, std::vector<RGB> &outColors)
{
if (!m_bFileOpenedForReading)
openCurrentFileForReading();
outPoints.clear();
outColors.clear();
FILE *f = m_pFileHandle;
int nPoints, timestamp;
char tmp[1024];
int nread = fscanf_s(f, "%s %d %s %d", tmp, 1024, &nPoints, tmp, 1024, &timestamp);
if (nread < 4)
return false;
if (nPoints == 0)
return true;
fgetc(f); // '\n'
outPoints.resize(nPoints);
outColors.resize(nPoints);
fread((void*)outPoints.data(), sizeof(outPoints[0]), nPoints, f);
fread((void*)outColors.data(), sizeof(outColors[0]), nPoints, f);
fgetc(f); // '\n'
return true;
}
void FramesFileWriterReader::writeFrame(std::vector<Point3s> points, std::vector<RGB> colors)
{
if (!m_bFileOpenedForWriting)
openNewFileForWriting();
FILE *f = m_pFileHandle;
int nPoints = static_cast<int>(points.size());
fprintf(f, "n_points= %d\nframe_timestamp= %d\n", nPoints, getRecordingTimeMilliseconds());
if (nPoints > 0)
{
fwrite((void*)points.data(), sizeof(points[0]), nPoints, f);
fwrite((void*)colors.data(), sizeof(colors[0]), nPoints, f);
}
fprintf(f, "\n");
}
FramesFileWriterReader::~FramesFileWriterReader()
{
closeFileIfOpened();
}
+38 -13
View File
@@ -53,6 +53,7 @@ LiveScanClient::LiveScanClient() :
m_bFilter(false), m_bFilter(false),
m_bStreamOnlyBodies(false), m_bStreamOnlyBodies(false),
m_bCaptureFrame(false), m_bCaptureFrame(false),
m_bCaptureToFile(true),
m_bConnected(false), m_bConnected(false),
m_bConfirmCaptured(false), m_bConfirmCaptured(false),
m_bConfirmCalibrated(false), m_bConfirmCalibrated(false),
@@ -183,6 +184,8 @@ int LiveScanClient::Run(HINSTANCE hInstance, int nCmdShow)
return static_cast<int>(msg.wParam); return static_cast<int>(msg.wParam);
} }
void LiveScanClient::UpdateFrame() void LiveScanClient::UpdateFrame()
{ {
if (!pCapture->bInitialized) if (!pCapture->bInitialized)
@@ -203,10 +206,19 @@ void LiveScanClient::UpdateFrame()
if (m_bCaptureFrame) if (m_bCaptureFrame)
{ {
m_vGatheredVertices.push_back(m_vLastFrameVertices); if (m_bCaptureToFile)
m_vGatheredRGBPoints.push_back(m_vLastFrameRGB); {
m_bConfirmCaptured = true; m_framesFileWriterReader.writeFrame(m_vLastFrameVertices, m_vLastFrameRGB);
m_bCaptureFrame = false; m_bConfirmCaptured = true;
m_bCaptureFrame = false;
}
else
{
m_vGatheredVertices.push_back(m_vLastFrameVertices);
m_vGatheredRGBPoints.push_back(m_vLastFrameRGB);
m_bConfirmCaptured = true;
m_bCaptureFrame = false;
}
} }
} }
@@ -515,18 +527,32 @@ void LiveScanClient::HandleSocket()
{ {
byteToSend = MSG_STORED_FRAME; byteToSend = MSG_STORED_FRAME;
m_pClientSocket->SendBytes(&byteToSend, 1); m_pClientSocket->SendBytes(&byteToSend, 1);
if (m_bCaptureToFile)
if (m_vGatheredRGBPoints.size() > 0)
{ {
SendFrame(m_vGatheredVertices[0], m_vGatheredRGBPoints[0], m_vLastFrameBody); vector<Point3s> points;
vector<RGB> colors;
m_vGatheredRGBPoints.erase(m_vGatheredRGBPoints.begin(), m_vGatheredRGBPoints.begin() + 1); bool res = m_framesFileWriterReader.readFrame(points, colors);
m_vGatheredVertices.erase(m_vGatheredVertices.begin(), m_vGatheredVertices.begin() + 1); if (res == false)
{
int size = -1;
m_pClientSocket->SendBytes((char*)&size, 4);
} else
SendFrame(points, colors, m_vLastFrameBody);
} }
else else
{ {
int size = -1; if (m_vGatheredRGBPoints.size() > 0)
m_pClientSocket->SendBytes((char*)&size, 4); {
SendFrame(m_vGatheredVertices[0], m_vGatheredRGBPoints[0], m_vLastFrameBody);
m_vGatheredRGBPoints.erase(m_vGatheredRGBPoints.begin(), m_vGatheredRGBPoints.begin() + 1);
m_vGatheredVertices.erase(m_vGatheredVertices.begin(), m_vGatheredVertices.begin() + 1);
}
else
{
int size = -1;
m_pClientSocket->SendBytes((char*)&size, 4);
}
} }
} }
//send last frame //send last frame
@@ -692,7 +718,6 @@ void LiveScanClient::SendFrame(vector<Point3s> vertices, vector<RGB> RGB, vector
if (m_bFrameCompression) if (m_bFrameCompression)
{ {
char buf[8];
// *2, because according to zstd documentation, increasing the size of the output buffer above a // *2, because according to zstd documentation, increasing the size of the output buffer above a
// bound should speed up the compression. // bound should speed up the compression.
int cBuffSize = ZSTD_compressBound(size) * 2; int cBuffSize = ZSTD_compressBound(size) * 2;