🧠[2026/04/19][💻LLM⚙️🚀] Google AI Edge Gallery: Ask Image 掃瞄圖片 2 [💻含程式碼 Python]

[English Version]

Google AI Edge Gallery邊緣運算實驗室 2 [含程式碼 Python]

我一直深信,基於已有的知識去學習新事物是最容易上手的。

繼上次實戰過 HTML 與 JS 呼叫 CDN 的「Ask Image」模型後,這次我決定踏入 Python 的世界,挑戰另一種呼叫方式。

背景交代:

我並沒有 Python 的實務經驗,也沒上過相關課程。但這就是實驗室的精神——在 Python 的世界裡探險,打通技術的一條路。


 🔍 什麼是 Local LLM?(本地大語言模型)

來補充一下,釐清一個大家常聽到的技術術語:Local LLM。

很多人對「Edge(邊緣運算)」一頭霧水,其實這反映了計算架構的演進,我們可以用一個簡單的比喻來解釋:

  • Cloud AI (雲端 AI):像一間超大的圖書館,你每次問問題都要跑去借書(還要連網、等回應、數據還會被記錄)。
  • 本地 AI(Edge 運算):直接把書架搬回家,全部裝在你電腦裡。不用網路,數據不離機,速度快,隱私也更有保障。

這次我用的是 Google AI Edge Gallery 的物件偵測模型,直接在本地跑,感覺就像在家裡養了一隻小 AI 助手,隨時可以玩。


 📊 掃描報告:當 AI 遇上現實生活

這是我和豹貓的自拍合照。背景是滿滿的綠植,豹貓的花紋又跟環境差不多。

【測試一:大阪貓 Cafe 的驚險「保護色」事件】

這是我和豹貓的自拍合照。背景是滿滿的綠植,豹貓的花紋又跟環境差不多。
AI 掃描後顯示:

  • PERSON (77.97%):穩穩地把我框出來。
  • CAT (30.25%):剛好卡在門檻邊緣!
  • 診斷:豹貓幾乎跟背景融為一體,差點就被 AI 當成「隱形貓咪」過濾掉。好險貓咪爭氣,勉強達到 30% 的信心值,否則我就只能跟 AI 說「你把我的貓朋友弄不見了啦!」🐱✨

【測試二:水怪咕𠱸的「靈魂合一」誤判】

這次更誇張。我把臉整個埋進水怪咕𠱸公仔裡,只露出一點點。
本地 AI 看完後,冷靜地輸出:

  • PERSON (71.13%)
  • 診斷:「我的臉 + 公仔」當成一個完整的「人」!因為公仔閉著的眼睛跟我的臉重疊,加上沒有明顯邊界,AI 大概在心裡想:「這是什麼新型態的生物?人類進化出新品種了嗎?還是他們已經靈魂合一了?」

這一刻我真的笑出來——原來本地 AI 這麼有想像力,完全不輸雲端模型的搞笑程度。「靈魂合一」了。


 🚀 小結與下一步

雖然現階段本地 AI 在精準度上還比不上雲端(畢竟雲端有強大後援),但它已經夠好玩、夠安全,也夠快了。 我非常期待未來邊緣運算能再進化,讓我們在家裡就能擁有真正屬於自己的 AI 實驗室。

技術探險仍在繼續,下次實驗室報告見!如果你也跟著試了,歡迎在留言區分享你的「AI 誤判名場面」,我們一起笑 😂


程式碼與實作步驟


 🛠️ 開發環境建置 (技術指南)

如果你也想動手試試看,這是我的實戰流程:

步驟一:在電腦開一個新資料夾(例如 C:\_Playground\Python_AI_Lab\)。

步驟二:建立虛擬環境 (在 Terminal / 命令提示字元輸入)

  • 建立環境:執行 python -m venv venv 成功建立了虛擬環境資料夾 。
  • 啟動指令:執行 .\venv\Scripts\activate 成功呼叫了啟動腳本 。

註:

  1. Windows: venv\Scripts\activate
  2. Mac/Linux: source venv/bin/activate
  • 狀態確認:提示字元左側出現了 (venv),這表示目前的終端機已經與系統其他的 Python 環境隔離,現在安裝任何套件都會待在這個資料夾裡 。

步驟四:接著在同一個視窗輸入以下指令來安裝 AI 引擎

安裝指令:執行 pip install mediapipe

註: Google AI Edge 的 Python 支援主要是透過 mediapipe 這個庫來實現的。

註:  如果之後關掉了Terminal (這個黑色的視窗),下次想回來繼續玩 Python 版時,記得要再跑一次 .\venv\Scripts\activate 啟動指令

步驟五:下載模型檔案

在執行程式碼前,請先下載模型檔,並放在 .py 檔案同一個資料夾內:

步驟六:撰寫測試腳本 (detector.py)

import mediapipe as mp
from mediapipe.tasks import python
from mediapipe.tasks.python import vision
# ==========================================
# Annie's AI Lab - Object Detection Script
# Model: EfficientDet-Lite0
# ==========================================
def run_detection():
# 1. Configuration: Set the model path
# Ensure 'efficientdet_lite0.tflite' is in the same directory
model_path = 'efficientdet_lite0.tflite'
# 2. Detector Options Setup
# score_threshold: Lowering this might help find the "hidden" celery!
base_options = python.BaseOptions(model_asset_path=model_path)
options = vision.ObjectDetectorOptions(
base_options=base_options,
score_threshold=0.3,
running_mode=vision.RunningMode.IMAGE
)
# 3. Create Detector Instance
with vision.ObjectDetector.create_from_options(options) as detector:
# 4. Load the input image
# Ensure the filename matches your photo (e.g., 'test.jpeg')
image_file = 'test.jpeg'
try:
numpy_image = mp.Image.create_from_file(image_file)
except Exception as e:
print(f"Error: Could not load image file '{image_file}'. {e}")
return
# 5. Execute Inference
detection_result = detector.detect(numpy_image)
# 6. Output Analysis Report
print("\n" + "="*45)
print(" ANNIE'S PYTHON AI LAB - SCAN REPORT 🐰✨")
print("="*45)
if not detection_result.detections:
print("Status: No objects detected above threshold.")
print("Action: Try lowering the score_threshold in the code.")
else:
print(f"Status: {len(detection_result.detections)} object(s) found.")
print("-" * 45)
for i, detection in enumerate(detection_result.detections):
category = detection.categories[0]
label = category.category_name.upper()
confidence = round(category.score * 100, 2)
# Retrieve Bounding Box Coordinates
bbox = detection.bounding_box
print(f"[{i+1}] Object: {label}")
print(f" Confidence: {confidence}%")
print(f" Location: [X:{bbox.origin_x}, Y:{bbox.origin_y}, W:{bbox.width}, H:{bbox.height}]")
print("-" * 45)
print("UAT Result: Scan completed successfully.")
if __name__ == "__main__":
run_detection()

步驟七:把測試的圖檔命名為 test.jpeg,放在 .py 檔案的資料夾。

步驟八:在有(venv)字眼的 terminal, 在.py 檔案的資料夾,行 command python .py 檔案的名字。

💯 測試完成~ ✨