首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在进行AR绘制时出错

在进行AR绘制时出错
EN

Stack Overflow用户
提问于 2021-10-25 08:16:54
回答 1查看 137关注 0票数 0

我试图用python和opencv库制作一个AR绘图程序,但由于某种原因,当我运行我的代码时,程序并没有划出界限,我也不知道为什么。我遵循了yt:https://www.youtube.com/watch?v=ZiwZaAVbXQo&t=201s上的本教程,并通过删除颜色选择菜单和删除HandTrackingModule.py文件对其进行了简化,因为我刚刚从cvzone.HandTrackingModule库导入了HandDetector,并编辑了一些部分,因为我使用了opencv的最新版本。

,所以错误是我的程序没有画线.

我使用的是cvzone版本1.5.2 (最新版本)

这是我的密码:

代码语言:javascript
复制
import cv2
import numpy as np
import os
from cvzone.HandTrackingModule import HandDetector #----> this is the library I imported whcih was made by him
 
brushThickness = 25

drawColor = (0, 0, 0)
 
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
 
detector = HandDetector(detectionCon=0.65,maxHands=1) #here I imported the HandDetector
xp, yp = 0, 0
imgCanvas = np.zeros((480, 640, 3), np.uint8)
 
while True:
 
    # 1. Import image
    success, img = cap.read()
    img = cv2.flip(img, 1)
 
    # 2. Find Hand Landmarks
    hands, img = detector.findHands(img, flipType=False)
    
    if hands:
        lmList = hands[0]['lmList'] #this was the change I made because my version does not support his line
        #he used : lmList = detector.findPosition(img, draw=False)
 
        if len(lmList) != 0:
    
            # tip of index and middle fingers
            x1, y1 = lmList[8][:2]
            x2, y2 = lmList[12][:2]
    
            # 3. Check which fingers are up
            fingers = detector.fingersUp(hands[0])

            # 4. If two index fingers are up - drawing mode -----> somewhere here is the error becuase my program does not draw the line
            if fingers[1] == 1 and fingers[2] == 1:
                cv2.circle(img, (x1, y1), 15, drawColor, cv2.FILLED)

                if xp == 0 and yp == 0:
                    xp, yp = x1, y1
    
                cv2.line(img, (xp, yp), (x1, y1), drawColor, brushThickness)
    
                xp, yp = x1, y1
    
            # Clear Canvas when all fingers are up
            if all (x >= 1 for x in fingers):
                imgCanvas = np.zeros((480, 640, 3), np.uint8)
    
        imgGray = cv2.cvtColor(imgCanvas, cv2.COLOR_BGR2GRAY)
        _, imgInv = cv2.threshold(imgGray, 50, 255, cv2.THRESH_BINARY_INV)
        imgInv = cv2.cvtColor(imgInv,cv2.COLOR_GRAY2BGR)
        img = cv2.bitwise_and(img, img, imgInv)
        img = cv2.bitwise_or(img, img, imgCanvas)

    cv2.imshow("Image", img)
    cv2.waitKey(1)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-11 08:05:37

我搞砸了循环指令,这是解决方案(我还补充说,在所有手指都抬起后行擦除):

代码语言:javascript
复制
import cv2
import numpy as np
from cvzone.HandTrackingModule import HandDetector


brushThickness = 25

drawColor = (255, 0, 255)
 
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
 
detector = HandDetector(detectionCon=0.65,maxHands=1)
xp, yp = 0, 0
imgCanvas = np.zeros((480, 640, 3), np.uint8)

 
while True:
 
    success, img = cap.read()
    img = cv2.flip(img, 1)
 
    hands, img = detector.findHands(img)
    
    if hands:
        lmList = hands[0]['lmList']
        if len(lmList) != 0:
            
            # tip of index and middle fingers
            x1, y1 = lmList[8][:2]
            x2, y2 = lmList[12][:2]
    
            #Check which fingers are up
            fingers = detector.fingersUp(hands[0])
            
            #Drawing Mode - Index finger is up
            if fingers == [0, 1, 0, 0, 0]:
                cv2.circle(img, (x1, y1), 15, drawColor, cv2.FILLED)
                #print("Drawing Mode")
                if xp == 0 and yp == 0:
                    xp, yp = x1, y1

                #ce je sam img pol update vsakic in nemors risat, rise v canvas 
                cv2.line(imgCanvas, (xp, yp), (x1, y1), drawColor, brushThickness)
              
                xp, yp = x1, y1

            #reset when two fingers are up
            if fingers == [0, 1, 1, 0, 0]:
                xp, yp = x1, y1
                
                
            # Clear Canvas when all fingers are up
            if all (x >= 1 for x in fingers):
                 imgCanvas = np.zeros((480, 640, 3), np.uint8)
    
    #zdruzis canvas pa img 
    imgGray = cv2.cvtColor(imgCanvas, cv2.COLOR_BGR2GRAY)
    _, imgInv = cv2.threshold(imgGray, 50, 255, cv2.THRESH_BINARY_INV)
    imgInv = cv2.cvtColor(imgInv,cv2.COLOR_GRAY2BGR)
    img = cv2.bitwise_and(img,imgInv)
    img = cv2.bitwise_or(img,imgCanvas)
 

    cv2.imshow("Image", img)
    #cv2.imshow("Canvas", imgCanvas)
    cv2.waitKey(1)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69704743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档