【问题标题】:Unknown problem with module using CV2 and mediapipe使用 CV2 和 mediapipe 的模块的未知问题
【发布时间】:2022-01-17 11:38:16
【问题描述】:

我在 python 3.10.1 上运行该模块时遇到了一些问题。这是我的代码:

import mediapipe as mp
import cv2
import time

class handDetector:

    
    def __init__(self, mode=False, maxHands=2, complexity = 1, detectionCon=0.5, trackCon=0.5):
        self.mode = mode
        self.maxHands = maxHands
        self.complexity = complexity
        self.detectionCon = detectionCon
        self.trackCon = trackCon


        self.mpDraw = mp.solutions.drawing_utils

    def FindHands(self, img, draw = True):
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        results = self.hands.process(imgRGB)
        landmarks = results.multi_hand_landmarks
        #print(results.multi_hand_landmarks)
        if landmarks:
        
            for handLms in landmarks:
                if draw:
                    self.mpDraw.draw_landmarks(img,
                                               handLms,
                                               self.mpHands.HAND_CONNECTIONS)
        return img   
                      
                
                
            #for id, lm in enumerate(handLms.landmark):
                #print(id,lm)
            #    height, width, c = img.shape
            #    cx, cy = int(lm.x*width), int(lm.y*height)
            #    print(id, ", x=",cx, ", y=",cy)
            #    if id%10 == 0:
            #        cv2.circle(img, (cx,cy), 8, (255,0,255), cv2.FILLED)
        



def main():
    pTime = 0
    cTime = 0
    cap = cv2.VideoCapture(1)
    detector = handDetector()
    while True:
        success, img = cap.read()
        img = detector.FindHands(img)
        cTime = time.time()
        fps = 1/(cTime-pTime)
        pTime = cTime
        cv2.putText(img, str(int(fps)), (10,70),
                    cv2.FONT_HERSHEY_PLAIN, 3,(255,0,255), 3)
        cv2.imshow("Image",img)
        cv2.waitKey(1)

if __name__ == '__main__':
    main()

它返回给我以下回溯:

 Traceback (most recent call last):
  File "C:\Users\Eduardo.PC\Documents\UNAM 2020-24\COMPU\HandTracker\HandTrackModule.py", line 65, in <module>
    main()
  File "C:\Users\Eduardo.PC\Documents\UNAM 2020-24\COMPU\HandTracker\HandTrackModule.py", line 55, in main
    img = detector.FindHands(img)
  File "C:\Users\Eduardo.PC\Documents\UNAM 2020-24\COMPU\HandTracker\HandTrackModule.py", line 21, in FindHands
    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(4.5.4) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

我正在关注计算机视觉的 youtube 在线课程linked here。在大约 30 分钟时,添加了上面介绍的模块。我几乎复制了它,但它不起作用。发生了什么事?

感谢和抱歉发了这么长的帖子

【问题讨论】:

  • 请发布回溯,使其可读edit你的帖子来改变它。
  • 完成。谢谢
  • if not success: break

标签: python computer-vision mediapipe


【解决方案1】:

试试这个

import mediapipe as mp
import cv2
import time

class handDetector:

    
    def __init__(self, mode=False, maxHands=2, complexity = 1, detectionCon=0.5, trackCon=0.5):
        self.mode = mode
        self.maxHands = maxHands
        self.complexity = complexity
        self.detectionCon = detectionCon
        self.trackCon = trackCon


        self.mpDraw = mp.solutions.drawing_utils

    def FindHands(self, img, draw = True):
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        results = self.hands.process(imgRGB)
        landmarks = results.multi_hand_landmarks
        #print(results.multi_hand_landmarks)
        if landmarks:
        
            for handLms in landmarks:
                if draw:
                    self.mpDraw.draw_landmarks(img,
                                               handLms,
                                               self.mpHands.HAND_CONNECTIONS)
        return img   
                      
                
                
            #for id, lm in enumerate(handLms.landmark):
                #print(id,lm)
            #    height, width, c = img.shape
            #    cx, cy = int(lm.x*width), int(lm.y*height)
            #    print(id, ", x=",cx, ", y=",cy)
            #    if id%10 == 0:
            #        cv2.circle(img, (cx,cy), 8, (255,0,255), cv2.FILLED)
        



def main():
    pTime = 0
    cTime = 0
    cap = cv2.VideoCapture(1)
    detector = handDetector()
    while True:
        success, img = cap.read()
        if not success:
            break
        img = detector.FindHands(img)
        cTime = time.time()
        fps = 1/(cTime-pTime)
        pTime = cTime
        cv2.putText(img, str(int(fps)), (10,70),
                    cv2.FONT_HERSHEY_PLAIN, 3,(255,0,255), 3)
        cv2.imshow("Image",img)
        cv2.waitKey(1)

if __name__ == '__main__':
    main()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 2021-08-25
    • 2022-06-15
    相关资源
    最近更新 更多