Configuration, Controls និងការវាស់ FPS ពិតរបស់កាមេរ៉ា
ប្រៀបធៀប Preview / Still / Video Configuration កំណត់ Frame Rate និង Autofocus ហើយវាស់ FPS ពិតនៅ Resolution ផ្សេងៗ។
- ភ្ជាប់ Camera Module ត្រឹមត្រូវ (CSI 15-pin / 22-pin) និងផ្ទៀងផ្ទាត់ដោយ rpicam-hello --list-cameras
- ដំឡើង python3-picamera2 / python3-opencv តាម apt និងបង្កើត venv ជាមួយ --system-site-packages
- កំណត់ Configuration (resolution, format, frame rate) និងវាស់ FPS ពិតរបស់កាមេរ៉ា
Picamera2 មាន Configuration គំរូ ៣៖ create_preview_configuration() (Resolution ទាប Buffer តិច សម្រាប់មើលផ្ទាល់), create_still_configuration() (Resolution ពេញ គុណភាពខ្ពស់ តែ Frame Rate ទាប) និង create_video_configuration() (Buffer ច្រើន សម្រាប់ Stream ជាប់ៗ)។ សម្រាប់ Computer Vision Real-time យើងប្រើ Video Configuration ទំហំ 640×480 ព្រោះវាផ្ដល់ Frame ជាប់លាប់។ Picamera2 ក៏អាចមាន Stream ទី២ lores (Low Resolution) ក្នុងពេលតែមួយ ឧ. main 1920×1080 សម្រាប់ថតទុក និង lores 320×240 សម្រាប់វិភាគ — លើ Pi 4 lores ត្រូវតែជា Format YUV420។
Controls អនុញ្ញាតឱ្យគ្រប់គ្រងសេនស័រដោយផ្ទាល់៖ FrameDurationLimits ជាមីក្រូវិនាទី (ឧ. (33333, 33333) = 1000000/33333 ≈ 30 FPS), ExposureTime និង AnalogueGain សម្រាប់ Manual Exposure និង AfMode សម្រាប់ Autofocus របស់ Camera Module 3 (controls.AfModeEnum.Continuous ពី from libcamera import controls)។ កាមេរ៉ាដែលគ្មាន Autofocus មិនមាន Control AfMode ទេ ដូច្នេះត្រូវពិនិត្យ picam2.camera_controls ជាមុនសិន។ capture_metadata() ប្រាប់តម្លៃពិតដូចជា ExposureTime និង AnalogueGain នៃ Frame នីមួយៗ។
FPS (Frames Per Second) គណនាដោយ FPS = N/(Δ t) ដែល N ជាចំនួន Frame និង Δ t ជារយៈពេលគិតជាវិនាទី វាស់ដោយ time.perf_counter() (ច្បាស់ជាង time.time())។ capture_array() រង់ចាំ Frame ថ្មីពីសេនស័រ ដូច្នេះ FPS ដែលវាស់បានមិនអាចលើស Frame Rate ដែលកំណត់ (30 FPS) ឡើយ។ បើ FPS ធ្លាក់ខ្លាំងនៅ Resolution ខ្ពស់ គឺដោយសារ CPU ចំណាយពេលចម្លង និងដំណើរការ Pixel ច្រើនពេក — នេះជាមូលហេតុដែល Vision លើ Edge ប្រើ 640×480 ឬតូចជាង។
ក្នុងបន្ទប់ងងឹត AE នឹងបង្កើន ExposureTime ធ្វើឱ្យ Frame Rate ធ្លាក់ និងរូបព្រិល (Motion Blur)។ ដំណោះស្រាយ៖ បន្ថែមពន្លឺ ឬកំណត់ FrameDurationLimits ដើម្បីការពារកុំឱ្យ Exposure វែងពេក (ទទួលយក Noise ច្រើនជាងមុនបន្តិចជាថ្នូរ)។
- ប្រើ create_video_configuration 640×480 សម្រាប់ Real-time Vision; Still Configuration សម្រាប់រូបគុណភាពខ្ពស់
- FrameDurationLimits គិតជាមីក្រូវិនាទី — 33333 µs ≈ 30 FPS
- ពិនិត្យ AfMode ក្នុង camera_controls មុនបើក Autofocus (មានតែលើ Camera Module 3)
- វាស់ FPS ដោយ time.perf_counter() លើ Frame ច្រើន (≥ 60) ដើម្បីបានលទ្ធផលស្ថិរ
"""Measure the real capture + processing FPS of Picamera2 at several resolutions."""
import time
import cv2
from picamera2 import Picamera2
RESOLUTIONS = [(1280, 720), (640, 480), (320, 240)]
FRAMES_PER_TEST = 60
FRAME_DURATION_US = 33333 # 1 / 30 s -> target 30 FPS
def enable_autofocus(picam2):
"""Camera Module 3 supports continuous autofocus; other cameras do not."""
if "AfMode" in picam2.camera_controls:
from libcamera import controls
picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous})
print("Continuous autofocus enabled")
def benchmark(picam2, size):
config = picam2.create_video_configuration(
main={"size": size, "format": "RGB888"},
controls={"FrameDurationLimits": (FRAME_DURATION_US, FRAME_DURATION_US)},
)
picam2.configure(config)
picam2.start()
enable_autofocus(picam2)
time.sleep(1.0) # let AE/AWB settle before timing
brightness = 0.0
frame = None
start = time.perf_counter()
for _ in range(FRAMES_PER_TEST):
frame = picam2.capture_array() # BGR order, (h, w, 3)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # typical first CV step
brightness = float(gray.mean())
elapsed = time.perf_counter() - start
metadata = picam2.capture_metadata()
cv2.imwrite(f"bench_{size[0]}x{size[1]}.jpg", frame)
picam2.stop()
fps = FRAMES_PER_TEST / elapsed
print(
f"{size[0]:>4}x{size[1]:<4} -> {fps:5.1f} FPS | "
f"exposure {metadata.get('ExposureTime')} us | "
f"gain {metadata.get('AnalogueGain', 0):.2f} | brightness {brightness:.0f}"
)
def main():
picam2 = Picamera2()
try:
for size in RESOLUTIONS:
benchmark(picam2, size)
finally:
picam2.close()
if __name__ == "__main__":
main()ការពន្យល់៖ កម្មវិធីនេះ configure កាមេរ៉ាឡើងវិញសម្រាប់ Resolution នីមួយៗ (ត្រូវ stop() មុន configure ថ្មី) វាស់ពេលសម្រាប់ 60 Frame ដែលរួមទាំងការបម្លែងទៅ Grayscale ហើយបោះពុម្ព FPS, ExposureTime និង Gain ពិត។ លទ្ធផលបង្ហាញភ្លាមថា Resolution ប៉ះពាល់ល្បឿនប៉ុណ្ណា។
សំណួរវាស់សមត្ថភាព (Knowledge Check)
ឆ្លើយគ្រប់សំណួរ រួចចុច «ពិនិត្យចម្លើយ»។ បើត្រឹមត្រូវ ≥ ៨០% មេរៀននេះនឹងត្រូវបានកត់ត្រាថាបានបញ្ចប់ស្វ័យប្រវត្តិ។
- Picamera2 — Official GitHub Repository & Examples github.com/raspberrypiកូដគំរូផ្លូវការសម្រាប់ Configurations, Controls, Autofocus និង OpenCV Integration។