임베디드/Jetson

Jetson Nano 카메라 모듈 연결하기 (8MP IMX219 Stock Lens Fixed Focus)

나야, 웅이 2023. 8. 24. 16:00
728x90
반응형
SMALL

1. 젯슨 나노에 카메라 모듈 연결하기

MIPI-CSI (Mobile Industry Processor Interface - Camera Serial Interface)방식의 카메라 모듈을 젯슨 나노 카메라 슬롯에 파란색의 연결 부분이 바깥쪽으로 향하게 연결한다.

 

카메라는 8MP IMX219 Stosck Lens Fixed Focus(SKU:B0191)를 사용한다.

카메라 스펙은 아래 표와 같다.

사이즈 25 x 24 x 9 mm
무게 3g
해상도 8 MegaPixels
Frame rate 8MP/21fps
1080p/60fps
720p/180fps
Sensor Sony IMX219
Sensor resolution 2280 x2464 pixels
Sensor Image Area 3.68 x 2.76 mm (4.6mm diagonal)
Pixel size 1.12 micro meter x 1.12 micro meter
Optical size 1/4"
Horizontal field of view 62.2 degrees
Vertical field of view 488 degrees
Focal ratio (F-stop) 2.0

 

이 카메라는 드라이버를 설치할 필요가 없다.

젯슨나노 CSI 카메라 사용을 위한 기본 프로그램은 nvarguscamerasrc 프로그램이며, GStreamer방식으로 pipeline을 생성하여 사용하거나, nvgstcapture라는 유틸리티 프로그램을 사용하여 작동시킨다.

 

GStreamer pipeline 방식은 주로 프로그램에서 카메라를 제어할 때 사용한다. nvgstcaputre 유틸리티 프로그램은 키보드 명령어로 카메라를 제어하면서 촬영하고자 할 때 사용한다.

 

2. 카메라 사용하기

젯슨 나노 터미널을 열고, 촬영 option 지정하여 촬영하기 위하여 아래 커멘드를 입력한다.

DISPLAY=:0.0 gst-launch-1.0 nvarguscamerasrc ! ‘video/x-raw(memory:NVMM), width=3280, height=2464, format=(string)NV12, framerate=(fraction)20/1’ ! nvoverlaysink -e

 

단순 프리뷰로 보려면 아래 커맨드를 입력한다.

gst-launch-1.0 nvarguscamerasrc ! nvoverlayshink

 

60fps / 1280x720 옵션을 사용하려면 아래 커맨드를 입력한다.

DISPLAY=:0.0 gst-launch-1.0 nvarguscamerasrc ! ‘video/x-raw(memory:NVMM), width=1280, height=720, format=(string)NV12, framerate=(fraction)60/1’ ! nvoverlaysink -e

 

또는 파이썬 코드를 통해서 간단하게 실행가능하다.

 

아래 파이썬 코드를 simple_camera.py 파일명으로 저장한다. 

import cv2

""" 
gstreamer_pipeline returns a GStreamer pipeline for capturing from the CSI camera
Flip the image by setting the flip_method (most common values: 0 and 2)
display_width and display_height determine the size of each camera pane in the window on the screen
Default 1280x720 displayd in a 1280x720 size window
"""

def gstreamer_pipeline(
    sensor_id=0,
    capture_width=1280,
    capture_height=720,
    display_width=1280,
    display_height=720,
    framerate=60,
    flip_method=0,
):
    return (
        "nvarguscamerasrc sensor-id=%d ! "
        "video/x-raw(memory:NVMM), width=(int)%d, height=(int)%d, framerate=(fraction)%d/1 ! "
        "nvvidconv flip-method=%d ! "
        "video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
        "videoconvert ! "
        "video/x-raw, format=(string)BGR ! appsink"
        % (
            sensor_id,
            capture_width,
            capture_height,
            framerate,
            flip_method,
            display_width,
            display_height,
        )
    )


def show_camera():
    window_title = "CSI Camera"

    # To flip the image, modify the flip_method parameter (0 and 2 are the most common)
    print(gstreamer_pipeline(flip_method=0))
    video_capture = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
    if video_capture.isOpened():
        try:
            window_handle = cv2.namedWindow(window_title, cv2.WINDOW_AUTOSIZE)
            while True:
                ret_val, frame = video_capture.read()
                # Check to see if the user closed the window
                # Under GTK+ (Jetson Default), WND_PROP_VISIBLE does not work correctly. Under Qt it does
                # GTK - Substitute WND_PROP_AUTOSIZE to detect if window has been closed by user
                if cv2.getWindowProperty(window_title, cv2.WND_PROP_AUTOSIZE) >= 0:
                    cv2.imshow(window_title, frame)
                else:
                    break 
                keyCode = cv2.waitKey(10) & 0xFF
                # Stop the program on the ESC key or 'q'
                if keyCode == 27 or keyCode == ord('q'):
                    break
        finally:
            video_capture.release()
            cv2.destroyAllWindows()
    else:
        print("Error: Unable to open camera")


if __name__ == "__main__":
    show_camera()

 

아래 커맨드를 사용하여 저장한 파이썬 코드를 실행한다.

$ python simple_camera.py

 

3. 카메라 옵션 설명

sensor_id = 0
카메라 센서 1 개 연결 했을 때는 Sensor_id = 0
2개의 카메라를 사용할 경우 Sensor_id = 0, Sensor_id = 1 
센서 아이디 값을 사용한다.

 

caputre_width, caputre_height
촬영 해상도를 의미한다.

 

display_width, display_height
촬영 영상 윈도우 창 사이즈를 의미한다.

 

framerate = 60
60fps로 촬영

 

file-method = 0
카메라 촬영 방향을 지정하는 옵션이다.
0은 none
1은 반시계 방향 90도 회전
2는 180도 회전
3은 시계방향 90도 회전
4는 수평으로 뒤집기
5는 오른쪽 위/왼쪽 아래 대각선으로 뒤집기
6은 수직으로 뒤집기
7은 왼쪽 위/아래로 뒤집기

 

video/x-raw, width=960, height=616
preview하거나 저장할 영상의 size를 의미한다.

 

728x90
반응형
LIST

'임베디드 > Jetson' 카테고리의 다른 글

Jetson Nano OS 설치  (0) 2023.08.24