머신러닝/openCV

openCV 기초, mediapipe 설치 에러 해결하기, dll문제

Olivia-BlackCherry 2025. 3. 4. 08:21

 
1. 발생한 에러
ImportError: DLL load failed while importing _framework_bindings: DLL 초기화 루틴을 실행할 수 없습니다. mediapipe 를 임포트 하려고 하는 중에 발생한 에러였다. 
 
이를 해결하기 위한 방법으로 챗이 제안한 것은 다양했다. 다른 방법으로는 해결되지 않았다. 마지막 방법이었던 파이썬 3.9를 설치하니 바로 해결! 결국 파이썬 버전이 맞지 않아서 되지 않았던 것이다. 
 
찾아보니, 나와 같은 mediapipe 설치 에러 문제가 많이 발생하는데 설치한 패키지와 파이썬 버전의 호환 때문이 대다수다. 
 
파이썬을 설치하는 방법은 아래의 공식홈페이지에서 3.9 버전 중 하나를 설치하는 것
https://www.python.org/downloads/windows/

 

Python Releases for Windows

The official home of the Python Programming Language

www.python.org

 
또는 파이참-설정-인터프리터에서 add 버튼을 눌러 그 자리에서 설치하는 방법이 있다. 결론적으로 이 방법이 더욱 간단했다.

 
 
2. mediapipe
1) mediapipe는?
구글에서 제공하는 라이브러리로 손, 얼굴, 몸 등 다양한 신체 부위의 랜드마크를 추적하는 데 사용된다.
 
2) 코드

import cv2
import mediapipe as mp
#Create a Video Capture Object 비디오 캡처 객체 생성
cap = cv2.VideoCapture(0)

#MediaPipe
mpHands = mp.solutions.hands #손 추적에 필요한 기능 제공되는 모듈
hands = mpHands.Hands() #손의 랜드마크를 인식하고 추적하는 객체 생성

#Draw
mpDraw = mp.solutions.drawing_utils #랜드마크를 화면에 그릴 때 사용하는 유틸리티

#웹캠에서 실시간 비디오 처리
while True:
    ret, frame = cap.read()
    if ret: #성공여부확인
        frameRGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) #mediapipe RGB 사용
        results= hands.process(frameRGB) #손의 랜드마크를 인식하는 함수
        if results.multi_hand_landmarks: #손의 랜드마크가 인식되었다면?
            #handtype:오?왼?
            for handType, handLMS in zip(results.multi_handedness, results.multi_hand_landmarks):
                #실제 좌표로 변환
                for id, lm in enumerate(handLMS.landmark):
                    h, w, c = frame.shape
                    cx, cy, cz = int(lm.x*w), int(lm.y*h), int(lm.z*w)
                    print(id, lm)
                    cv2.circle(frame, (cx,cy), 10, (255,143,0), cv2.FILLED)
                mpDraw.draw_landmarks(frame, handLMS, mpHands.HAND_CONNECTIONS)
        cv2.imshow("Frame", frame)
        if cv2.waitKey(1) & 0xFF == ord('1'):
            break
    else:
        break

cap.release()
cv2.destroyAllWindows()

 
fps 시간 넣기

import cv2
import mediapipe as mp
import time
#Create a Video Capture Object 비디오 캡처 객체 생성
cap = cv2.VideoCapture(0)

#MediaPipe
mpHands = mp.solutions.hands #손 추적에 필요한 기능 제공되는 모듈
hands = mpHands.Hands() #손의 랜드마크를 인식하고 추적하는 객체 생성

#Draw
mpDraw = mp.solutions.drawing_utils #랜드마크를 화면에 그릴 때 사용하는 유틸리티

#시간
pTime = 0
cTime = 0

#웹캠에서 실시간 비디오 처리
while True:
    ret, frame = cap.read()
    if ret: #성공여부확인
        frameRGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) #mediapipe RGB 사용
        results= hands.process(frameRGB) #손의 랜드마크를 인식하는 함수
        if results.multi_hand_landmarks: #손의 랜드마크가 인식되었다면?
            #handtype:오?왼?
            for handType, handLMS in zip(results.multi_handedness, results.multi_hand_landmarks):
                #실제 좌표로 변환
                for id, lm in enumerate(handLMS.landmark):
                    h, w, c = frame.shape
                    cx, cy, cz = int(lm.x*w), int(lm.y*h), int(lm.z*w)
                    print(id, lm)
                    cv2.circle(frame, (cx,cy), 10, (255,143,0), cv2.FILLED)
                mpDraw.draw_landmarks(frame, handLMS, mpHands.HAND_CONNECTIONS)
                if handType.classification[0].label =='Right':
                    type = "Left"
                else:
                    type = "Right"
        # 프레임속도 계산하여 글씨 넣기
        cTime = time.time()
        fps = 1 / (cTime -pTime) #초당프레임
        pTime = cTime
        cv2.putText(frame, str(int(fps)), (10,70), cv2.FONT_HERSHEY_COMPLEX, 3, (255,0,0),3)
        cv2.imshow("Frame", frame)
        if cv2.waitKey(1) & 0xFF == ord('1'):
            break
    else:
        break

cap.release()
cv2.destroyAllWindows()