gesture deserialized ok

This commit is contained in:
侯歌 2024-07-26 08:56:24 +08:00
parent d53266fb09
commit e6bd3f6bc2
11 changed files with 43 additions and 38 deletions

View File

@ -23,7 +23,7 @@ Gesture* Gesture::clone() {
/**
* @return all the strokes of the gesture
*/
std::vector<GestureStroke*> Gesture::getStrokes() {
const std::vector<GestureStroke*>& Gesture::getStrokes() const{
return mStrokes;
}
@ -195,16 +195,16 @@ void Gesture::serialize(std::ostream& out){
}
}
Gesture Gesture::deserialize(std::istream& in){
Gesture gesture;
Gesture* Gesture::deserialize(std::istream& in){
Gesture* gesture=new Gesture();
// Gesture ID
gesture.mGestureID = GestureIOHelper::readLong(in);
gesture->mGestureID = GestureIOHelper::readLong(in);
// Number of strokes
int count = GestureIOHelper::readInt(in);
const int count = GestureIOHelper::readInt(in);
LOGD("gesture:%llu %d strokes",int64_t(gesture->mGestureID),count);
for (int i = 0; i < count; i++) {
gesture.addStroke(GestureStroke::deserialize(in));
gesture->addStroke(GestureStroke::deserialize(in));
}
return gesture;

View File

@ -26,7 +26,7 @@ public:
/**
* @return all the strokes of the gesture
*/
std::vector<GestureStroke*> getStrokes();
const std::vector<GestureStroke*>& getStrokes()const;
/**
* @return the number of strokes included by this gesture
@ -89,7 +89,7 @@ public:
*/
Bitmap toBitmap(int width, int height, int inset, int color);
void serialize(std::ostream& out);
static Gesture deserialize(std::istream& in);
static Gesture* deserialize(std::istream& in);
#if 0
public static final @android.annotation.NonNull Parcelable.Creator<Gesture> CREATOR = new Parcelable.Creator<Gesture>() {
public Gesture createFromParcel(Parcel in);

View File

@ -36,8 +36,13 @@ public:
bool load() override{
if (!mResourceId.empty()) {
auto ifs = mContext->getInputStream(mResourceId,nullptr);
mStore->load(*ifs, true);
if(mContext){
auto fs=mContext->getInputStream(mResourceId,nullptr);
if(fs)mStore->load(*fs);
}else{
std::ifstream fs(mResourceId);
mStore->load(fs, true);
}
LOGD("load the gesture library from %s",mResourceId.c_str());
return true;
}

View File

@ -47,7 +47,7 @@ public:
return mStore->getGestureEntries();
}
std::vector<Prediction> recognize(Gesture* gesture) {
std::vector<Prediction> recognize(const Gesture& gesture) {
return mStore->recognize(gesture);
}

View File

@ -80,8 +80,8 @@ std::vector<std::string> GestureStore::getGestureEntries() {
* @param gesture the query
* @return a list of predictions of possible entries for a given gesture
*/
std::vector<Prediction> GestureStore::recognize(Gesture* gesture) {
Instance* instance = Instance::createInstance(mSequenceType, mOrientationStyle, *gesture, nullptr);
std::vector<Prediction> GestureStore::recognize(const Gesture& gesture) {
Instance* instance = Instance::createInstance(mSequenceType, mOrientationStyle, gesture, nullptr);
return mClassifier->classify(mSequenceType, mOrientationStyle, instance->vector);
}
@ -251,13 +251,13 @@ void GestureStore::readFormatV1(std::istream& in) {
const std::string name = GestureIOHelper::readUTF(in);
// Number of gestures
const int gestureCount = GestureIOHelper::readInt(in);
LOGD("[%d]%d :%s",i,gestureCount,name.c_str());
std::vector<Gesture*> gestures;
/*for (int j = 0; j < gestureCount; j++) {
for (int j = 0; j < gestureCount; j++) {
Gesture* gesture = Gesture::deserialize(in);
gestures.push_back(gesture);
classifier->addInstance(Instance::createInstance(mSequenceType, mOrientationStyle, gesture, name));
}*/
//classifier->addInstance(Instance::createInstance(mSequenceType, mOrientationStyle, gesture, name));
}
namedGestures.insert({name, gestures});
}
}
@ -282,7 +282,7 @@ namespace GestureIOHelper{
int32_t readInt(std::istream&in){
uint8_t u8[4];
readBytes(in,u8,4);
return (u8[0]<<24)|(u8[1]<<16)||(u8[2]<<8)|u8[3];
return (u8[0]<<24)|(u8[1]<<16)|(u8[2]<<8)|u8[3];
}
int64_t readLong(std::istream&in){
@ -304,13 +304,13 @@ namespace GestureIOHelper{
}
std::string readUTF(std::istream&in){
int32_t size = readInt(in);
uint8_t *buff= new uint8_t[size+1];
const uint16_t utflen = readShort(in);
uint8_t *bBuff= new uint8_t[utflen+1];
std::string str;
readBytes(in,buff,size);
buff[size] = 0;
str = (char*)buff;
delete []buff;
readBytes(in,bBuff,utflen);
bBuff[utflen] = 0;
str = (char*)bBuff;
delete []bBuff;
return str;
}
@ -354,7 +354,7 @@ namespace GestureIOHelper{
}
void writeUTF(std::ostream&out,const std::string&str){
writeInt(out,str.size());
writeShort(out,str.size());
writeBytes(out,(uint8_t*)str.c_str(),str.size());
}
}/*endof namespace GestureIOHelper*/

View File

@ -107,7 +107,7 @@ public:
* @param gesture the query
* @return a list of predictions of possible entries for a given gesture
*/
std::vector<Prediction> recognize(Gesture* gesture);
std::vector<Prediction> recognize(const Gesture& gesture);
/**
* Add a gesture for the entry
*

View File

@ -170,7 +170,7 @@ GestureStroke* GestureStroke::deserialize(std::istream& in){
for (int i = 0; i < count; i++) {
points.push_back(GesturePoint::deserialize(in));
}
LOGD("\t\t%d points",count);
return new GestureStroke(points);
}

View File

@ -29,7 +29,7 @@ GestureUtils::GestureUtils() {
* as a 1D array. The float at index i represents the grayscale
* value at pixel [i%bitmapSize, i/bitmapSize]
*/
std::vector<float> GestureUtils::spatialSampling(Gesture& gesture, int bitmapSize) {
std::vector<float> GestureUtils::spatialSampling(const Gesture& gesture, int bitmapSize) {
return spatialSampling(gesture, bitmapSize, false);
}
@ -46,7 +46,7 @@ std::vector<float> GestureUtils::spatialSampling(Gesture& gesture, int bitmapSiz
* as a 1D array. The float at index i represents the grayscale
* value at pixel [i%bitmapSize, i/bitmapSize]
*/
std::vector<float> GestureUtils::spatialSampling(Gesture& gesture, int bitmapSize,bool keepAspectRatio) {
std::vector<float> GestureUtils::spatialSampling(const Gesture& gesture, int bitmapSize,bool keepAspectRatio) {
const float targetPatchSize = bitmapSize - 1;
std::vector<float> sample(bitmapSize * bitmapSize,0);
//Arrays.fill(sample, 0);

View File

@ -79,7 +79,7 @@ public:
* as a 1D array. The float at index i represents the grayscale
* value at pixel [i%bitmapSize, i/bitmapSize]
*/
static std::vector<float> spatialSampling(Gesture& gesture, int bitmapSize);
static std::vector<float> spatialSampling(const Gesture& gesture, int bitmapSize);
/**
* Samples the gesture spatially by rendering the gesture into a 2D
@ -94,7 +94,7 @@ public:
* as a 1D array. The float at index i represents the grayscale
* value at pixel [i%bitmapSize, i/bitmapSize]
*/
static std::vector<float> spatialSampling(Gesture& gesture, int bitmapSize,bool keepAspectRatio);
static std::vector<float> spatialSampling(const Gesture& gesture, int bitmapSize,bool keepAspectRatio);
/**
* Samples a stroke temporally into a given number of evenly-distributed

View File

@ -30,7 +30,7 @@ void Instance::normalize() {
* @param label
* @return the instance
*/
Instance* Instance::createInstance(int sequenceType, int orientationType, Gesture& gesture, const std::string& label) {
Instance* Instance::createInstance(int sequenceType, int orientationType,const Gesture& gesture, const std::string& label) {
std::vector<float> pts;
Instance* instance;
if (sequenceType == GestureStore::SEQUENCE_SENSITIVE) {
@ -44,7 +44,7 @@ Instance* Instance::createInstance(int sequenceType, int orientationType, Gestur
return instance;
}
std::vector<float> Instance::spatialSampler(Gesture& gesture) {
std::vector<float> Instance::spatialSampler(const Gesture& gesture) {
return GestureUtils::spatialSampling(gesture, PATCH_SAMPLE_SIZE, false);
}
@ -55,7 +55,7 @@ static float ORIENTATIONS[] = {
(float) (-M_PI * 3 / 4), (float) -M_PI
};
std::vector<float> Instance::temporalSampler(int orientationType, Gesture& gesture) {
std::vector<float> Instance::temporalSampler(int orientationType,const Gesture& gesture) {
std::vector<float> pts = GestureUtils::temporalSampling(*gesture.getStrokes().at(0),
SEQUENCE_SAMPLE_SIZE);
std::vector<float> center = GestureUtils::computeCentroid(pts);

View File

@ -13,8 +13,8 @@ private:
private:
Instance(long id,const std::vector<float>& sample,const std::string& sampleName);
void normalize();
static std::vector<float> spatialSampler(Gesture& gesture);
static std::vector<float> temporalSampler(int orientationType, Gesture& gesture);
static std::vector<float> spatialSampler(const Gesture& gesture);
static std::vector<float> temporalSampler(int orientationType,const Gesture& gesture);
protected:
// the feature vector
std::vector<float> vector;
@ -35,7 +35,7 @@ public:
* @param label
* @return the instance
*/
static Instance* createInstance(int sequenceType, int orientationType, Gesture& gesture,const std::string& label);
static Instance* createInstance(int sequenceType, int orientationType,const Gesture& gesture,const std::string& label);
};
}/*endof namespace*/
#endif /*__GESTURE_INSTANCE_H__*/