首頁 >後端開發 >php教程 >在100行代碼中構建Microsoft的What-Dog AI

在100行代碼中構建Microsoft的What-Dog AI

Jennifer Aniston
Jennifer Aniston原創
2025-02-15 10:30:121013瀏覽

本教程向您展示瞭如何構建類似於Microsoft的What-Dog AI,但使用Diffbot的Image API,該教程向您展示瞭如何構建狗品種標識符。 整個應用程序少於100行代碼和利用IMGUR以託管以最大程度地降低成本。

Building Microsoft's What-Dog AI in under 100 Lines of Code 密鑰功能:

>使用簡單的圖像上傳表格和PHP進行處理。 > difbot的圖像API分析上載的圖像,並根據已確定的標籤返回建議。

    雖然不完美,但最終的應用程序展示了現代AI在圖像識別方面的可訪問性和潛力。
  • 入門:

difbot帳戶:從diffbot.com獲取免費的14天API代幣。

    >
  1. 作曲家設置:使用以下
  2. 安裝必要的庫:
  3. 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。
  1. 代碼結構(index.php):>
  2. 核心邏輯位於
中。 代碼通過HTML表單上傳圖像(省略了簡潔,重點是PHP後端)。 Imgur用於託管,節省服務器成本。 然後將上傳的圖像URL發送到Diffbot的Image API。

函數(輔助功能):

代碼使用輔助功能(未顯示)為每個建議的品種創建指向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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn