تحليل الصور وفهم المحتوى البصري
يعتمد محرك ONYX على نموذج nlpconnect/vit-gpt2-image-captioning لإنشاء وصف نصي دقيق للصور المرفقة، مما يسهل على الذكاء الاصطناعي فهم السياق البصري.
nlpconnect/vit-gpt2-image-captioning
torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def predict_image(image_data_base64: str) -> str:
try:
# فك تشفير الصورة من base64
img_bytes = base64.decodebytes(image_data_base64.encode())
image = Image.open(io.BytesIO(img_bytes))
# التأكد من صيغة الألوان RGB
if image.mode != "RGB":
image = image.convert("RGB")
# استخراج الميزات وتحويلها لموترات (Tensors)
pixel_values = v_feature_extractor(images=[image], return_tensors="pt").pixel_values.to(v_device)
# توليد الوصف النصي
output_ids = v_model.generate(pixel_values, max_length=16, num_beams=4)
preds = v_tokenizer.batch_decode(output_ids, skip_special_tokens=True)
return preds[0].strip()
except Exception:
return "صورة مرفقة"def predict_image(image_data_base64: str) -> str:
try:
# فك تشفير الصورة من base64
img_bytes = base64.decodebytes(image_data_base64.encode())
image = Image.open(io.BytesIO(img_bytes))
# التأكد من صيغة الألوان RGB
if image.mode != "RGB":
image = image.convert("RGB")
# استخراج الميزات وتحويلها لموترات (Tensors)
pixel_values = v_feature_extractor(images=[image], return_tensors="pt").pixel_values.to(v_device)
# توليد الوصف النصي
output_ids = v_model.generate(pixel_values, max_length=16, num_beams=4)
preds = v_tokenizer.batch_decode(output_ids, skip_special_tokens=True)
return preds[0].strip()
except Exception:
return "صورة مرفقة"تتم العملية عبر تحويل مصفوفة البكسلات إلى متجهات يفهمها النموذج (ViT)، ثم يقوم المولد النصي (GPT2) بصياغة الوصف المناسب.
Author: ONYX (2026)
Base Model: vit-gpt2-image-captioning