发布时间:2026/7/14 0:02:17
Florence-2 多视觉任务实战:检测、描述、OCR、区域理解与分割提示
Florence-2 多视觉任务实战检测、描述、OCR、区域理解与分割提示这篇教程根据我复现 Florence-2 多任务推理流程时整理集中演示同一个模型在目标检测、图像描述、短语定位、开放词汇检测、OCR 和区域理解任务上的用法。Florence-2 的价值在于统一任务接口。只要切换任务 token 和输入文本就可以覆盖多个视觉理解任务适合作为视觉大模型能力测试模板。本文会重点跑通以下流程安装 Florence-2 推理依赖并准备示例图片封装通用推理函数和可视化函数运行图像描述、目标检测和短语定位运行开放词汇检测、密集区域描述和 OCR通过交互框完成区域类别与区域描述任务如果你正在系统学习目标检测、实例分割、OCR、多目标跟踪或视觉大模型建议收藏本文配套 notebook、示例图片和运行环境说明后续会继续整理。如果环境配置卡住可以在评论区说明具体报错。 文章目录Florence-2 多视觉任务实战检测、描述、OCR、区域理解与分割提示⚙️ 环境准备 加载 Florence-2 预训练模型 运行 Florence-2 多任务推理 区域交互输入 区域类别与区域描述 小结 同系列教程汇总⚙️ 环境准备先检查 GPU 运行环境并准备后续训练和推理依赖。建议优先使用 Colab GPU 或本地 NVIDIA GPU 环境。!nvidia-smi!pip install-q transformers timm einops supervision jupyter_bbox_widgetimportos HOMEos.getcwd()print(HOME:,HOME)!mkdir-p{HOME}/data# 请从数据集后台下载示例图片和 OCR 图片并放到 {HOME}/data 目录。# 默认文件名dog.jpeg、dog-2.jpeg、dog-3.jpeg、dog-4.jpeg、license_plate_1.jpg、handwritten-text.jpg 加载 Florence-2 预训练模型加载模型和 processor 后后面的任务都通过任务 token 切换。importtorchfromtransformersimportAutoModelForCausalLM,AutoProcessor# CHECKPOINT microsoft/Florence-2-base-ftCHECKPOINTmicrosoft/Florence-2-large-ftDEVICEtorch.device(cudaiftorch.cuda.is_available()elsecpu)modelAutoModelForCausalLM.from_pretrained(CHECKPOINT,trust_remote_codeTrue,revisionrefs/pr/38).to(DEVICE)processorAutoProcessor.from_pretrained(CHECKPOINT,trust_remote_codeTrue,revisionrefs/pr/38) 运行 Florence-2 多任务推理Florence-2 的不同任务共用一套run_inference入口区别主要是 task 和 text。# title Wrap annotation code into utility functionimportsupervisionassvfromPILimportImagedefannotate_image(image:Image,detections:sv.Detections)-Image:根据 detections 在图像上绘制 mask、检测框和标签。text_scalesv.calculate_optimal_text_scale(resolution_whimage.size)thicknesssv.calculate_optimal_line_thickness(resolution_whimage.size)ifdetections.maskisnotNone:mask_annotatorsv.MaskAnnotator(color_lookupsv.ColorLookup.INDEX)imagemask_annotator.annotate(image,detections)else:box_annotatorsv.BoxAnnotator(color_lookupsv.ColorLookup.INDEX,thicknessthickness)imagebox_annotator.annotate(image,detections)label_annotatorsv.LabelAnnotator(color_lookupsv.ColorLookup.INDEX,text_colorsv.Color.BLACK,text_scaletext_scale,text_thicknessthickness-1,smart_positionTrue)imagelabel_annotator.annotate(image,detections)returnimage# title Example object detection (OB) inferenceimportsupervisionassvfromPILimportImage imageImage.open(f{HOME}/data/dog-3.jpeg)textODtaskODinputsprocessor(texttext,imagesimage,return_tensorspt).to(DEVICE)generated_idsmodel.generate(input_idsinputs[input_ids],pixel_valuesinputs[pixel_values],max_new_tokens1024,num_beams3)generated_textprocessor.batch_decode(generated_ids,skip_special_tokensFalse)[0]resultprocessor.post_process_generation(generated_text,tasktask,image_sizeimage.size)detectionssv.Detections.from_vlm(sv.VLM.FLORENCE_2,resultresult,resolution_whimage.size)imageannotate_image(imageimage,detectionsdetections)image.thumbnail((800,800))image# title Wrap inference code into utility functiondefrun_inference(image:Image,task:str,text:str):prompttasktext inputsprocessor(textprompt,imagesimage,return_tensorspt).to(DEVICE)generated_idsmodel.generate(input_idsinputs[input_ids],pixel_valuesinputs[pixel_values],max_new_tokens1024,num_beams3)generated_textprocessor.batch_decode(generated_ids,skip_special_tokensFalse)[0]returnprocessor.post_process_generation(generated_text,tasktask,image_sizeimage.size)# title Example CAPTION inferenceimportsupervisionassvfromPILimportImage imageImage.open(f{HOME}/data/dog-3.jpeg)taskCAPTIONresponserun_inference(imageimage,tasktask)response# title Example DETAILED_CAPTION inferenceimportsupervisionassvfromPILimportImage imageImage.open(f{HOME}/data/dog-3.jpeg)taskDETAILED_CAPTIONresponserun_inference(imageimage,tasktask)response# title Example MORE_DETAILED_CAPTION inferenceimportsupervisionassvfromPILimportImage imageImage.open(f{HOME}/data/dog-3.jpeg)taskMORE_DETAILED_CAPTIONresponserun_inference(imageimage,tasktask)response# title Example CAPTION_TO_PHRASE_GROUNDING inferenceimportsupervisionassvfromPILimportImage imageImage.open(f{HOME}/data/dog-3.jpeg)taskCAPTION_TO_PHRASE_GROUNDINGtexttailresultrun_inference(imageimage,tasktask,texttext)detectionssv.Detections.from_vlm(sv.VLM.FLORENCE_2,resultresult,resolution_whimage.size)imageannotate_image(imageimage,detectionsdetections)image.thumbnail((800,800))image# title Example DETAILED_CAPTION CAPTION_TO_PHRASE_GROUNDING combo inferenceimportsupervisionassvfromPILimportImage imageImage.open(f{HOME}/data/dog-3.jpeg)taskDETAILED_CAPTIONresponserun_inference(imageimage,tasktask)textresponse[task]taskCAPTION_TO_PHRASE_GROUNDINGresultrun_inference(imageimage,tasktask,texttext)detectionssv.Detections.from_vlm(sv.VLM.FLORENCE_2,resultresult,resolution_whimage.size)imageannotate_image(imageimage,detectionsdetections)image.thumbnail((800,800))print(text)image# title Example OPEN_VOCABULARY_DETECTION inferenceimportsupervisionassvfromPILimportImage imageImage.open(f{HOME}/data/dog-3.jpeg)taskOPEN_VOCABULARY_DETECTIONtextfingerresultrun_inference(imageimage,tasktask,texttext)detectionssv.Detections.from_vlm(sv.VLM.FLORENCE_2,resultresult,resolution_whimage.size)imageannotate_image(imageimage,detectionsdetections)image.thumbnail((800,800))image# title Example DENSE_REGION_CAPTION inferenceimportsupervisionassvfromPILimportImage imageImage.open(f{HOME}/data/dog-3.jpeg)taskDENSE_REGION_CAPTIONresultrun_inference(imageimage,tasktask)detectionssv.Detections.from_vlm(sv.VLM.FLORENCE_2,resultresult,resolution_whimage.size)imageannotate_image(imageimage,detectionsdetections)image.thumbnail((800,800))image# title Example REGION_PROPOSAL inferenceimportsupervisionassvfromPILimportImage imageImage.open(f{HOME}/data/dog-3.jpeg)taskREGION_PROPOSALresultrun_inference(imageimage,tasktask)detectionssv.Detections.from_vlm(sv.VLM.FLORENCE_2,resultresult,resolution_whimage.size)imageannotate_image(imageimage,detectionsdetections)image.thumbnail((800,800))image# title Example OCR_WITH_REGION inferenceimportsupervisionassvfromPILimportImage imageImage.open(f{HOME}/data/license_plate_1.jpg)taskOCR_WITH_REGIONresultrun_inference(imageimage,tasktask)detectionssv.Detections.from_vlm(sv.VLM.FLORENCE_2,resultresult,resolution_whimage.size)imageannotate_image(imageimage,detectionsdetections)image.thumbnail((800,800))imageimportsupervisionassvfromPILimportImage imageImage.open(f{HOME}/data/handwritten-text.jpg)taskOCR_WITH_REGIONresultrun_inference(imageimage,tasktask)detectionssv.Detections.from_vlm(sv.VLM.FLORENCE_2,resultresult,resolution_whimage.size)imageannotate_image(imageimage,detectionsdetections)image.thumbnail((800,800))image# title Example OCR inferenceimportsupervisionassvfromPILimportImage imageImage.open(f{HOME}/data/handwritten-text.jpg)taskOCRresponserun_inference(imageimage,tasktask)response[task]# title Example REFERRING_EXPRESSION_SEGMENTATION inferenceimportsupervisionassvfromPILimportImage imageImage.open(f{HOME}/data/dog-3.jpeg)taskREFERRING_EXPRESSION_SEGMENTATIONtextmanresultrun_inference(imageimage,tasktask,texttext)detectionssv.Detections.from_vlm(sv.VLM.FLORENCE_2,resultresult,resolution_whimage.size)imageannotate_image(imageimage,detectionsdetections)image.thumbnail((800,800))image 区域交互输入区域任务需要先把交互框转换成 Florence-2 使用的loc_*坐标文本。!pip install-q jupyter_bbox_widgetimportbase64defencode_image(filepath):withopen(filepath,rb)asf:image_bytesf.read()encodedstr(base64.b64encode(image_bytes),utf-8)returndata:image/jpg;base64,encodedIS_COLABTrueIMAGE_PATHf{HOME}/data/dog-3.jpegifIS_COLAB:fromgoogle.colabimportoutput output.enable_custom_widget_manager()fromjupyter_bbox_widgetimportBBoxWidget widgetBBoxWidget()widget.imageencode_image(IMAGE_PATH)widgetwidget.bboxesimportnumpyasnp# 如果没有在上方图片中手动画框就使用默认框default_box{x:243,y:733,width:52,height:211,label:}boxwidget.bboxes[0]ifwidget.bboxeselsedefault_box boxnp.array([box[x],box[y],box[x]box[width],box[y]box[height]])imageImage.open(IMAGE_PATH)w,himage.size boxbox/np.array([w,h,w,h])boxbox*1000boxbox.astype(np.int32)boxtext.join([floc_{coordinate}forcoordinateinbox])text 区域类别与区域描述给定区域后模型可以输出类别或区域描述。# title Example REGION_TO_CATEGORY inferenceimportsupervisionassvfromPILimportImage taskREGION_TO_CATEGORYimageImage.open(IMAGE_PATH)resultrun_inference(imageimage,tasktask,texttext)detectionssv.Detections.from_vlm(sv.VLM.FLORENCE_2,resultresult,resolution_whimage.size)imageannotate_image(imageimage,detectionsdetections)image.thumbnail((800,800))image# title Example REGION_TO_DESCRIPTION inferenceimportsupervisionassvfromPILimportImage taskREGION_TO_DESCRIPTIONimageImage.open(IMAGE_PATH)resultrun_inference(imageimage,tasktask,texttext)detectionssv.Detections.from_vlm(sv.VLM.FLORENCE_2,resultresult,resolution_whimage.size)imageannotate_image(imageimage,detectionsdetections)image.thumbnail((800,800))image 小结Florence-2 多任务推理的核心是任务 token。实际使用时可以先封装统一的run_inference再按任务类型把输出解析为文本、检测框、mask 或 OCR 结果。这一类 notebook 建议按“先环境、再数据、再单样例、最后批量推理”的顺序复现。遇到报错时优先检查 GPU、依赖版本、数据集目录和模型权重路径。后续我会继续按源项目顺序整理同系列中的目标检测、实例分割、OCR、多目标跟踪和视觉大模型教程。 同系列教程汇总Google Gemini 3.5 Flash 零样本目标检测教程从提示词到可视化结果GLM-OCR 文档识别实战教程从验证码、公式到车牌 OCRRF-DETR ByteTrack 多目标跟踪实战教程从命令行到 Python 视频轨迹可视化SAM 3 图像分割实战教程文本、框和点提示的多种分割方式Florence-2 多视觉任务实战检测、描述、OCR、区域理解与分割提示-本文

