hjxdl 0.2.82__py3-none-any.whl → 0.2.84__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
hdl/_version.py CHANGED
@@ -12,5 +12,5 @@ __version__: str
12
12
  __version_tuple__: VERSION_TUPLE
13
13
  version_tuple: VERSION_TUPLE
14
14
 
15
- __version__ = version = '0.2.82'
16
- __version_tuple__ = version_tuple = (0, 2, 82)
15
+ __version__ = version = '0.2.84'
16
+ __version_tuple__ = version_tuple = (0, 2, 84)
@@ -30,3 +30,14 @@ COT_TEMPLATE = """
30
30
  你的回答中应只能是 Markdown 格式,且不能包含其他多余文字或格式错误。
31
31
  以下是可用的工具:
32
32
  """
33
+
34
+ OD_TEMPLATE = """
35
+ Detect all the objects in the image, return bounding boxes for all of them using the following format (DO NOT INCLUDE ANY OTHER WORDS IN YOUR ANSWER BUT ONLY THE LIST ITSELF!):
36
+ [
37
+ {
38
+ "object": "object_name",
39
+ "bboxes": [[xmin, ymin, xmax, ymax], [xmin, ymin, xmax, ymax], ...]
40
+ },
41
+ ...
42
+ ]
43
+ """
hdl/utils/llm/chat.py CHANGED
@@ -8,8 +8,9 @@ import re
8
8
 
9
9
 
10
10
  from openai import OpenAI
11
- from ..desc.template import FN_TEMPLATE, COT_TEMPLATE
11
+ from ..desc.template import FN_TEMPLATE, COT_TEMPLATE, OD_TEMPLATE
12
12
  from ..desc.func_desc import TOOL_DESC
13
+ from .vis import draw_and_plot_boxes_from_json
13
14
  import json
14
15
  # import traceback
15
16
 
@@ -132,6 +133,7 @@ class OpenAI_M():
132
133
  tools: list = None,
133
134
  tool_desc: dict = None,
134
135
  cot_desc: str = None,
136
+ od_desc: str = None,
135
137
  *args,
136
138
  **kwargs
137
139
  ):
@@ -189,9 +191,8 @@ class OpenAI_M():
189
191
  self.tool_info = "\n".join(self.tool_descs)
190
192
  self.tool_desc_str = "\n".join(self.tool_descs_verbose)
191
193
 
192
- self.cot_desc = cot_desc
193
- if not self.cot_desc:
194
- self.cot_desc = COT_TEMPLATE
194
+ self.cot_desc = cot_desc if cot_desc else COT_TEMPLATE
195
+ self.od_desc = od_desc if od_desc else OD_TEMPLATE
195
196
 
