本教程向您展示瞭如何構建類似於Microsoft的What-Dog AI,但使用Diffbot的Image API,該教程向您展示瞭如何構建狗品種標識符。 整個應用程序少於100行代碼和利用IMGUR以託管以最大程度地降低成本。
密鑰功能:
>使用簡單的圖像上傳表格和PHP進行處理。 > difbot的圖像API分析上載的圖像,並根據已確定的標籤返回建議。
。difbot帳戶:從diffbot.com獲取免費的14天API代幣。
composer.json
{ "require": { "swader/diffbot-php-client": "^2", "php-http/guzzle6-adapter": "^1.0" }, "minimum-stability": "dev", "prefer-stable": true, "require-dev": { "symfony/var-dumper": "^3.0" } }
<code>Run `composer install`. The `minimum-stability` setting accommodates a beta dependency.</code>
函數(輔助功能):
代碼使用輔助功能(未顯示)為每個建議的品種創建指向Bing Image搜索結果的鏈接。
index.php
<?php require 'vendor/autoload.php'; $token = 'YOUR_DIFFBOT_TOKEN'; // Replace with your Diffbot token $imgur_client = 'YOUR_IMGUR_CLIENT_ID'; // Replace with your Imgur Client ID if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle image upload (using $_FILES) or URL submission (using $_POST['url']) // ... (Image upload to Imgur using Guzzle, obtaining the image URL) ... if (!isset($url) || empty($url)) { die("Image upload or URL submission failed."); } $diffbot = new Swader\Diffbot\Diffbot($token); $imageDetails = $diffbot->createImageAPI($url)->call(); $tags = $imageDetails->getTags(); echo "<img src=\"{$url}\" style="max-width:90%"500\" alt="在100行代碼中構建Microsoft的What-Dog AI" ></img>"; if (empty($tags)) { echo "<h4>No breed identified.</h4>"; } else { echo "<h4>Suggested Breed(s):</h4>"; foreach ($tags as $tag) { echo "- <a href=\"https://www.bing.com/images/search?q=" . urlencode($tag['label']) . "\" target=\"_blank\">" . $tag['label'] . "</a><br>"; } } } ?> <!-- HTML form for image upload or URL input -->
結論:
>本教程展示了將AI驅動圖像分析集成到簡單Web應用程序中的易用性。儘管準確性並不完美,但它突出了隨時可用的API來構建強大的圖像識別功能的潛力。 切記用自己的佔位符令牌和ID替換。
以上是在100行代碼中構建Microsoft的What-Dog AI的詳細內容。更多資訊請關注PHP中文網其他相關文章!