相关新闻

2026年6月AI模型实战排名:仅剩72小时可获取的闭源模型性能对比表(附Hugging Face未公开的量化实测数据)
2026/7/14 0:02:17

2026年6月AI模型实战排名:仅剩72小时可获取的闭源模型性能对比表(附Hugging Face未公开的量化实测数据)

更多请点击: https://codechina.net 第一章:2026年6月AI模型实战排名总览 截至2026年6月,AI模型在真实业务场景中的综合表现已超越单纯基准测试指标,转向以推理效率、多模态协同能力、边缘部署兼容性及合规可解释性为核心维度的实…

阅读更多
AD74412R与STM32F407ZG在工业信号采集中的高效应用
2026/7/14 0:02:17

AD74412R与STM32F407ZG在工业信号采集中的高效应用

1. 为什么选择AD74412R与STM32F407ZG组合?在工业控制和嵌入式系统设计中,信号采集与处理的实时性、精度和稳定性往往是核心挑战。AD74412R作为ADI公司推出的四通道软件可配置I/O解决方案,与STMicroelectronics的STM32F407ZG高性能微控制器组合…

阅读更多
Perplexity vs ChatGPT vs Claude:实测127组复杂查询任务,谁才是真正可靠的“事实型AI助手”?
2026/7/14 0:02:17

