From d19bc748e07b3f9e32be037492d6b514859f1e84 Mon Sep 17 00:00:00 2001 From: Van Pham <64571718+malajvan@users.noreply.github.com> Date: Mon, 8 May 2023 16:38:48 -0400 Subject: [PATCH 1/6] add bytes check &decode to core --- gamera-4/gamera/core.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gamera-4/gamera/core.py b/gamera-4/gamera/core.py index 928d3e06..1708fb2a 100644 --- a/gamera-4/gamera/core.py +++ b/gamera-4/gamera/core.py @@ -356,6 +356,8 @@ def classify_manual(self, id_name): A value in range (0, 1), where 0 is uncertain and 1 is certain. *class_name* A ``.``-delimited class name.""" + if type(id_name) is bytes: + id_name = id_name.decode() if util.is_string_or_unicode(id_name): id_name = [(1.0, id_name)] elif type(id_name) is not list: @@ -383,6 +385,8 @@ def classify_automatic(self, id_name): A value in range (0, 1), where 0 is uncertain and 1 is certain. *class_name* A ``.``-delimited class name.""" + if type(id_name) is bytes: + id_name = id_name.decode() if util.is_string_or_unicode(id_name): id_name = [(0.0, id_name)] elif not isinstance(id_name, list): From c43324a23008364441fd6b991f9a6df39511a990 Mon Sep 17 00:00:00 2001 From: Van Pham <64571718+malajvan@users.noreply.github.com> Date: Fri, 12 May 2023 15:39:48 -0400 Subject: [PATCH 2/6] temporary fix for bytes classname --- gamera-4/include/knnmodule.hpp | 10 ++++++---- gamera-4/src/knncore/knncoremodule.cpp | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/gamera-4/include/knnmodule.hpp b/gamera-4/include/knnmodule.hpp index ac708e06..a829f30d 100644 --- a/gamera-4/include/knnmodule.hpp +++ b/gamera-4/include/knnmodule.hpp @@ -85,10 +85,12 @@ inline int image_get_id_name(PyObject* image, char** id_name, int* len) { PyErr_SetString(PyExc_TypeError, "knn: could not get string from id_name tuple."); return -1; } - Py_ssize_t lenTmp = 0; - if(PyBytes_AsStringAndSize(id, id_name, &lenTmp) < 0){ - PyErr_SetString(PyExc_TypeError, "knn: could not get string from id_name tuple."); - return -1; + const char *id_str = PyUnicode_AsUTF8(id); //id is currently the class name, type PyUnicode (str) + PyObject *id_bytes = PyBytes_FromString(id_str); + Py_ssize_t lenTmp = 0; + if (PyBytes_AsStringAndSize(id_bytes, id_name, &lenTmp) < 0){ + PyErr_SetString(PyExc_TypeError, "knn: could not get string from id_name tuple."); + return -1; } *len = lenTmp; if (*id_name == nullptr) { diff --git a/gamera-4/src/knncore/knncoremodule.cpp b/gamera-4/src/knncore/knncoremodule.cpp index 381aee1c..c2919ab7 100644 --- a/gamera-4/src/knncore/knncoremodule.cpp +++ b/gamera-4/src/knncore/knncoremodule.cpp @@ -444,7 +444,7 @@ static PyObject* knn_classify_with_images(PyObject* self, PyObject* args) { (This is most likely because features have not been generated.)"); return 0; } - + //id_name is equivalent to class_name in glyph (RODAN Comment) char* id_name; int len; if (image_get_id_name(cur, &id_name, &len) < 0) { From 66d757099e1420e36ca8dd9376272eb77b78466a Mon Sep 17 00:00:00 2001 From: Van Pham <64571718+malajvan@users.noreply.github.com> Date: Mon, 8 May 2023 16:38:48 -0400 Subject: [PATCH 3/6] add bytes check &decode to core --- gamera-4/gamera/core.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gamera-4/gamera/core.py b/gamera-4/gamera/core.py index 928d3e06..1708fb2a 100644 --- a/gamera-4/gamera/core.py +++ b/gamera-4/gamera/core.py @@ -356,6 +356,8 @@ def classify_manual(self, id_name): A value in range (0, 1), where 0 is uncertain and 1 is certain. *class_name* A ``.``-delimited class name.""" + if type(id_name) is bytes: + id_name = id_name.decode() if util.is_string_or_unicode(id_name): id_name = [(1.0, id_name)] elif type(id_name) is not list: @@ -383,6 +385,8 @@ def classify_automatic(self, id_name): A value in range (0, 1), where 0 is uncertain and 1 is certain. *class_name* A ``.``-delimited class name.""" + if type(id_name) is bytes: + id_name = id_name.decode() if util.is_string_or_unicode(id_name): id_name = [(0.0, id_name)] elif not isinstance(id_name, list): From bf054da445e2305d78d7f80fcccd3a179ba65207 Mon Sep 17 00:00:00 2001 From: Van Pham <64571718+malajvan@users.noreply.github.com> Date: Fri, 12 May 2023 15:39:48 -0400 Subject: [PATCH 4/6] temporary fix for bytes classname --- gamera-4/include/knnmodule.hpp | 10 ++++++---- gamera-4/src/knncore/knncoremodule.cpp | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/gamera-4/include/knnmodule.hpp b/gamera-4/include/knnmodule.hpp index ac708e06..a829f30d 100644 --- a/gamera-4/include/knnmodule.hpp +++ b/gamera-4/include/knnmodule.hpp @@ -85,10 +85,12 @@ inline int image_get_id_name(PyObject* image, char** id_name, int* len) { PyErr_SetString(PyExc_TypeError, "knn: could not get string from id_name tuple."); return -1; } - Py_ssize_t lenTmp = 0; - if(PyBytes_AsStringAndSize(id, id_name, &lenTmp) < 0){ - PyErr_SetString(PyExc_TypeError, "knn: could not get string from id_name tuple."); - return -1; + const char *id_str = PyUnicode_AsUTF8(id); //id is currently the class name, type PyUnicode (str) + PyObject *id_bytes = PyBytes_FromString(id_str); + Py_ssize_t lenTmp = 0; + if (PyBytes_AsStringAndSize(id_bytes, id_name, &lenTmp) < 0){ + PyErr_SetString(PyExc_TypeError, "knn: could not get string from id_name tuple."); + return -1; } *len = lenTmp; if (*id_name == nullptr) { diff --git a/gamera-4/src/knncore/knncoremodule.cpp b/gamera-4/src/knncore/knncoremodule.cpp index 381aee1c..c2919ab7 100644 --- a/gamera-4/src/knncore/knncoremodule.cpp +++ b/gamera-4/src/knncore/knncoremodule.cpp @@ -444,7 +444,7 @@ static PyObject* knn_classify_with_images(PyObject* self, PyObject* args) { (This is most likely because features have not been generated.)"); return 0; } - + //id_name is equivalent to class_name in glyph (RODAN Comment) char* id_name; int len; if (image_get_id_name(cur, &id_name, &len) < 0) { From 46a55fb6a7cfe3526fad83ff849b4effda40800e Mon Sep 17 00:00:00 2001 From: Van Pham <64571718+malajvan@users.noreply.github.com> Date: Mon, 15 May 2023 16:00:04 -0400 Subject: [PATCH 5/6] update dockerfile for test build --- Dockerfile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index b5dd877d..2ca0d563 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,10 +12,11 @@ RUN apt-get install -y \ python3-distutils # Build Gamera 4. -RUN git clone https://github.com/DDMAL/gamera4-rodan.git -WORKDIR /gamera4-rodan +#RUN git clone https://github.com/DDMAL/gamera4-rodan.git +COPY . /gamera4-rodan +#WORKDIR /gamera4-rodan #this commit fixes miyao staff finder -RUN git checkout 76608f6 +#RUN git checkout 76608f6 WORKDIR /gamera4-rodan/gamera-4 RUN python3 setup.py --nowx build # We need to install Gamera (even though it won't be used) From 3865c74f067dddfe46c7d5c73f0b4708bcf3bacb Mon Sep 17 00:00:00 2001 From: Anthony Tan Date: Thu, 22 Jun 2023 14:51:41 -0400 Subject: [PATCH 6/6] add type checking to image_get_id_name to ensure it does not crash due to bytes/string confusion --- gamera-4/include/knnmodule.hpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/gamera-4/include/knnmodule.hpp b/gamera-4/include/knnmodule.hpp index a829f30d..81786026 100644 --- a/gamera-4/include/knnmodule.hpp +++ b/gamera-4/include/knnmodule.hpp @@ -85,8 +85,13 @@ inline int image_get_id_name(PyObject* image, char** id_name, int* len) { PyErr_SetString(PyExc_TypeError, "knn: could not get string from id_name tuple."); return -1; } - const char *id_str = PyUnicode_AsUTF8(id); //id is currently the class name, type PyUnicode (str) - PyObject *id_bytes = PyBytes_FromString(id_str); + PyObject *id_bytes = id; + PyTypeObject* type = Py_TYPE(id); + const char* typeName = type->tp_name; + if(PyUnicode_Check(id)) { + const char *id_str = PyUnicode_AsUTF8(id); //id is currently the class name, type PyUnicode (str) + id_bytes = PyBytes_FromString(id_str); + } Py_ssize_t lenTmp = 0; if (PyBytes_AsStringAndSize(id_bytes, id_name, &lenTmp) < 0){ PyErr_SetString(PyExc_TypeError, "knn: could not get string from id_name tuple.");