196
197
  def cot(
197
198
  self,
@@ -573,6 +574,26 @@ class OpenAI_M():
573
574
  print(e)
574
575
  return ""
575
576
 
577
+ def od(
578
+ self,
579
+ image_path,
580
+ ):
581
+ json_str = self.invoke(
582
+ prompt=self.od_desc,
583
+ images=image_path,
584
+ )
585
+ return json_str
586
+
587
+ def od_v(
588
+ self,
589
+ image_path,
590
+ save_path,
591
+ ):
592
+ json_str = self.od(image_path)
593
+ img = draw_and_plot_boxes_from_json(json_str, image_path, save_path)
594
+ return img, save_path
595
+
596
+
576
597
 
577
598
  class MMChatter():
578
599
  def __init__(
@@ -644,4 +665,4 @@ class MMChatter():
644
665
  # Process the model's response by parsing the output
645
666
  response = output.splitlines()[-1].strip('<assistant>') # Assuming the last line is the model's response
646
667
 
647
- return response
668
+ return response
hdl/utils/llm/vis.py CHANGED
@@ -8,11 +8,15 @@ import hashlib
8
8
 
9
9
  import torch
10
10
  import numpy as np
11
- from PIL import Image
12
11
  # from transformers import ChineseCLIPProcessor, ChineseCLIPModel
13
12
  from transformers import AutoModel
14
13
  from transformers import AutoTokenizer
15
14
  import open_clip
15
+
16
+ from PIL import Image, ImageDraw, ImageFont
17
+ import json
18
+ import re
19
+ import matplotlib.pyplot as plt
16
20
  # import natsort
17
21
  from redis.commands.search.field import VectorField
18
22
  from redis.commands.search.indexDefinition import IndexDefinition, IndexType
@@ -120,6 +124,87 @@ def pilimg_to_base64(pilimg):
120
124
  return img_base64
121
125
 
122
126
 
127
+
128
+ def draw_and_plot_boxes_from_json(
129
+ json_data,
130
+ image_path,
131
+ save_path=None
132
+ ):
133
+ """
134
+ Parses the JSON data to extract bounding box coordinates,
135
+ scales them according to the image size, draws the boxes on the image,
136
+ and returns the image as a PIL object.
137
+
138
+ Args:
139
+ json_data (str or list): The JSON data as a string or already parsed list.
140
+ image_path (str): The path to the image file on which boxes are to be drawn.
141
+ save_path (str or None): The path to save the resulting image. If None, the image won't be saved.
142
+
143
+ Returns:
144
+ PIL.Image.Image: The processed image with boxes drawn on it.
145
+ """
146
+ # If json_data is a string, parse it into a Python object
147
+ if isinstance(json_data, str):
148
+ json_data = json_data.strip()
149
+ json_data = re.sub(r"^```json\s*", "", json_data)
150
+ json_data = re.sub(r"```$", "", json_data)
151
+ try:
152
+ data = json.loads(json_data)
153
+ except json.JSONDecodeError as e:
154
+ print("Failed to parse JSON data:", e)
155
+ return None
156
+ else:
157
+ data = json_data
158
+
159
+ # Open the image
160
+ try:
161
+ img = Image.open(image_path)
162
+ except FileNotFoundError:
163
+ print(f"Image file not found at {image_path}. Please check the path.")
164
+ return None
165
+
166
+ draw = ImageDraw.Draw(img)
167
+ width, height = img.size
168
+
169
+ # Use a commonly available font
170
+ try:
171
+ font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", size=25)
172
+ except IOError:
173
+ print("Default font not found. Using a basic PIL font.")
174
+ font = ImageFont.load_default()
175
+
176
+ # Process and draw boxes
177
+ for item in data:
178
+ object_type = item.get("object", "unknown")
179
+ for bbox in item.get("bboxes", []):
180
+ x1, y1, x2, y2 = bbox
181
+ x1 = x1 * width / 1000
182
+ y1 = y1 * height / 1000
183
+ x2 = x2 * width / 1000
184
+ y2 = y2 * height / 1000
185
+ draw.rectangle([(x1, y1), (x2, y2)], outline="blue", width=5)
186
+ draw.text((x1, y1), object_type, fill="red", font=font)
187
+
188
+ # Plot the image using matplotlib and save it as a PIL Image
189
+ buf = BytesIO()
190
+ plt.figure(figsize=(8, 8))
191
+ plt.imshow(img)
192
+ plt.axis("off") # Hide axes ticks
193
+ plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0)
194
+ buf.seek(0)
195
+
196
+ # Load the buffer into a PIL Image and ensure full loading into memory
197
+ pil_image = Image.open(buf)
198
+ pil_image.load() # Ensure full data is loaded from the buffer
199
+
200
+ # Save the image if save_path is provided
201
+ if save_path:
202
+ pil_image.save(save_path)
203
+
204
+ buf.close() # Close the buffer after use
205
+
206
+ return pil_image
207
+
123
208
  class ImgHandler:
124
209
  """
125
210
  ImgHandler is a class for handling image processing tasks using pretrained models.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: hjxdl
3
- Version: 0.2.82
3
+ Version: 0.2.84
4
4
  Summary: A collection of functions for Jupyter notebooks
5
5
  Home-page: https://github.com/huluxiaohuowa/hdl
6
6
  Author: Jianxing Hu
@@ -1,5 +1,5 @@
1
1
  hdl/__init__.py,sha256=GffnD0jLJdhkd-vo989v40N90sQbofkayRBwxc6TVhQ,72
2
- hdl/_version.py,sha256=e3IzU75tUXwJv1hSzAqotnR7QIz2cy3BvcTNm40NY9c,413
2
+ hdl/_version.py,sha256=6bOdkZwhZPgKGVmGjUF-gPGdfXzkUfQKQI9fPPKf_UQ,413
3
3
  hdl/args/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  hdl/args/loss_args.py,sha256=s7YzSdd7IjD24rZvvOrxLLFqMZQb9YylxKeyelSdrTk,70
5
5
  hdl/controllers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -123,23 +123,23 @@ hdl/utils/database_tools/datetime.py,sha256=xqE2xNiOpADzX-R8_bM0bioJRF3Ay9Jp1CAG
123
123
  hdl/utils/database_tools/web.py,sha256=awJ8lafL-2KRjf3V1uuij8JIvX9U5fI8fLZKOkOvqtk,5771
124
124
  hdl/utils/desc/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
125
125
  hdl/utils/desc/func_desc.py,sha256=VCqjvZs7qCwBq3NR3ZRknl4oiO5-JP7xm-Rx85W2exg,3365
126
- hdl/utils/desc/template.py,sha256=GJSXkVzdTAQoNT3j7YTLhz8-4CmvCMt2gLr7YBYPRWw,2192
126
+ hdl/utils/desc/template.py,sha256=Kf_tbL-XkDCKNQ3UncbCuYEeUgXEa7kRVCf9TD2b8og,2526
127
127
  hdl/utils/general/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
128
128
  hdl/utils/general/glob.py,sha256=Zuf7WHU0UdUPOs9UrhxmrCiMC8GrHxQU6n3mTThv6yc,1120
129
129
  hdl/utils/general/runners.py,sha256=x7QBolp3MrqNV6L4rB6Ueybr26bqkRFZTuXhY0SwyLk,3061
130
130
  hdl/utils/llm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
131
- hdl/utils/llm/chat.py,sha256=JnegglANgyp-avF4BdnhGji1w-x2wtonPPgjllsquV0,23283
131
+ hdl/utils/llm/chat.py,sha256=nEQMWqbRQe4J0cHMZZ0lGYW8HMBsKUTqS9-bHV452mY,23797
132
132
  hdl/utils/llm/chatgr.py,sha256=5F5PJHe8vz3iCfi4TT54DCLRi1UeJshECdVtgvvvao0,3696
133
133
  hdl/utils/llm/embs.py,sha256=Tf0FOYrOFZp7qQpEPiSCXzlgyHH0X9HVTUtsup74a9E,7174
134
134
  hdl/utils/llm/extract.py,sha256=2sK_WJzmYIc8iuWaM9DA6Nw3_6q1O4lJ5pKpcZo-bBA,6512
135
135
  hdl/utils/llm/llama_chat.py,sha256=watcHGOaz-bv3x-yDucYlGk5f8FiqfFhwWogrl334fk,4387
136
- hdl/utils/llm/vis.py,sha256=BsGAfy5X8sMFnX5A3vHpTPDRe_-IDdhs6YVQ-efvyQ0,21424
136
+ hdl/utils/llm/vis.py,sha256=JPil4gJ-n-awv_EjNIjOwG9Gc89lsqTojV58_66U0_A,24204
137
137
  hdl/utils/llm/visrag.py,sha256=0i-VrxqgiV-J7R3VPshu9oc7-rKjFJOldYik3HDXj6M,10176
138
138
  hdl/utils/schedulers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
139
139
  hdl/utils/schedulers/norm_lr.py,sha256=bDwCmdEK-WkgxQMFBiMuchv8Mm7C0-GZJ6usm-PQk14,4461
140
140
  hdl/utils/weather/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
141
141
  hdl/utils/weather/weather.py,sha256=k11o6wM15kF8b9NMlEfrg68ak-SfSYLN3nOOflFUv-I,4381
142
- hjxdl-0.2.82.dist-info/METADATA,sha256=o5ShJ2TtbtQjRWiWt_laol2kYbOys3NYLJsJdvffplo,836
143
- hjxdl-0.2.82.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
144
- hjxdl-0.2.82.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
145
- hjxdl-0.2.82.dist-info/RECORD,,
142
+ hjxdl-0.2.84.dist-info/METADATA,sha256=tuKn0Ql6hjCplkEJLhtI3CgA2Z4hC7MGzR9kgce6x3Q,836
143
+ hjxdl-0.2.84.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
144
+ hjxdl-0.2.84.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
145
+ hjxdl-0.2.84.dist-info/RECORD,,
File without changes