From 2145bf3d0b581c97e034f30d1733e38fdbdfa4cc Mon Sep 17 00:00:00 2001 From: Ashp116 Date: Sun, 15 Jun 2025 03:28:30 -0400 Subject: [PATCH 1/3] ADD: Added query search for searching images in a dataset --- roboflow/core/project.py | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/roboflow/core/project.py b/roboflow/core/project.py index c451d908..fbb51fce 100644 --- a/roboflow/core/project.py +++ b/roboflow/core/project.py @@ -790,6 +790,47 @@ def search_all( offset += limit + def query( + self, + query_str: Optional[str] = "", + page_size: int = 100, + fields: Optional[List[str]] = None, + ): + """ + Query images in a project using a semantic search query string. + + Args: + query_str (str): Search query string, e.g. 'filename:"example.jpg"' or 'project:foo night'. + page_size (int): Number of results to return per page (default: 100). + fields (list): Fields to return in results + (default: ["tags", "width", "height", "filename", "aspectRatio", "split"]). + + Returns: + A list of images that match the query criteria. + + Example: + >>> results = project.query(query_str='project:example', page_size=10) + """ + if fields is None: + fields = ["tags", "width", "height", "filename", "aspectRatio", "split"] + + payload: Dict[str, Union[str, int, List[str]]] = {} + + if query_str: + payload["query"] = query_str + + if page_size: + payload["pageSize"] = page_size + + payload["fields"] = fields + + data = requests.post( + f"{API_URL}/{self.__workspace}/search/v1?api_key={self.__api_key}", + json=payload, + ) + + return data.json().get("results", []) + def __str__(self): """ Show a string representation of a Project object. From 4e6fa1b1430b99b0a771ee738cdaa3f3a700de3a Mon Sep 17 00:00:00 2001 From: Ashp116 Date: Sun, 15 Jun 2025 03:47:15 -0400 Subject: [PATCH 2/3] ADD: Added query_all search for searching images in a dataset --- roboflow/core/project.py | 65 ++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/roboflow/core/project.py b/roboflow/core/project.py index fbb51fce..3d7de5de 100644 --- a/roboflow/core/project.py +++ b/roboflow/core/project.py @@ -795,33 +795,40 @@ def query( query_str: Optional[str] = "", page_size: int = 100, fields: Optional[List[str]] = None, + continuation_token: Optional[str] = None ): """ Query images in a project using a semantic search query string. Args: - query_str (str): Search query string, e.g. 'filename:"example.jpg"' or 'project:foo night'. - page_size (int): Number of results to return per page (default: 100). - fields (list): Fields to return in results - (default: ["tags", "width", "height", "filename", "aspectRatio", "split"]). + query_str (str, optional): Search query string, e.g. 'filename:"example.jpg"' or 'project:foo night'. + page_size (int, optional): Number of results to return per page (default is 100). + fields (list, optional): Fields to return in results. + Defaults to ["tags", "width", "height", "filename", "aspectRatio", "split"]. + continuation_token (str, optional): Token to continue fetching next results. Returns: - A list of images that match the query criteria. + tuple: A tuple containing: + - list: A list of images that match the query criteria. + - str or None: A continuation token if more results are available. Example: - >>> results = project.query(query_str='project:example', page_size=10) + >>> results, token = project.query(query_str='project:example', page_size=10) """ if fields is None: fields = ["tags", "width", "height", "filename", "aspectRatio", "split"] payload: Dict[str, Union[str, int, List[str]]] = {} - if query_str: + if query_str is not None: payload["query"] = query_str - if page_size: + if page_size is not None: payload["pageSize"] = page_size + if continuation_token is not None: + payload["continuationToken"] = continuation_token + payload["fields"] = fields data = requests.post( @@ -829,7 +836,47 @@ def query( json=payload, ) - return data.json().get("results", []) + return data.json().get("results", []), data.json()["continuationToken"] + + def query_all( + self, + query_str: Optional[str] = "", + page_size: int = 100, + fields: Optional[List[str]] = None, + ): + """ + Create a paginated list of semantic search results for images in a project. + + Args: + query_str (str): Search query string, e.g. 'filename:"example.jpg"' or 'project:foo night'. + page_size (int): Number of results to return per page (default: 100). + fields (list): Fields to return in results + (default: ["tags", "width", "height", "filename", "aspectRatio", "split"]). + + Returns: + Generator that yields pages of images that match the query criteria. + + Example: + >>> results = project.query_all(query_str="filename:image.png") + >>> for result in results: + >>> print(result) + """ # noqa: E501 // docs + + continuation_token = None + + while True: + data, continuation_token_temp = self.query( + query_str=query_str, + page_size=page_size, + fields=fields, + continuation_token=continuation_token + ) + + yield data + continuation_token = continuation_token_temp + + if len(data) < page_size: + break def __str__(self): """ From 3776fd5e2f484f9b03c01a598b34a42b6fb11c82 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Jun 2025 08:00:16 +0000 Subject: [PATCH 3/3] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto=20?= =?UTF-8?q?format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- roboflow/core/project.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/roboflow/core/project.py b/roboflow/core/project.py index 3d7de5de..a79edf4d 100644 --- a/roboflow/core/project.py +++ b/roboflow/core/project.py @@ -791,11 +791,11 @@ def search_all( offset += limit def query( - self, - query_str: Optional[str] = "", - page_size: int = 100, - fields: Optional[List[str]] = None, - continuation_token: Optional[str] = None + self, + query_str: Optional[str] = "", + page_size: int = 100, + fields: Optional[List[str]] = None, + continuation_token: Optional[str] = None, ): """ Query images in a project using a semantic search query string. @@ -839,10 +839,10 @@ def query( return data.json().get("results", []), data.json()["continuationToken"] def query_all( - self, - query_str: Optional[str] = "", - page_size: int = 100, - fields: Optional[List[str]] = None, + self, + query_str: Optional[str] = "", + page_size: int = 100, + fields: Optional[List[str]] = None, ): """ Create a paginated list of semantic search results for images in a project. @@ -850,7 +850,7 @@ def query_all( Args: query_str (str): Search query string, e.g. 'filename:"example.jpg"' or 'project:foo night'. page_size (int): Number of results to return per page (default: 100). - fields (list): Fields to return in results + fields (list): Fields to return in results (default: ["tags", "width", "height", "filename", "aspectRatio", "split"]). Returns: @@ -866,10 +866,7 @@ def query_all( while True: data, continuation_token_temp = self.query( - query_str=query_str, - page_size=page_size, - fields=fields, - continuation_token=continuation_token + query_str=query_str, page_size=page_size, fields=fields, continuation_token=continuation_token ) yield data