Untitled

 avatar
unknown
plain_text
a year ago
4.3 kB
14
Indexable
#include "pch.h"

#include <iostream>
#include <fstream>
#include <string>
#include <deque>
#include "StringUtils.h"

#pragma warning(disable: 4251 4800)
#include "TriggerFileExtensions.pb.h"
#include <google/protobuf/util/delimited_message_util.h>

#define CLICKIT_PROTOBUF_LIB_EXPORTS
#include "ProtobufWrapper.h"

using namespace std;

namespace TF = ClickIt::Utilities::TriggerFileEx;

struct ObjectDetails
{
	int X;
	int Y;
	int W;
	int H;
	/// <summary>
	/// Class Id
	/// </summary>
	int C;
	/// <summary>
	/// Confidence (score)
	/// </summary>
	int S;
	/// <summary>
	/// Object Id
	/// </summary>
	int I;
};


bool file_exists(const std::wstring& name) {
	ifstream f(name.c_str());
	return f.good();
}

string GetClassLabel(int classId)
{
	switch (classId) 
	{
	case 0: return "Person";
	case 1: return "Bicycle";
	case 2: return "Car";
	case 3: return "Motorcycle";      
	case 5: return "Bus";
	case 7: return "Truck";
	case 26: return "Bag";
	default: return "Unclassified";
	}
}

extern "C" CLICKIT_PROTOBUF_LIB_API bool ProtobufWriteHeader(LPCTSTR filename, __int64 time, BYTE* pNoiseMaskData, DWORD NMWidth, DWORD NMHeight, bool no_overwrite)
{
	// Verify that the version of the library that we linked against is
	// compatible with the version of the headers we compiled against.
	GOOGLE_PROTOBUF_VERIFY_VERSION;

	try
	{
		if (no_overwrite && file_exists(filename))
			return true;

		// It's critical to write the files with the stream mode set to binary. Otherwise, the stream will have a number of 0D0A characters instead of 0A.
		// The c# protobuf-net and google.ProtoBuf will both choke badly on the file in that case.
		// 
		// Also, the C++ serializers use the equivalent of the C# PrefixStyle.Base128 for the lengths, so the C# readers must use that.
		//
		TF::Header hdr;
		hdr.set_filename(to_string(filename));
		hdr.set_time(time);
		hdr.mutable_noisemask()->set_data(string(reinterpret_cast<const char*>(pNoiseMaskData), ((NMWidth * NMHeight) + 7) / 8));		//length of data when bit packed. bit packing is already done in CBaseMotionDetector.cpp
		//hdr.mutable_noisemask()->set_data(pNoiseMaskData, 50);		
		hdr.mutable_noisemask()->set_width(NMWidth);
		hdr.mutable_noisemask()->set_height(NMHeight);
		

		ofstream fs(filename, ios_base::trunc | ios_base::binary);
		return google::protobuf::util::SerializeDelimitedToOstream(hdr, &fs);
	}
	catch (const std::exception&)
	{
		return false;
	}
}

extern "C" CLICKIT_PROTOBUF_LIB_API bool ProtobufAddThumbnail(LPCTSTR filename, __int64 time, const BYTE * data, DWORD length)
{
	try
	{
		TF::Record f;
		f.set_time(time);
		auto m = f.mutable_thumbnail();
		m->set_image(string(&data[0], &data[length]));
		ofstream fs(filename, ios::ios_base::app | ios_base::binary);
		return google::protobuf::util::SerializeDelimitedToOstream(f, &fs);
	}
	catch (const std::exception&)
	{
		return false;
	}
}

extern "C" CLICKIT_PROTOBUF_LIB_API bool ProtobufAddObjectDetails(LPCTSTR filename, __int64 time, const BYTE * data, DWORD length)
{
	if (data == nullptr || length == 0) 
	{
		// Empty or null data buffer.
		return false;
	}

	size_t structSize = sizeof(ObjectDetails);
	if (length % structSize != 0) 
	{
		// Data length is not aligned with ObjectDetails size
		return false;
	}

	size_t count = length / structSize;

	try
	{
		TF::Record r;
		r.set_time(time);
		for (int i = 0; i < count; i++)
		{
			const ObjectDetails* objDetails = reinterpret_cast<const ObjectDetails*>(data + i * structSize);

			auto* obj = r.add_objects();
			obj->set_id(static_cast<int>(0x0000000FFFFFFFF & objDetails->I));
			obj->set_camera_object_id(objDetails->I);
			obj->set_type(GetClassLabel(objDetails->C));

			auto* bb = obj->mutable_bounding_box();
			bb->set_left(objDetails->X);
			bb->set_top(objDetails->Y);
			bb->set_right(objDetails->X + objDetails->W);
			bb->set_bottom(objDetails->Y + objDetails->H);

			obj->set_confidence(objDetails->S / 100.00f);
		}		
		ofstream fs(filename, ios::ios_base::app | ios_base::binary);
		return google::protobuf::util::SerializeDelimitedToOstream(r, &fs);
	}
	catch (const std::exception&)
	{
		return false;
	}
}
Editor is loading...
Leave a Comment