hjxdl 0.2.82__py3-none-any.whl → 0.2.88__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 +2 -2
- hdl/utils/desc/template.py +11 -0
- hdl/utils/llm/chat.py +43 -6
- hdl/utils/llm/vis.py +102 -1
- {hjxdl-0.2.82.dist-info → hjxdl-0.2.88.dist-info}/METADATA +1 -1
- {hjxdl-0.2.82.dist-info → hjxdl-0.2.88.dist-info}/RECORD +8 -8
- {hjxdl-0.2.82.dist-info → hjxdl-0.2.88.dist-info}/WHEEL +0 -0
- {hjxdl-0.2.82.dist-info → hjxdl-0.2.88.dist-info}/top_level.txt +0 -0
hdl/_version.py
CHANGED
hdl/utils/desc/template.py
CHANGED
@@ -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,9 +8,10 @@ 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
|
-
import
|
13
|
+
from .vis import draw_and_plot_boxes_from_json, to_img
|
14
|
+
# import json
|
14
15
|
# import traceback
|
15
16
|
|
16
17
|
def parse_fn_markdown(markdown_text, params_key="params"):
|
@@ -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
|
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,42 @@ class OpenAI_M():
|
|
573
574
|
print(e)
|
574
575
|
return ""
|
575
576
|
|
577
|
+
def od(
|
578
|
+
self,
|
579
|
+
image,
|
580
|
+
):
|
581
|
+
"""
|
582
|
+
Perform object detection on the given image.
|
583
|
+
Args:
|
584
|
+
image_path (str): The path to the image file on which to perform object detection.
|
585
|
+
Returns:
|
586
|
+
str: A JSON string containing the results of the object detection.
|
587
|
+
"""
|
588
|
+
if isinstance(image, str):
|
589
|
+
image = to_img(image)
|
590
|
+
json_str = self.invoke(
|
591
|
+
prompt=self.od_desc,
|
592
|
+
images=[image]
|
593
|
+
)
|
594
|
+
return json_str
|
595
|
+
|
596
|
+
def od_v(
|
597
|
+
self,
|
598
|
+
image,
|
599
|
+
save_path,
|
600
|
+
):
|
601
|
+
"""
|
602
|
+
Perform object detection on an image and save the result.
|
603
|
+
Args:
|
604
|
+
image_path (str): The path to the input image.
|
605
|
+
save_path (str): The path to save the output image with detected objects.
|
606
|
+
Returns:
|
607
|
+
tuple: A tuple containing the processed image and the save path.
|
608
|
+
"""
|
609
|
+
json_str = self.od(image)
|
610
|
+
img = draw_and_plot_boxes_from_json(json_str, image, save_path)
|
611
|
+
return img, save_path
|
612
|
+
|
576
613
|
|
577
614
|
class MMChatter():
|
578
615
|
def __init__(
|
@@ -644,4 +681,4 @@ class MMChatter():
|
|
644
681
|
# Process the model's response by parsing the output
|
645
682
|
response = output.splitlines()[-1].strip('<assistant>') # Assuming the last line is the model's response
|
646
683
|
|
647
|
-
return response
|
684
|
+
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
|
@@ -25,6 +29,21 @@ from redis.commands.search.query import Query
|
|
25
29
|
|
26
30
|
HF_HUB_PREFIX = "hf-hub:"
|
27
31
|
|
32
|
+
def to_img(img_str):
|
33
|
+
if img_str.startswith("data:image"):
|
34
|
+
img = imgbase64_to_pilimg(img_str)
|
35
|
+
elif img_str.startswith("http"):
|
36
|
+
response = requests.get(img_str)
|
37
|
+
if response.status_code == 200:
|
38
|
+
# Read the image content from the response
|
39
|
+
img_data = response.content
|
40
|
+
|
41
|
+
# Load the image using PIL to determine its format
|
42
|
+
img = Image.open(BytesIO(img_data))
|
43
|
+
elif Path(img_str).is_file():
|
44
|
+
img = Image.open(img_str)
|
45
|
+
return img
|
46
|
+
|
28
47
|
|
29
48
|
def imgurl_to_base64(image_url: str):
|
30
49
|
"""Converts an image from a URL to base64 format.
|
@@ -120,6 +139,88 @@ def pilimg_to_base64(pilimg):
|
|
120
139
|
return img_base64
|
121
140
|
|
122
141
|
|
142
|
+
|
143
|
+
def draw_and_plot_boxes_from_json(
|
144
|
+
json_data,
|
145
|
+
image,
|
146
|
+
save_path=None
|
147
|
+
):
|
148
|
+
"""
|
149
|
+
Parses the JSON data to extract bounding box coordinates,
|
150
|
+
scales them according to the image size, draws the boxes on the image,
|
151
|
+
and returns the image as a PIL object.
|
152
|
+
|
153
|
+
Args:
|
154
|
+
json_data (str or list): The JSON data as a string or already parsed list.
|
155
|
+
image_path (str): The path to the image file on which boxes are to be drawn.
|
156
|
+
save_path (str or None): The path to save the resulting image. If None, the image won't be saved.
|
157
|
+
|
158
|
+
Returns:
|
159
|
+
PIL.Image.Image: The processed image with boxes drawn on it.
|
160
|
+
"""
|
161
|
+
# If json_data is a string, parse it into a Python object
|
162
|
+
if isinstance(json_data, str):
|
163
|
+
json_data = json_data.strip()
|
164
|
+
json_data = re.sub(r"^```json\s*", "", json_data)
|
165
|
+
json_data = re.sub(r"```$", "", json_data)
|
166
|
+
try:
|
167
|
+
data = json.loads(json_data)
|
168
|
+
except json.JSONDecodeError as e:
|
169
|
+
print("Failed to parse JSON data:", e)
|
170
|
+
return None
|
171
|
+
else:
|
172
|
+
data = json_data
|
173
|
+
|
174
|
+
# Open the image
|
175
|
+
# try:
|
176
|
+
# img = Image.open(image_path)
|
177
|
+
# except FileNotFoundError:
|
178
|
+
# print(f"Image file not found at {image_path}. Please check the path.")
|
179
|
+
# return None
|
180
|
+
img = image
|
181
|
+
|
182
|
+
draw = ImageDraw.Draw(img)
|
183
|
+
width, height = img.size
|
184
|
+
|
185
|
+
# Use a commonly available font
|
186
|
+
try:
|
187
|
+
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", size=25)
|
188
|
+
except IOError:
|
189
|
+
print("Default font not found. Using a basic PIL font.")
|
190
|
+
font = ImageFont.load_default()
|
191
|
+
|
192
|
+
# Process and draw boxes
|
193
|
+
for item in data:
|
194
|
+
object_type = item.get("object", "unknown")
|
195
|
+
for bbox in item.get("bboxes", []):
|
196
|
+
x1, y1, x2, y2 = bbox
|
197
|
+
x1 = x1 * width / 1000
|
198
|
+
y1 = y1 * height / 1000
|
199
|
+
x2 = x2 * width / 1000
|
200
|
+
y2 = y2 * height / 1000
|
201
|
+
draw.rectangle([(x1, y1), (x2, y2)], outline="blue", width=5)
|
202
|
+
draw.text((x1, y1), object_type, fill="red", font=font)
|
203
|
+
|
204
|
+
# Plot the image using matplotlib and save it as a PIL Image
|
205
|
+
buf = BytesIO()
|
206
|
+
plt.figure(figsize=(8, 8))
|
207
|
+
plt.imshow(img)
|
208
|
+
plt.axis("off") # Hide axes ticks
|
209
|
+
plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0)
|
210
|
+
buf.seek(0)
|
211
|
+
|
212
|
+
# Load the buffer into a PIL Image and ensure full loading into memory
|
213
|
+
pil_image = Image.open(buf)
|
214
|
+
pil_image.load() # Ensure full data is loaded from the buffer
|
215
|
+
|
216
|
+
# Save the image if save_path is provided
|
217
|
+
if save_path:
|
218
|
+
pil_image.save(save_path)
|
219
|
+
|
220
|
+
buf.close() # Close the buffer after use
|
221
|
+
|
222
|
+
return pil_image
|
223
|
+
|
123
224
|
class ImgHandler:
|
124
225
|
"""
|
125
226
|
ImgHandler is a class for handling image processing tasks using pretrained models.
|
@@ -1,5 +1,5 @@
|
|
1
1
|
hdl/__init__.py,sha256=GffnD0jLJdhkd-vo989v40N90sQbofkayRBwxc6TVhQ,72
|
2
|
-
hdl/_version.py,sha256=
|
2
|
+
hdl/_version.py,sha256=gmIfdIg5gEvfMxffRlk3KF_G7qsO5gyiwsJ6wEhCrTk,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=
|
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=
|
131
|
+
hdl/utils/llm/chat.py,sha256=B0OSX2Yc4OZUwRu-aHnZOW-knTeDqlzCP_UqlgnhMKA,24476
|
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=
|
136
|
+
hdl/utils/llm/vis.py,sha256=fuXdEFfUpW4KR7TKmIBEuMmo3ZdZxfd79a_qfTM0tPk,24739
|
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.
|
143
|
-
hjxdl-0.2.
|
144
|
-
hjxdl-0.2.
|
145
|
-
hjxdl-0.2.
|
142
|
+
hjxdl-0.2.88.dist-info/METADATA,sha256=jAJxvM6YypO5nHnLRSe8Zhi8QwOlvmsfMFi8H9IjeVg,836
|
143
|
+
hjxdl-0.2.88.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
|
144
|
+
hjxdl-0.2.88.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
|
145
|
+
hjxdl-0.2.88.dist-info/RECORD,,
|
File without changes
|
File without changes
|