Unity MiniCPM-V 让引擎拥有视觉

2024-06-03 00:36

本文主要是介绍Unity MiniCPM-V 让引擎拥有视觉,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Unity MiniCPM-V 让引擎拥有视觉

  • 前言
  • 项目
    • Python环境布置
    • Unity场景布置
    • 代码编写
    • 添加并设置脚本
    • 总结
  • 鸣谢
  • AI提示

前言

新发布的MiniCPM-V,忍不住玩一下,可以让之前制作的语音助手拥有一定的视觉能力(不是OpenCV不行,而是AI更加符合一个助手所需要的观察力)。这个简单的小项目中我只实现了少量交互代码,大部分全部由ChatGPT 3.5完成,可以在文末链接查看对话记录。

AI视觉识别

项目

Python环境布置

参考官网配置: https://github.com/OpenBMB/MiniCPM-V/tree/main

除了官网的配置还要安装一下flask库

pip install flask

将脚本放到根目录下,并运行

from flask import Flask, request, jsonify
from chat import MiniCPMVChat, img2base64
import torch
import json
import base64
from io import BytesIO
from PIL import Image# Initialize Flask app
app = Flask(__name__)# Initialize the chat model
torch.manual_seed(0)
chat_model = MiniCPMVChat('openbmb/MiniCPM-Llama3-V-2_5')def pil_image_to_base64(img):buffer = BytesIO()img.save(buffer, format="PNG")return base64.b64encode(buffer.getvalue()).decode()@app.route('/analyze', methods=['POST'])
def analyze_image():try:data = request.jsonprint(f"Received data: {data}")  # Debug: Log the received dataimage_base64 = data.get('image')question = data.get('question')if not image_base64 or not question:return jsonify({'error': 'Missing image or question'}), 400# Decode base64 imageimage_data = base64.b64decode(image_base64)image = Image.open(BytesIO(image_data))im_64 = pil_image_to_base64(image)  # Convert PIL image to base64 string# Prepare the inputs for the modelmsgs = [{"role": "user", "content": question}]inputs = {"image": im_64, "question": json.dumps(msgs)}print(f"Inputs for model: {inputs}")  # Debug: Log the inputs for the model# Get the answer from the modelanswer = chat_model.chat(inputs)# Prepare the responseresponse = {'answer': answer,'context': msgs}return jsonify(response)except Exception as e:print(f"Error: {str(e)}")  # Debug: Log any error that occursreturn jsonify({'error': str(e)}), 500if __name__ == '__main__':app.run(host='127.0.0.1', port=5000)

配置环境并运行Python脚本

Unity场景布置

Unity场景布置

代码编写

