demo-vit-v2 / ex.py
nhatipoglu's picture
add app files
8677efd
raw
history blame
2.82 kB
from PIL import Image
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
from qwen_vl_utils import process_vision_info
# %%
model = Qwen2VLForConditionalGeneration.from_pretrained(pretrained_model_name_or_path="Qwen/Qwen2-VL-2B-Instruct",
torch_dtype="auto",
device_map="auto")
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")
# %%
def rescale_image_dimensions(original_width, original_height, max_size=1000):
# Orijinal boyutlar 1000 pikselin üzerindeyse yeniden ölçeklendir
if original_width > max_size or original_height > max_size:
aspect_ratio = original_width / original_height
if aspect_ratio > 1: # Genişlik yükseklikten büyükse
scaled_width = max_size
scaled_height = int(max_size / aspect_ratio)
else: # Yükseklik genişlikten büyükse veya eşitse
scaled_height = max_size
scaled_width = int(max_size * aspect_ratio)
else:
# Orijinal boyutlar zaten uygun ise
scaled_width = original_width
scaled_height = original_height
return scaled_width, scaled_height
# %%
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "/home/nuh-hatipoglu/Desktop/NewMind/demo-vit/images/IMG_3644.JPG",
},
{"type": "text", "text": "Describe image"},
],
}
]
# %%%
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
# %%
image_inputs, video_inputs = process_vision_info(messages)
# %%
original_width, original_height = image_inputs[0].size
new_width, new_height = rescale_image_dimensions(original_width, original_height)
rescaled_image = image_inputs[0].resize((new_width, new_height), Image.Resampling.LANCZOS)
image_inputs = rescaled_image
#%%
image_inputs[0].show()
# %%
inputs = processor(text=[text],
images=image_inputs,
videos=video_inputs,
padding=True,
return_tensors="pt", )
inputs = inputs.to("cuda")
# %%
# Görseli aç
image_path = "your_image_path.jpg" # Görselin dosya yolu
image = Image.open(image_path)
# Orijinal boyutları al
original_width, original_height = image.size
# Yeni boyutları hesapla
new_width, new_height = rescale_image_dimensions(original_width, original_height)
# Görseli yeniden boyutlandır
rescaled_image = image.resize((new_width, new_height), Image.ANTIALIAS)
# Yeniden boyutlandırılmış görseli kaydet
rescaled_image.save("rescaled_" + image_path)
print(f"Görsel başarıyla yeniden boyutlandırıldı: {new_width}x{new_height}")