news 2026/5/1 7:23:14

用modelscope运行grounding dino

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
用modelscope运行grounding dino

参考

grounding-dino-tiny · 模型库

不支持中文,试过了

Downloading Model from https://www.modelscope.cn to directory: C:\Users\njsgcs\.cache\modelscope\hub\models\IDEA-Research\grounding-dino-tiny Downloading Model from https://www.modelscope.cn to directory: C:\Users\njsgcs\.cache\modelscope\hub\models\IDEA-Research\grounding-dino-tiny 检测结果: Result 0: Boxes shape: torch.Size([3, 4]) e:\code\my_python_server\micromambavenv\lib\site-packages\transformers\models\grounding_dino\processing_grounding_dino.py:93: FutureWarning: The key `labels` is will return integer ids in `GroundingDinoProcessor.post_process_grounded_object_detection` output since v4.51.0. Use `text_labels` instead to retrieve string object names. warnings.warn(self.message, FutureWarning) Labels: ['a cat', 'a cat', 'a remote control'] Scores: tensor([0.4785, 0.4381, 0.4759], device='cuda:0') Text Labels: ['a cat', 'a cat', 'a remote control'] 结果已保存到 result.jpg
import requests import torch from PIL import Image, ImageDraw, ImageFont import numpy as np from modelscope import AutoProcessor, AutoModelForZeroShotObjectDetection def visualize_results(image, results, text_labels): """ 可视化检测结果 """ draw = ImageDraw.Draw(image) # 从结果中获取检测框、标签和分数 boxes = results[0]['boxes'] labels = results[0]['text_labels'] if 'text_labels' in results[0] else results[0]['labels'] scores = results[0]['scores'] for i in range(len(boxes)): box = boxes[i].cpu().numpy() score = scores[i].item() label = labels[i] # 现在是字符串,不需要 .item() # 只绘制置信度高的框 if score > 0.4: x0, y0, x1, y1 = box color = tuple(np.random.randint(0, 255, size=3).tolist()) draw.rectangle([x0, y0, x1, y1], outline=color, width=3) # 使用标签文本 text_to_draw = f"{label} {score:.2f}" # 绘制标签 font = ImageFont.load_default() if hasattr(font, "getbbox"): bbox = draw.textbbox((x0, y0), text_to_draw, font) else: w, h = draw.textsize(text_to_draw, font) bbox = (x0, y0, x0 + w, y0 + h) draw.rectangle(bbox, fill=color) draw.text((x0, y0), text_to_draw, fill="white", font=font) return image model_id = "IDEA-Research/grounding-dino-tiny" device = "cuda" if torch.cuda.is_available() else "cpu" processor = AutoProcessor.from_pretrained(model_id) model = AutoModelForZeroShotObjectDetection.from_pretrained(model_id).to(device) image_url = "http://images.cocodataset.org/val2017/000000039769.jpg" image = Image.open(requests.get(image_url, stream=True).raw) # Check for cats and remote controls # VERY important: text queries need to be lowercased + end with a dot text = "a cat. a remote control." inputs = processor(images=image, text=text, return_tensors="pt").to(device) with torch.no_grad(): outputs = model(**inputs) # 使用正确的参数名 results = processor.post_process_grounded_object_detection( outputs, input_ids=inputs.input_ids, threshold=0.4, # 使用 threshold 而不是 box_threshold text_threshold=0.3, target_sizes=[image.size[::-1]] ) # 打印结果 print("检测结果:") for i, result in enumerate(results): print(f"Result {i}:") print(f" Boxes shape: {result['boxes'].shape}") print(f" Labels: {result['labels']}") print(f" Scores: {result['scores']}") print(f" Text Labels: {result.get('text_labels', 'N/A')}") # 可视化结果 result_image = visualize_results(image.copy(), results, text) result_image.save("result.jpg") print("结果已保存到 result.jpg")

想让它识别点赞和收藏按钮识别不出来,效果很拉

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/22 1:42:59

终极Windows网盘图标清理工具:一键告别杂乱界面

还在为Windows"此电脑"中密密麻麻的网盘图标而烦恼吗?这些多余的图标不仅影响视觉美观,还会降低工作效率。现在,通过这款专业的网盘图标删除器,您可以在几分钟内彻底清理这些烦人的图标,恢复清爽的桌面体验。…

作者头像 李华
网站建设 2026/5/1 6:12:01

5个步骤让微信小助手无障碍功能真正服务于视障用户

5个步骤让微信小助手无障碍功能真正服务于视障用户 【免费下载链接】WeChatPlugin-MacOS 微信小助手 项目地址: https://gitcode.com/gh_mirrors/we/WeChatPlugin-MacOS 在数字化时代,无障碍技术正成为连接不同能力用户的重要桥梁。微信小助手通过深度整合Vo…

作者头像 李华
网站建设 2026/4/23 0:56:15

Spring Boot 3终极配置优化与性能调优完整指南

Spring Boot 3终极配置优化与性能调优完整指南 【免费下载链接】mybatis-3 MyBatis SQL mapper framework for Java 项目地址: https://gitcode.com/gh_mirrors/my/mybatis-3 Spring Boot 3作为Java生态中备受推崇的微服务框架,其配置优化和性能调优直接影响…

作者头像 李华
网站建设 2026/5/1 7:21:56

如何构建可靠的自动化部署:GitHub Actions完整解决方案

如何构建可靠的自动化部署:GitHub Actions完整解决方案 【免费下载链接】actions-gh-pages GitHub Actions for GitHub Pages 🚀 Deploy static files and publish your site easily. Static-Site-Generators-friendly. 项目地址: https://gitcode.com…

作者头像 李华
网站建设 2026/4/23 17:35:23

Intel RealSense D435i深度相机嵌入式部署实战速成指南

Intel RealSense D435i深度相机嵌入式部署实战速成指南 【免费下载链接】librealsense Intel RealSense™ SDK 项目地址: https://gitcode.com/GitHub_Trending/li/librealsense 深度感知技术正推动嵌入式视觉应用的革新,Intel RealSense D435i深度相机凭借其…

作者头像 李华
网站建设 2026/5/1 7:20:31

Protel99SE安装与多页原理图设计准备指南

Protel99SE 安装避坑指南与多页原理图设计实战准备你有没有试过打开一个二十年前的电路项目,结果发现它用的是.ddb文件?如果你在维修老设备、接手遗留项目,或者只是想重温经典 EDA 工具的操作逻辑,那Protel99SE很可能就是你要面对…

作者头像 李华