本教程向您展示了如何构建类似于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中文网其他相关文章!