本教程向您展示瞭如何構建類似於Microsoft的What-Dog AI,但使用Diffbot的Image API,該教程向您展示瞭如何構建狗品種標識符。 整個應用程序少於100行代碼和利用IMGUR以託管以最大程度地降低成本。
密鑰功能:
>使用簡單的圖像上傳表格和PHP進行處理。 > difbot的圖像API分析上載的圖像,並根據已確定的標籤返回建議。
。difbot帳戶:從diffbot.com獲取免費的14天API代幣。
composer.json
<code class="language-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>> imgur帳戶:
<code>Run `composer install`. The `minimum-stability` setting accommodates a beta dependency.</code>>創建一個IMGUR帳戶並獲取匿名映像上傳的客戶ID。
函數(輔助功能):
代碼使用輔助功能(未顯示)為每個建議的品種創建指向Bing Image搜索結果的鏈接。
index.php
<code class="language-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="%5C%22%7B%24url%7D%5C%22" style="max-width:90%"500\"' alt="在100行代碼中構建Microsoft的What-Dog AI" >"; if (empty($tags)) { echo "<h4>No breed identified.</h4>"; } else { echo "<h4>Suggested Breed(s):</h4>"; foreach ($tags as $tag) { echo "- <a href="%5C%22https://www.bing.com/images/search?q=%22" . urlencode target='\"_blank\"'>" . $tag['label'] . "</a><br>"; } } } ?> <!-- HTML form for image upload or URL input --></code>該教程包括幾個測試圖像及其結果,強調了品種識別的成功和失敗。 準確性與微軟的What-Dog AI相媲美,證明了使用DiffBot構建類似應用的可行性。
結論:
>本教程展示了將AI驅動圖像分析集成到簡單Web應用程序中的易用性。儘管準確性並不完美,但它突出了隨時可用的API來構建強大的圖像識別功能的潛力。 切記用自己的佔位符令牌和ID替換。
以上是在100行代碼中構建Microsoft的What-Dog AI的詳細內容。更多資訊請關注PHP中文網其他相關文章!