Device: Raspberry Pi 5 · ระดับ: intermediate

รันตัวอย่าง Computer Vision จาก GitHub บน Raspberry Pi 5

นำตัวอย่างโค้ดจาก repo computer-vision มารันบน Pi 5 และส่งผลลัพธ์ขึ้นเว็บ/Cloudflare

รันตัวอย่าง Computer Vision จาก GitHub บน Raspberry Pi 5

ต่อจาก tutorial แรกที่เตรียม OS และ OpenCV แล้ว บทนี้จะสาธิตการ:

1. Clone repo และเตรียม environment

cd ~/projects
git clone https://github.com/pwd-vw/computer-vision.git

cd computer-vision
source ~/cv-env/bin/activate  # ถ้าใช้ virtual env
pip install -r requirements.txt  # ถ้ามี หรือ pip install ตามที่ต้องการ

หาก repo ยังไม่มี requirements.txt สามารถเริ่มจาก:

pip install opencv-python numpy pillow matplotlib

2. ตัวอย่างสคริปต์: โหลดภาพ + ใส่ filter

สร้างไฟล์ใหม่เช่น pi_examples/ch01_basic_filters.py:

import cv2
import numpy as np
from pathlib import Path

INPUT = Path(\"input.jpg\")
OUTPUT = Path(\"output_blur.jpg\")

img = cv2.imread(str(INPUT))
if img is None:
    raise SystemExit(\"ไม่พบไฟล์ input.jpg ลองวางรูปตัวอย่างในโฟลเดอร์เดียวกันก่อนรัน\")

# แปลงเป็น grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# ใส่ blur
blur = cv2.GaussianBlur(gray, (9, 9), 0)

cv2.imwrite(str(OUTPUT), blur)
print(\"บันทึกผลลัพธ์ที่\", OUTPUT)

ลองวางรูป input.jpg แล้วรัน:

python pi_examples/ch01_basic_filters.py

3. ส่งผลลัพธ์ขึ้น Cloudflare / Backend

สมมติว่าคุณมี endpoint บน Cloudflare Pages Functions เช่น /api/edge-upload ที่รับไฟล์ภาพ:

import requests

API_URL = \"https://www.bs4u-tech.com/api/edge-upload\"

with open(\"output_blur.jpg\", \"rb\") as f:
    files = {\"image\": f}
    resp = requests.post(API_URL, files=files, timeout=30)

print(resp.status_code, resp.text)

ในฝั่งเว็บสามารถแสดงรายการรูปที่มาจาก Pi 5 หรือใช้ใน dashboard ต่อไป

4. รันเป็น service (แนวคิด)

เมื่อสคริปต์ทำงานได้แล้ว คุณสามารถ:

ตัวอย่าง unit file (ย่อ ๆ):

[Unit]
Description=Pi Computer Vision Edge Service

[Service]
ExecStart=/home/pi/cv-env/bin/python /home/pi/projects/computer-vision/pi_examples/ch01_basic_filters.py
WorkingDirectory=/home/pi/projects/computer-vision
Restart=on-failure

[Install]
WantedBy=multi-user.target

จากนั้น:

sudo systemctl daemon-reload
sudo systemctl enable pi-cv.service
sudo systemctl start pi-cv.service

5. เชื่อมกับหลักสูตรบนเว็บ

ในแต่ละบทเรียนของ www.bs4u-tech.com คุณสามารถ:

ใน tutorial ถัดไปจะเจาะจงไปที่ Radxa DRAGON Q6A ซึ่งมี OS และแพ็กเกจแตกต่างจาก Pi เล็กน้อย แต่แนวคิด edge pipeline เดียวกัน