AI/vision

영상 처리를 위한 OpenCV 라이브러리

민사민서 2024. 8. 29. 16:07

https://github.com/opencv/opencv

 

GitHub - opencv/opencv: Open Source Computer Vision Library

Open Source Computer Vision Library. Contribute to opencv/opencv development by creating an account on GitHub.

github.com

https://docs.opencv.org/4.x/d6/d00/tutorial_py_root.html

 

OpenCV: OpenCV-Python Tutorials

Core Operations In this section you will learn basic operations on image like pixel editing, geometric transformations, code optimization, some mathematical tools etc.

docs.opencv.org

 

  • Homebrew 설치 (없다면):
  • 필요한 라이브러리 설치:
    • OpenCV를 설치하기 전에 필요한 라이브러리를 Homebrew를 사용하여 설치합니다:
    • brew install cmake; brew install pkg-config; brew install ffmpeg; brew install numpy;
  • Python 가상환경 활성화:
    • 이미 설정한 .venv 가상환경을 활성화합니다. 예를 들어, 프로젝트 디렉토리에서 다음 명령어를 입력합니다:
    • source .venv/bin/activate
  • OpenCV-Python 설치:
    • pip를 사용하여 OpenCV-Python을 설치합니다:
    • pip install opencv-python
  • 설치 확인:
    • Python 인터프리터를 열고 OpenCV가 정상적으로 설치되었는지 확인합니다:
    • import cv2 print(cv2.__version__)

 

https://www.kaggle.com/code/shakhrulsiam/bufferless-video-capture-opencv

 

Bufferless-video-capture-Opencv

Explore and run machine learning code with Kaggle Notebooks | Using data from No attached data sources

www.kaggle.com

cv2.VideoCapture()로 rtsp stream 에서 프레임을 가져와서 처리하다보면

수신 속도 > 처리 속도로 인해 "프레임이 실제로 처리되는 시간"과 "프레임이 도착한 시간" 이 버퍼가 찰수록 계속 늘어남

- timestamp를 부여하거나

- 가장 최근에 수신된 프레임으로 어떤 작업을 하려고 할 때 방해가 되겠죠

 

멀티쓰레드 + 버퍼 조합을 활용해 처리 시점에서 가장 latest 프레임만 handling하도록 코드를 짤 수 있음 (위 링크 참고)

def _reader(self) -> None:
    while True:
        ret, frame = self.cap.read()
        if not ret:
            print("Can't receive frame (stream end?). Exiting ...")
            break

        # Clear the queue if it's not empty to maintain only the latest frame
        while not self.q.empty():
            try:
                self.q.get_nowait()
            except queue.Empty:
                pass
        self.q.put(frame)

def read(self) -> cv.Mat | None:
    return self.q.get()

 

이 외에도 cv 엄청난게 많던데요 - 이모저모에서 이어서 작성

'AI > vision' 카테고리의 다른 글

camera calibration using OpenCV  (0) 2024.08.29
OpenCV 이모저모  (0) 2024.08.29