创建并挂载ImageAnalyzer.cs脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.Text;
using Newtonsoft.Json;
using UnityEngine.UI;public class Response
{[JsonProperty("answer")]public string Answer { get; set; }[JsonProperty("context")]public List<ContextItem> Context { get; set; }
}public class ContextItem
{[JsonProperty("role")]public string Role { get; set; }[JsonProperty("content")]public string Content { get; set; }
}
public class ImageAnalyzer : MonoBehaviour
{[SerializeField] private string serverUrl = "http://your_server_ip:5000/analyze";[SerializeField] private int webcamWidth = 1280;[SerializeField] private int webcamHeight = 720;[SerializeField] private int webcamFPS = 30;[SerializeField] private WebCamTexture webCamTexture;private Texture2D snap;public Text tipText;public Button clickButton;public RawImage rawImage;private void Start(){// Start the webcamwebCamTexture = new WebCamTexture(webcamWidth, webcamHeight);webCamTexture.Play();tipText.text = "请点击按钮询问";clickButton.interactable = true;rawImage.texture = webCamTexture;}private void Update(){if (Input.GetKeyDown(KeyCode.Q)){Debug.Log("按下了按钮");StartCoroutine(AnalyzeImageFromWebcam("用简短易懂的语言告诉我这张图上有什么?"));}}public void ClickButtonFunction(){tipText.text = "请等待。。。";clickButton.interactable = false;StartCoroutine(AnalyzeImageFromWebcam("用简短易懂的语言告诉我这张图上有什么?"));}public IEnumerator AnalyzeImageFromWebcam(string question){// Wait until the webcam is readyyield return new WaitUntil(() => webCamTexture.didUpdateThisFrame);// Dispose of the previous snap texture if it existsif (snap != null){Destroy(snap);}// Get the current frame from the webcamsnap = new Texture2D(webCamTexture.width, webCamTexture.height);snap.SetPixels(webCamTexture.GetPixels());snap.Apply();// Convert the image to base64string base64Image = ConvertTextureToBase64(snap);// Analyze the imageyield return StartCoroutine(AnalyzeImage(base64Image, question));}private string ConvertTextureToBase64(Texture2D texture){byte[] imageBytes = texture.EncodeToPNG();return System.Convert.ToBase64String(imageBytes);}public IEnumerator AnalyzeImage(string base64Image, string question){var formData = new Dictionary<string, string>{{ "image", base64Image },{ "question", question }};string jsonData = JsonConvert.SerializeObject(formData);byte[] postData = Encoding.UTF8.GetBytes(jsonData);UnityWebRequest request = new UnityWebRequest(serverUrl, "POST");request.uploadHandler = new UploadHandlerRaw(postData);request.downloadHandler = new DownloadHandlerBuffer();request.SetRequestHeader("Content-Type", "application/json");yield return request.SendWebRequest();if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError){Debug.LogError(request.error);tipText.text = request.error;clickButton.interactable = true;}else{string jsonResponse = request.downloadHandler.text;Debug.Log("Response JSON: " + jsonResponse); // Log the response for debugging// Process the JSON response here// Deserialize the JSON response to the Response objectResponse response = JsonConvert.DeserializeObject<Response>(jsonResponse);// Use the response dataDebug.Log("Answer: " + response.Answer);foreach (var contextItem in response.Context){Debug.Log($"Role: {contextItem.Role}, Content: {contextItem.Content}");}tipText.text = response.Answer;clickButton.interactable = true;}request.Dispose();}void OnDestroy(){// Stop the webcamif (webCamTexture != null){webCamTexture.Stop();webCamTexture = null;}// Dispose of the snap textureif (snap != null){Destroy(snap);snap = null;}}
}

添加并设置脚本

在按钮上添加一下事件,给ImageAnalyzer物体赋值即可
按钮添加事件

总结

除了交互部分,一行代码没写,这也是未来趋势,提前适应一下。

鸣谢

https://chatgpt.com/
https://github.com/OpenBMB/MiniCPM-V/tree/main

AI提示

点击下方链接可以查看我和ChatGPT 3.5的聊天记录并了解详细开发过程
Image Q&A Service

这篇关于Unity MiniCPM-V 让引擎拥有视觉的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1025539

相关文章

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

MySQL 存储引擎 MyISAM详解(最新推荐)

《MySQL存储引擎MyISAM详解(最新推荐)》使用MyISAM存储引擎的表占用空间很小,但是由于使用表级锁定,所以限制了读/写操作的性能,通常用于中小型的Web应用和数据仓库配置中的只读或主要... 目录mysql 5.5 之前默认的存储引擎️‍一、MyISAM 存储引擎的特性️‍二、MyISAM 的主

MySQL常见的存储引擎和区别说明

《MySQL常见的存储引擎和区别说明》MySQL支持多种存储引擎,如InnoDB、MyISAM、MEMORY、Archive、CSV和Blackhole,每种引擎有其特点和适用场景,选择存储引擎时需根... 目录mysql常见的存储引擎和区别说明1. InnoDB2. MyISAM3. MEMORY4. A

MySQL InnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据

《MySQLInnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据》mysql的ibdata文件被误删、被恶意修改,没有从库和备份数据的情况下的数据恢复,不能保证数据库所有表数据... 参考:mysql Innodb表空间卸载、迁移、装载的使用方法注意!此方法只适用于innodb_fi

Python基于火山引擎豆包大模型搭建QQ机器人详细教程(2024年最新)

《Python基于火山引擎豆包大模型搭建QQ机器人详细教程(2024年最新)》:本文主要介绍Python基于火山引擎豆包大模型搭建QQ机器人详细的相关资料,包括开通模型、配置APIKEY鉴权和SD... 目录豆包大模型概述开通模型付费安装 SDK 环境配置 API KEY 鉴权Ark 模型接口Prompt

4B参数秒杀GPT-3.5:MiniCPM 3.0惊艳登场!

​ 面壁智能 在 AI 的世界里,总有那么几个时刻让人惊叹不已。面壁智能推出的 MiniCPM 3.0,这个仅有4B参数的"小钢炮",正在以惊人的实力挑战着 GPT-3.5 这个曾经的AI巨人。 MiniCPM 3.0 MiniCPM 3.0 MiniCPM 3.0 目前的主要功能有: 长上下文功能:原生支持 32k 上下文长度,性能完美。我们引入了

速了解MySQL 数据库不同存储引擎

快速了解MySQL 数据库不同存储引擎 MySQL 提供了多种存储引擎,每种存储引擎都有其特定的特性和适用场景。了解这些存储引擎的特性,有助于在设计数据库时做出合理的选择。以下是 MySQL 中几种常用存储引擎的详细介绍。 1. InnoDB 特点: 事务支持:InnoDB 是一个支持 ACID(原子性、一致性、隔离性、持久性)事务的存储引擎。行级锁:使用行级锁来提高并发性,减少锁竞争

计算机视觉工程师所需的基本技能

一、编程技能 熟练掌握编程语言 Python:在计算机视觉领域广泛应用,有丰富的库如 OpenCV、TensorFlow、PyTorch 等,方便进行算法实现和模型开发。 C++:运行效率高,适用于对性能要求严格的计算机视觉应用。 数据结构与算法 掌握常见的数据结构(如数组、链表、栈、队列、树、图等)和算法(如排序、搜索、动态规划等),能够优化代码性能,提高算法效率。 二、数学基础