This commit is contained in:
Saifeddine ALOUI 2024-03-10 03:12:46 +01:00
parent 4206618bae
commit 0bbd14e6a2
2 changed files with 46 additions and 44 deletions

View File

@ -13,7 +13,7 @@ from lollms.config import InstallOption, TypedConfig, BaseConfig
from lollms.main_config import LOLLMSConfig
from lollms.paths import LollmsPaths
from lollms.binding import LLMBinding, BindingType
from lollms.utilities import PromptReshaper, PackageManager, discussion_path_to_url
from lollms.utilities import PromptReshaper, PackageManager, discussion_path_to_url, process_ai_output
from lollms.com import NotificationType, NotificationDisplayType
from lollms.client_session import Session, Client
import pkg_resources
@ -547,48 +547,7 @@ class AIPersonality:
ASCIIColors.yellow(prompt)
ASCIIColors.red(" *-*-*-*-*-*-*-*")
def process_ai_output(self, output, images, output_folder):
if not is_package_installed("cv2"):
install_package("cv2")
import cv2
images = [cv2.imread(img) for img in images]
# Find all bounding box entries in the output
bounding_boxes = re.findall(r'boundingbox\((\d+), ([^,]+), ([^,]+), ([^,]+), ([^,]+), ([^,]+)\)', output)
# Group bounding boxes by image index
image_boxes = {}
for box in bounding_boxes:
image_index = int(box[0])
if image_index not in image_boxes:
image_boxes[image_index] = []
image_boxes[image_index].append(box[1:])
# Process each image and its bounding boxes
for image_index, boxes in image_boxes.items():
# Get the corresponding image
image = images[image_index]
# Draw bounding boxes on the image
for box in boxes:
label, left, top, width, height = box
left, top, width, height = float(left), float(top), float(width), float(height)
x, y, w, h = int(left * image.shape[1]), int(top * image.shape[0]), int(width * image.shape[1]), int(height * image.shape[0])
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(image, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
# Save the modified image
output_path = Path(output_folder)/f"image_{image_index}.jpg"
cv2.imwrite(output_path, image)
# Remove bounding box text from the output
output = re.sub(r'boundingbox\([^)]+\)', '', output)
# Append img tags for the generated images
for image_index in image_boxes.keys():
url = discussion_path_to_url(Path(output_folder)/f"image_{image_index}.jpg")
output += f'\n<img src="{url}">'
return output
def fast_gen_with_images(self, prompt: str, images:list, max_generation_size: int=None, placeholders: dict = {}, sacrifice: list = ["previous_discussion"], debug: bool = False, callback=None, show_progress=False) -> str:
"""
@ -616,7 +575,7 @@ class AIPersonality:
"left, top: x,y coordinates of top-left box corner (0-1 scale)",
"width, height: box dimensions as fraction of image size",
"Coordinates have origin (0,0) at top-left, (1,1) at bottom-right.",
"For other queries, I will respond conversationally to the best of my abilities. Let me know if you have any other questions!",
"For other queries, I will respond conversationally to the best of my abilities.",
prompt
])
if debug == False:
@ -639,7 +598,7 @@ class AIPersonality:
gen = self.generate_with_images(prompt, images, max_generation_size, callback=callback, show_progress=show_progress).strip().replace("</s>", "").replace("<s>", "")
try:
gen = self.process_ai_output(gen, images, "/discussions/")
gen = process_ai_output(gen, images, "/discussions/")
except Exception as ex:
pass
if debug:

View File

@ -38,6 +38,49 @@ import git
import mimetypes
def process_ai_output(output, images, output_folder):
if not PackageManager.check_package_installed("cv2"):
PackageManager.install_package("opencv-python")
import cv2
images = [cv2.imread(str(img)) for img in images]
# Find all bounding box entries in the output
bounding_boxes = re.findall(r'boundingbox\((\d+), ([^,]+), ([^,]+), ([^,]+), ([^,]+), ([^,]+)\)', output)
# Group bounding boxes by image index
image_boxes = {}
for box in bounding_boxes:
image_index = int(box[0])
if image_index not in image_boxes:
image_boxes[image_index] = []
image_boxes[image_index].append(box[1:])
# Process each image and its bounding boxes
for image_index, boxes in image_boxes.items():
# Get the corresponding image
image = images[image_index]
# Draw bounding boxes on the image
for box in boxes:
label, left, top, width, height = box
left, top, width, height = float(left), float(top), float(width), float(height)
x, y, w, h = int(left * image.shape[1]), int(top * image.shape[0]), int(width * image.shape[1]), int(height * image.shape[0])
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(image, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
# Save the modified image
output_path = Path(output_folder)/f"image_{image_index}.jpg"
cv2.imwrite(str(output_path), image)
# Remove bounding box text from the output
output = re.sub(r'boundingbox\([^)]+\)', '', output)
# Append img tags for the generated images
for image_index in image_boxes.keys():
url = discussion_path_to_url(Path(output_folder)/f"image_{image_index}.jpg")
output += f'\n<img src="{url}">'
return output
def get_media_type(file_path):
"""
Determines the media type of a file based on its file extension.