Perplexity vs ChatGPT vs Claude:实测127组复杂查询任务,谁才是真正可靠的“事实型AI助手”?

更多请点击: https://codechina.net 第一章:Perplexity 怎么用 Perplexity 是衡量语言模型预测能力的核心指标,数值越低表示模型对文本序列的不确定性越小、预测越精准。它本质上是交叉熵损失的指数形式,计算公式为:…

阅读更多
Pandas多维聚合实战:从语法到银行级工程化落地
2026/7/14 3:02:18

Pandas多维聚合实战:从语法到银行级工程化落地

1. 项目概述:为什么多维聚合不是“加个groupby”就能搞定的事我在银行数据平台组干了八年,从最早用SQL写几十行嵌套子查询做客户分层,到现在每天在Jupyter里调试pandas的agg链式调用,踩过的坑比写的代码还多。今天这篇讲的“多维聚…

阅读更多
AI图像生成工具:狮子嘴巴高精度重建与批量生成实践
2026/7/14 3:02:18

AI图像生成工具:狮子嘴巴高精度重建与批量生成实践

这次我们来看一个名为"The Lions Mouth FanRecreation [GFC]"的项目,这是一个基于AI技术的图像生成工具,专门用于重现和创作狮子嘴巴相关的视觉内容。项目采用先进的生成模型,支持文生图、图生图等多种创作模式,特别适合…

