📘 EasyOCR + PiCamera2 即時辨識教學
🧰 一、安裝必要套件
sudo apt update
sudo apt install -y libgl1 python3-opencv python3-picamera2
pip install easyocr
如遇 PyTorch 缺失(尤其在 Pi 上),請補上:
pip install torch torchvision --extra-index-url https://download.pytorch.org/whl/cpu
📸 二、建立主程式:easyocr_picamera2.py
import cv2
import easyocr
from picamera2 import Picamera2
# 初始化 EasyOCR (中英文)
reader = easyocr.Reader(['en', 'ch_tra'])
# 初始化 PiCamera2
picam2 = Picamera2()
picam2.preview_configuration.main.size = (640, 480)
picam2.preview_configuration.main.format = "RGB888"
picam2.start()
print("📷 EasyOCR + PiCamera2 即時文字辨識,按 'q' 鍵離開")
while True:
frame = picam2.capture_array()
results = reader.readtext(frame)
for (bbox, text, confidence) in results:
(top_left, top_right, bottom_right, bottom_left) = bbox
top_left = tuple(map(int, top_left))
bottom_right = tuple(map(int, bottom_right))
# 畫框與顯示文字
cv2.rectangle(frame, top_left, bottom_right, (0, 255, 0), 2)
cv2.putText(frame, text, (top_left[0], top_left[1] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 0, 0), 2)
cv2.imshow("EasyOCR + PiCamera2", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
picam2.stop()
▶️ 三、執行程式
python3 easyocr_picamera2.py
🟩 螢幕會顯示:
-
PiCamera2 即時畫面
-
框住文字區域的綠色矩形
-
上方紅色文字為辨識出的結果
✅ 範例結果預期
| 元件 | 效果 |
|---|---|
| 📷 相機 | 來自 PiCamera2 |
| 🟩 框框 | 辨識到的文字區域 |
| 🔤 標籤 | EasyOCR 辨識出的文字(即時) |
| ⌨️ 操作 | 按 q 鍵結束程式 |
文章標籤
全站熱搜
