首页 > 其他分享 >CSharp: ImageToText using Microsoft.SemanticKernel

CSharp: ImageToText using Microsoft.SemanticKernel

时间:2024-04-10 14:48:21浏览次数:10  
标签:ImageToText Console Microsoft azure var using SemanticKernel

 

using Microsoft.SemanticKernel.ImageToText;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Connectors.HuggingFace;
//using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.Connectors;
using Microsoft.SemanticKernel.Experimental.Orchestration;
using System.Text;



namespace SemanticConsoleApp
{


#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
#pragma warning disable SKEXP0070 // Type is for evaluation purposes only and is subject to change or removal in future updates.
    /// <summary>
    /// 
    /// </summary>
    public partial class Program
    {


       // private readonly IImageToTextService service;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Console.WriteLine("Hello,CSharp World!");
            var builder = Kernel.CreateBuilder();
            builder.AddAzureOpenAIChatCompletion("gpt-35-turbo", "https://contoso.openai.azure.com/", "...your Azure OpenAI Key...");
            var kernel = builder.Build();
           // IImageToTextService service=kernel.GetRequiredService<IImageToTextService>();


            // var kernel = Kernel.CreateBuilder().AddHuggingFaceImageToText("Salesforce/blip-image-captioning-base").Build();
            IImageToTextService service = kernel.GetRequiredService<IImageToTextService>();
            
            var imageBinary = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "demo.jpg"));
            var imageContent = new ImageContent(imageBinary) { MimeType = "image/jpeg" };
            //var textContent = await service.GetTextContentAsync(imageContent);
            var textContent = service.GetTextContentAsync(imageContent);
            Console.WriteLine($"已识别图片中描述的内容: {textContent.Result}");


         //   var builder = Kernel.CreateBuilder();
         //   builder.AddAzureOpenAIChatCompletion(
         //"gpt-35-turbo",                      // Azure OpenAI Deployment Name
         //"https://contoso.openai.azure.com/", // Azure OpenAI Endpoint
         //"...your Azure OpenAI Key...");      // Azure OpenAI Key

         //   var kernel = builder.Build();



            //2
            ////# 初始化Kernel

           // var myKernel = Kernel.CreateBuilder();// Kernel.Builder.Build();

            //myKernel.Config.AddAzureOpenAITextCompletionService(
            //    "davinci-azure",
            //    "text-davinci-003",
            //    "{your azure openai endpoint}",
            //    "{your azure openai key}");
            ////# 导入技能
            //var mySkill = myKernel.ImportSemanticSkillFromDirectory("Skills", "Learning");
            //var myContext = new ContextVariables();
            //StringBuilder histories = new StringBuilder();
            //Console.WriteLine("Say anything to start practicing English.");
            //while (true)
            //{
            //    Console.ForegroundColor = ConsoleColor.DarkRed;
            //    var input = Console.ReadLine();
            //    //# 填充变量
            //    myContext.Set("history", histories.ToString());
            //    myContext.Set("input", input);
            //    //# 运行技能
            //    var myResult = await myKernel.RunAsync(myContext, mySkill["LearningEnglishSkill"]);
            //    histories.AppendLine(input);
            //    histories.AppendLine(myResult.Result.ToString());
            //    Console.WriteLine(myResult);
            //}



        }
    }
}

https://learn.microsoft.com/zh-cn/training/azure/?WT.mc_id=home_homepage-azureportal-learn&context=sso&source=azure-portal
https://learn.microsoft.com/zh-cn/azure/ai-services/openai/quickstart?pivots=rest-api&tabs=command-line%2Cpython-new
https://portal.azure.com/#home

  

 

标签:ImageToText,Console,Microsoft,azure,var,using,SemanticKernel
From: https://www.cnblogs.com/geovindu/p/18125980

相关文章

  • crictl images报错runtime connect using default endpoints: [unix:///var/run/docke
    想试试containerd运行k8s,结果报错还在找dockershim,网上找了解决方法crictl依次查找容器运行时,当查找第一个unix:///var/run/dockershim.sock没有找到,所以报错了,需要你手动指定当前kubernetes的容器运行时,使用什么,例如:kubernetes1.24+之后,dockershim已经变成了cri-docker,所以......
  • [React] Using key prop to reset component to avoid useEffect hook
    ThecomponentusinguseEffectwhichisnotnecessary:functionTopicEditor({selectedTopicId}){const[enteredNote,setEnteredNote]=useState('');constselectedTopic=DUMMY_TOPICS.find(topic=>topic.id===selectedTopicId)......
  • Property [renew] not found. Using default value [false]
    接口请求时,报错Property[renew]notfound. Usingdefaultvalue[false]返回:Therewasanunexpectederror(type=NotFound,status=404).有两种可能:1、添加扫描路径使用basePackages:@ComponentScan(basePackages={"com.person","com.controller"})2、添加依赖......
  • 安装Pygame过程中提示错误WARNING: Retrying…ERROR: Exception: Traceback…WARNING:
    安装Pygame过程中提示错误WARNING:Retrying…ERROR:Exception:Traceback…WARNING:Youareusingpipversion解决方案前言Pygame错误错误分析解决方案错误分析结论更新pip安装Pygame前言输入Pygame安装命令pipinstallpygame安装Pygame出错提......
  • ts using/核心技能/三个我/日志/如何做好工作/多写/写简单的代码
    TypeScript5.2的新关键词usinghttps://www.yuque.com/beilo/simpread/1712152587999?singleDoc#《译:TypeScript5.2的新关键词using–云谦的博客》核心技能:构建软件的基础、关键、核心技能是沟通和协作。三个我:过去的我是一个懒惰且粗心的家伙,总是留下一团糟。现在......
  • Go - Using Reflection
    Reflectioncanbeusedtoinspectthetypeofvariablesandtodynamicallyaccessandmodifytheirvalues.Followingisasimpleexampleofusingreflectiontoinspectavariable'stype: funcmain(){v1:=42inspectVariable(v1)v2:......
  • WPF Storyboary DoubleAnimationUsingPath PathGeometry
    <Windowx:Class="WpfApp30.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • 快速上手SemanticKernel+KernelMemory
    本文撰写目的是旨在帮助新手快速上手,原理部分可以参考博客园大佬宵伯特的教程。KernelMemory入门系列博客:(KernelMemory--宵伯特-博客园(cnblogs.com))SemanticKernel入门系列:(SemanticKernel--宵伯特-博客园(cnblogs.com))为了方便以下SemanticKernel称作SK,Kernel......
  • MySQL数据库报错:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘
    在安装或配置MySQL数据库时,遇到错误是一个常见现象。这篇文章将详细讨论另一个常见的安装错误,包括错误的表现、产生的原因以及如何有效地解决该问题。了解这些信息可以帮助你快速定位问题所在,并采取适当的措施解决问题。错误描述一个常见的MySQL安装错误是:ERROR1045(28......
  • Cannot connect to AnnotationHub server, using 'localHub=TRUE' instead Using 'loc
     001、问题CannotconnecttoAnnotationHubserver,using'localHub=TRUE'insteadUsing'localHub=TRUE'library(AnnotationHub)library(biomaRt)library(clusterProfiler)hub<-AnnotationHub()##该句报错 002、解决方法library(Annot......