阅读更多
TPS61170与PIC24FJ256GB110构建高效DC-DC升压系统
2026/7/14 3:02:18

TPS61170与PIC24FJ256GB110构建高效DC-DC升压系统

1. 项目背景与核心器件选型在工业控制、医疗设备和实验室仪器等领域,经常需要将低压直流电源转换为高压直流电源。传统方案采用分立元件搭建,存在设计复杂、效率低下和体积庞大等问题。TPS61170作为TI推出的高压升压转换芯片,配合PIC24FJ256G…

阅读更多
机器视觉(八):从特征提取到智能感知——关键点与描述子实战
2026/7/14 3:02:18

机器视觉(八):从特征提取到智能感知——关键点与描述子实战

1. 图像特征提取的基本概念第一次接触机器视觉时,最让我困惑的就是计算机如何"看懂"图像。后来发现关键在于特征提取——就像教小朋友认动物时,我们会说"猫咪有尖耳朵、长尾巴"。在计算机视觉中,这些"尖耳朵"就…

阅读更多
国产操作系统下Zabbix Agent部署与优化指南
2026/7/14 3:02:18

国产操作系统下Zabbix Agent部署与优化指南

1. 国产操作系统环境下的Zabbix Agent部署指南 在国产化替代的大背景下,麒麟、统信UOS和openEuler作为主流国产操作系统,其监控体系的建设成为企业IT运维的关键环节。Zabbix作为成熟的监控解决方案,其Agent端在这些系统上的部署存在诸多技术…

阅读更多
4K 60帧视频剪辑工作流:从素材验证到稳定输出的全流程实践
2026/7/14 2:02:18

4K 60帧视频剪辑工作流:从素材验证到稳定输出的全流程实践

这类视频制作项目最值得先看的不是标题里的分辨率参数,而是实际落地时的工作流能不能在普通设备上稳定跑起来。从标题看,这是一个 4K 60 帧的剪辑任务,涉及香港启德场景,但真正决定项目能否顺利交付的,往往是素材管理、…

阅读更多
智慧树刷课插件:5分钟实现自动化学习的智能助手
2026/7/12 0:01:57

智慧树刷课插件:5分钟实现自动化学习的智能助手

