التشغيل السريع: استخدام مكتبة عميل البحث عن الصور Bing
تحذير
في 30 أكتوبر 2020، انتقلت واجهات برمجة تطبيقات البحث Bing من الخدمات المعرفية إلى خدمات البحث Bing. يتم توفير هذه الوثائق للرجوع إليها فقط. للحصول على الوثائق المحدثة، راجع وثائق واجهة برمجة تطبيقات البحث Bing. للحصول على إرشادات حول إنشاء موارد Azure جديدة للبحث Bing، راجع إنشاء مورد بحث Bing خلال Azure Marketplace.
استخدم هذا التشغيل السريع لإجراء أول بحث عن الصور باستخدام مكتبة عميل Bing Image Search.
مكتبة بحث العميل هي المجمع لـ API REST وتحتوي على نفس الميزات.
يرسل تطبيق C# البسيط استعلام بحث عن الصور، يحلل استجابة JSON ويعرض عنوان URL للصورة الأولى التي أُرجعت.
المتطلبات الأساسية
- إذا كنت تستخدم Windows، أي نسخة من Visual Studio 2017 أو أحدث
- إذا كنت تستخدم macOS أو Linux، تعليمة VS الظاهرية مع تثبيت .NET Core
- اشتراك Azure مجاني
راجع تسعير الخدمات المعرفية - واجهة برمجة تطبيقات بحث Bing.
إنشاء مشروع وحدة تحكم
أولًا، قم بإنشاء تطبيق وحدة تحكم C# جديد.
قم بإنشاء حل وحدة تحكم جديد مسمى BingImageSearch في Visual Studio.
إضافة حزمة Cognitive Image Search NuGet
- انقر بزر الماوس الأيمن فوق المشروع في مستكشف الحلول.
- حدد Manage NuGet Packages.
- ابحث عن وحدد Microsoft.Azure.CognitiveServices.Search.ImageSearch، ثم ثبت الحزمة.
تهيئة التطبيق
استبدل كافة
usingالعبارات في Program.cs بالتعليمات البرمجية التالية:using System; using System.Linq; using Microsoft.Azure.CognitiveServices.Search.ImageSearch; using Microsoft.Azure.CognitiveServices.Search.ImageSearch.Models;في الأسلوب
Mainبمشروعك، قم بإنشاء متغيرات لمفتاح الاشتراك الصالح، ونتائج الصورة التي سيتم إرجاعها بواسطة Bing، وعبارة بحث. ثم قم بإنشاء مثيل عميل البحث عن الصور باستخدام المفتاح.static async Task Main(string[] args) { //IMPORTANT: replace this variable with your Cognitive Services subscription key string subscriptionKey = "ENTER YOUR KEY HERE"; //stores the image results returned by Bing Images imageResults = null; // the image search term to be used in the query string searchTerm = "canadian rockies"; //initialize the client //NOTE: If you're using version 1.2.0 or below for the Bing Image Search client library, // use ImageSearchAPI() instead of ImageSearchClient() to initialize your search client. var client = new ImageSearchClient(new ApiKeyServiceClientCredentials(subscriptionKey)); }
إرسال استعلام بحث باستخدام العميل
لا يزال في الأسلوب Main، استخدم العميل للبحث مع نص استعلام:
// make the search request to the Bing Image API, and get the results"
imageResults = await client.Images.SearchAsync(query: searchTerm).Result; //search query
تحليل وعرض أول نتيجة الصورة
قم بتحليل نتائج الصورة التي تم إرجاعها في الاستجابة.
إذا كانت الاستجابة تحتوي على نتائج البحث، قم بتخزين النتيجة الأولى وطباعة بعض تفاصيلها.
if (imageResults != null)
{
//display the details for the first image result.
var firstImageResult = imageResults.Value.First();
Console.WriteLine($"\nTotal number of returned images: {imageResults.Value.Count}\n");
Console.WriteLine($"Copy the following URLs to view these images on your browser.\n");
Console.WriteLine($"URL to the first image:\n\n {firstImageResult.ContentUrl}\n");
Console.WriteLine($"Thumbnail URL for the first image:\n\n {firstImageResult.ThumbnailUrl}");
Console.WriteLine("Press any key to exit ...");
Console.ReadKey();
}
الخطوات التالية
راجع أيضًا
استخدم التشغيل السريع هذا لإجراء أول بحث عن الصور باستخدام مكتبة عميل Bing للبحث عن الصور وهو عبارة عن برنامج تضمين لواجهة برمجة التطبيقات ويحتوي على نفس الميزات. يُرسل تطبيق جافا البسيط هذا استعلام البحث عن الصور ويوزع استجابة JSON، ويعرض عنوان URL للصورة الأولى التي تم إرجاعها.
المتطلبات الأساسية
الإصدار الحالي من Java Development Kit(JDK)
تثبيت تبعيات مكتبة عميل البحث عن الصور في Bing باستخدام نظام إدارة التبعية في تطبيق Maven أو Gradle أو أي نظام آخر. يتطلب ملف Maven POM الإعلان التالي:
<dependencies>
<dependency>
<groupId>com.microsoft.azure.cognitiveservices</groupId>
<artifactId>azure-cognitiveservices-imagesearch</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
إنشاء التطبيق وتهيئته
إنشاء مشروع Java جديد في IDE المفضلة لديك أو المحرر ثم إضافة عمليات الاستعادة التالية إلى تطبيق الفئة الخاصة بك:
import com.microsoft.azure.cognitiveservices.search.imagesearch.BingImageSearchAPI; import com.microsoft.azure.cognitiveservices.search.imagesearch.BingImageSearchManager; import com.microsoft.azure.cognitiveservices.search.imagesearch.models.ImageObject; import com.microsoft.azure.cognitiveservices.search.imagesearch.models.ImagesModel;أنشئ في الأسلوب الرئيسي المتغيرات لمفتاح الاشتراك وابحث عنهم. ثم أنشئ مثيلاً لعميل البحث عن الصور في Bing.
final String subscriptionKey = "COPY_YOUR_KEY_HERE"; String searchTerm = "canadian rockies"; //Image search client BingImageSearchAPI client = BingImageSearchManager.authenticate(subscriptionKey);
إرسال طلب بحث إلى واجهة برمجة التطبيقات
باستخدام
bingImages().search()، أرسل طلب بروتوكول نقل النص الفائق الذي يحتوي على استعلام البحث. احفظ الاستجابة في صورةImagesModel.ImagesModel imageResults = client.bingImages().search() .withQuery(searchTerm) .withMarket("en-us") .execute();
حلل واعرض النتيجة
حلل نتائج الصورة التي تم إرجاعها في الاستجابة. إذا كانت الاستجابة تحتوي على نتائج بحث، فخزن النتيجة الأولى واطبع تفاصيلها كعنوان URL المصغر وعنوان URL الأصلي بالإضافة إلى إجمالي عدد الصور التي تم إرجاعها.
if (imageResults != null && imageResults.value().size() > 0) {
// Image results
ImageObject firstImageResult = imageResults.value().get(0);
System.out.println(String.format("Total number of images found: %d", imageResults.value().size()));
System.out.println(String.format("First image thumbnail url: %s", firstImageResult.thumbnailUrl()));
System.out.println(String.format("First image content url: %s", firstImageResult.contentUrl()));
}
else {
System.out.println("Couldn't find image results!");
}
الخطوات التالية
راجع أيضًا
استخدم هذه البداية السريعة لإجراء أول بحث عن الصور باستخدام مكتبة عميل Bing Image Search، وهي عبارة عن غلاف لواجهة برمجة التطبيقات API وتحتوي على نفس الميزات. يرسل تطبيق JavaScript البسيط هذا استعلام بحث عن الصور ويحلل استجابة JSON ويعرض عنوان URL للصورة الأولى التي تم إرجاعها.
المتطلبات الأساسية
- أحدث إصدار من Node.js .
- The Bing Image Search SDK for JavaScript
- للتثبيت، قم بتشغيل
npm install @azure/cognitiveservices-imagesearch
- للتثبيت، قم بتشغيل
- فئة
CognitiveServicesCredentialsمن الحزمة@azure/ms-rest-azure-jsلمصادقة العميل.- للتثبيت، قم بتشغيل
npm install @azure/ms-rest-azure-js
- للتثبيت، قم بتشغيل
إنشاء التطبيق وتهيئته
قم بإنشاء ملف JavaScript جديد في IDE أو المحرر المفضل لديك، واضبط الصرامة وhttps والمتطلبات الأخرى.
'use strict'; const ImageSearchAPIClient = require('@azure/cognitiveservices-imagesearch'); const CognitiveServicesCredentials = require('@azure/ms-rest-azure-js').CognitiveServicesCredentials;في الطريقة الرئيسية لمشروعك، قم بإنشاء متغيرات لمفتاح الاشتراك الصالح الخاص بك، ونتائج الصور التي سيتم إرجاعها بواسطة Bing، ومصطلح البحث. ثم قم بإنشاء مثيل لعميل البحث عن الصور باستخدام المفتاح.
//replace this value with your valid subscription key. let serviceKey = "ENTER YOUR KEY HERE"; //the search term for the request let searchTerm = "canadian rockies"; //instantiate the image search client let credentials = new CognitiveServicesCredentials(serviceKey); let imageSearchApiClient = new ImageSearchAPIClient(credentials);
قم بإنشاء وظيفة مساعد غير متزامنة
قم بإنشاء وظيفة لاستدعاء العميل بشكل غير متزامن، وإرجاع الاستجابة من خدمة البحث Bing Image Search.
// a helper function to perform an async call to the Bing Image Search API const sendQuery = async () => { return await imageSearchApiClient.imagesOperations.search(searchTerm); };
إرسال استعلام والتعامل مع الاستجابة
قم باستدعاء الوظيفة المساعدة والتعامل معها
promiseلتحليل نتائج الصورة التي تم إرجاعها في الاستجابة.إذا كانت الاستجابة تحتوي على نتائج بحث، فقم بتخزين النتيجة الأولى وطباعة تفاصيلها، مثل عنوان URL للصورة المصغرة وعنوان URL الأصلي بالإضافة إلى العدد الإجمالي للصور التي تم إرجاعها.
sendQuery().then(imageResults => { if (imageResults == null) { console.log("No image results were found."); } else { console.log(`Total number of images returned: ${imageResults.value.length}`); let firstImageResult = imageResults.value[0]; //display the details for the first image result. After running the application, //you can copy the resulting URLs from the console into your browser to view the image. console.log(`Total number of images found: ${imageResults.value.length}`); console.log(`Copy these URLs to view the first image returned:`); console.log(`First image thumbnail url: ${firstImageResult.thumbnailUrl}`); console.log(`First image content url: ${firstImageResult.contentUrl}`); } }) .catch(err => console.error(err))
الخطوات التالية
راجع أيضًا
استخدم هذا التشغيل السريع لإجراء أول بحث عن الصور باستخدام مكتبة عميل Bing Image Search، وهي عبارة عن برنامج تضمين لواجهة برمجة التطبيقات وتحتوي على نفس الدالات. يرسل تطبيق Python البسيط استعلام بحث عن الصور ويصور استجابة JSON ويعرض عنوان "يو أر إل" للصورة الأولى التي أُرجعت.
المتطلبات الأساسية
مكتبة عميل Azure Image Search لـ Python
- تثبيت باستخدام
pip install azure-cognitiveservices-search-imagesearch
- تثبيت باستخدام
إنشاء وتهيئة التطبيق
إنشاء برنامج نصي Python جديد في IDE المفضلة أو المحرر، والواردات التالية:
from azure.cognitiveservices.search.imagesearch import ImageSearchClient from msrest.authentication import CognitiveServicesCredentialsإنشاء متغيرات لكلمة مرور الاشتراك وعبارة البحث.
subscription_key = "Enter your key here" subscription_endpoint = "Enter your endpoint here" search_term = "canadian rockies"
إنشاء image search للعميل
إنشاء مثيل من
CognitiveServicesCredentials، واستخدامه لإنشاء مثيل العميل:client = ImageSearchClient(endpoint=subscription_endpoint, credentials=CognitiveServicesCredentials(subscription_key))إرسال استعلام بحث إلى Bing Image Search API:
image_results = client.images.search(query=search_term)
معالجة وعرض النتائج
تحليل نتائج الصورة المُعادة في الاستجابة.
إذا احتوت الاستجابة على نتائج البحث، فخزن واطبع تفاصيل النتيجة الأولى، مثل عنوان "يو أر إل" المصغر وعنوان "يو أر إل" الأصلي، بالإضافة إلى إجمالي عدد الصور المُعادة.
if image_results.value:
first_image_result = image_results.value[0]
print("Total number of images returned: {}".format(len(image_results.value)))
print("First image thumbnail url: {}".format(
first_image_result.thumbnail_url))
print("First image content url: {}".format(first_image_result.content_url))
else:
print("No image results returned!")