from transformers import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor from qwen_vl_utils import process_vision_info def load_llm(): # default: Load the model on the available device(s) model = Qwen2_5_VLForConditionalGeneration.from_pretrained( "Qwen/Qwen2.5-VL-3B-Instruct", torch_dtype="auto", device_map="auto" ) min_pixels = 256*28*28 max_pixels = 1280*28*28 processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-3B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels) return model,processor import cv2 import base64 from PIL import Image from io import BytesIO def check_gender(model,processor,img_list,test_image): content = [ {"type": "text", "text": "Both of the images belong to same person. Identify the gender and respond only Male or Female"}, ] # Load image and crop face img = cv2.imread(test_image) for image in img_list[:3]: x1, y1, x2, y2 = image['bbox'] face_crop = img[y1:y2, x1:x2] # Encode to JPEG format in memory _, buffer = cv2.imencode('.jpg', face_crop) # Convert to Base64 face_base64 = base64.b64encode(buffer).decode('utf-8') image_bytes = base64.b64decode(face_base64) image_64 = Image.open(BytesIO(image_bytes)).convert("RGB") content.append({"type": "image", "image": image_64}) messages = [ { "role": "user", "content": content } ] # Preparation for inference text = processor.apply_chat_template( messages, tokenize=False, add_generation_prompt=True ) image_inputs, video_inputs = process_vision_info(messages) inputs = processor( text=[text], images=image_inputs, videos=video_inputs, padding=True, return_tensors="pt", ) inputs = inputs.to("cuda") # Inference generated_ids = model.generate(**inputs, max_new_tokens=128) generated_ids_trimmed = [ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids) ] output_text = processor.batch_decode( generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False ) print(output_text) return output_text def visual_llm_gender(unique_chars,predictions,test_image): model,processor = load_llm() # for panel in predictions.faces: # print(panel) gender_list = [] for i in range(unique_chars): print(i) new_panels = [p for p in predictions.faces if p['char_id'] == i] print(new_panels) gender = check_gender(model,processor,new_panels,test_image) gender_list.append(gender) return gender_list