智慧树刷课插件:5分钟实现自动化学习的智能助手 【免费下载链接】zhihuishu 智慧树刷课插件,自动播放下一集、1.5倍速度、无声 项目地址: https://gitcode.com/gh_mirrors/zh/zhihuishu 智慧树刷课插件是一款专为智慧树在线教育平台设计的Chrome浏…

阅读更多
Steam创意工坊下载器WorkshopDL:跨平台游戏模组获取的终极解决方案
2026/7/12 0:01:57

Steam创意工坊下载器WorkshopDL:跨平台游戏模组获取的终极解决方案

Steam创意工坊下载器WorkshopDL:跨平台游戏模组获取的终极解决方案 【免费下载链接】WorkshopDL WorkshopDL - The Best Steam Workshop Downloader 项目地址: https://gitcode.com/gh_mirrors/wo/WorkshopDL 你是否在GOG或Epic Games Store购买了心仪的游戏…

阅读更多
办公自动化实战:5个免费工具构建合同处理流水线
2026/7/13 17:34:38

办公自动化实战:5个免费工具构建合同处理流水线

1. 这不是“又一个工具包”,而是一套经过237次真实场景验证的效率组合拳“2026.04.28实用教程工具分享”这个标题乍看平平无奇,像极了你邮箱里被自动归入“促销/订阅”文件夹的那类通知——但如果你真把它当普通更新忽略,接下来半年里&#x…

阅读更多
DeepSeek提示词效果暴跌预警:当LLM置信度低于0.62时,必须启用的4层动态重写机制
2026/7/14 0:02:17

DeepSeek提示词效果暴跌预警:当LLM置信度低于0.62时,必须启用的4层动态重写机制

更多请点击: https://codechina.net 第一章:DeepSeek提示词效果暴跌预警:当LLM置信度低于0.62时,必须启用的4层动态重写机制 当DeepSeek-R1或DeepSeek-V3模型返回的token级置信度(logits softmax归一化后最大概率值&a…

阅读更多
Perplexity vs ChatGPT vs Claude:实测127组复杂查询任务,谁才是真正可靠的“事实型AI助手”?
2026/7/14 0:02:17

Perplexity vs ChatGPT vs Claude:实测127组复杂查询任务,谁才是真正可靠的“事实型AI助手”?

更多请点击: https://codechina.net 第一章:Perplexity 怎么用 Perplexity 是衡量语言模型预测能力的核心指标,数值越低表示模型对文本序列的不确定性越小、预测越精准。它本质上是交叉熵损失的指数形式,计算公式为:…

阅读更多
AD74412R与STM32F407ZG在工业信号采集中的高效应用
2026/7/14 0:02:17

AD74412R与STM32F407ZG在工业信号采集中的高效应用

1. 为什么选择AD74412R与STM32F407ZG组合?在工业控制和嵌入式系统设计中,信号采集与处理的实时性、精度和稳定性往往是核心挑战。AD74412R作为ADI公司推出的四通道软件可配置I/O解决方案,与STMicroelectronics的STM32F407ZG高性能微控制器组合…

阅读更多
基于Dify与DeepSeek构建私有知识库问答系统实战指南
2026/7/13 21:11:56

基于Dify与DeepSeek构建私有知识库问答系统实战指南

在业务中快速构建一个能理解私有文档、准确回答专业问题的智能助手,是很多开发团队面临的共同挑战。传统方案往往需要从零开始搭建复杂的 RAG(检索增强生成)系统,涉及文档解析、向量化、检索、大模型调用等多个环节,整…

阅读更多
FAE放射组学分析工具:医学影像特征探索的完整解决方案
2026/7/13 1:50:37

FAE放射组学分析工具:医学影像特征探索的完整解决方案

FAE放射组学分析工具:医学影像特征探索的完整解决方案 【免费下载链接】FAE FeAture Explorer 项目地址: https://gitcode.com/gh_mirrors/fae/FAE 你是否曾经面对海量医学影像数据感到无从下手?想要从CT、MRI等影像中提取有价值的定量特征&#…

阅读更多
DesktopNaotu:你的终极离线思维导图解决方案,告别网络依赖!
2026/7/13 13:09:00

DesktopNaotu:你的终极离线思维导图解决方案,告别网络依赖!

DesktopNaotu:你的终极离线思维导图解决方案,告别网络依赖! 【免费下载链接】DesktopNaotu 桌面版脑图 (百度脑图离线版,思维导图) 跨平台支持 Windows/Linux/Mac OS. (A cross-platform multilingual Mind Map Tool) 项目地址:…

阅读更多