Tech-Watt / YOUTUBE-TUTORIAL-CODES

How to Use the Repository You can browse the code for each playlist and episode by navigating to the corresponding folder in the repository. Each folder contains the code used in the video, along with any additional resources, such as data files, images, or configuration files. To use the code, simply clone the repository to your local machine and
https://www.youtube.com/@tech_watt
83 stars 42 forks source link

Script Fails at Line#86 with NoneType Error #1

Open grabani opened 1 year ago

grabani commented 1 year ago

Hi There,

When executing your code, at Line#86, I get the error:

'NoneType' object is not subscriptable

Can you help should light onto the cause?

Thanks.

Tech-Watt commented 1 year ago

Okay glad to hear from you which of the python projects??

On Mon, 23 Jan 2023 at 6:43 PM, grabani @.***> wrote:

Hi There,

When executing your code, at Line#86 https://github.com/Tech-Watt/Python-codes/blob/b723869d9891c23e0b1de5a0005f5ce46ea31f66/Ai%20dumbell%20trainer/main.py#L86, I get the error:

'NoneType' object is not subscriptable

Can you help should light onto the cause?

Thanks.

— Reply to this email directly, view it on GitHub https://github.com/Tech-Watt/Python-codes/issues/1, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2D4USSTXY3NBUWDRYNJOPLWT3GLPANCNFSM6AAAAAAUEF3DNQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

grabani commented 1 year ago

The "AI Dumbbell Trainer". If you click on the link in my issue it will take you to the exact location.

For you convenience the link is:

https://github.com/Tech-Watt/Python-codes/blob/b723869d9891c23e0b1de5a0005f5ce46ea31f66/Ai%20dumbell%20trainer/main.py#L86

Thanks again.

On Mon, 23 Jan 2023 at 18:49, Nanor Felix @.***> wrote:

Okay glad to hear from you which of the python projects??

On Mon, 23 Jan 2023 at 6:43 PM, grabani @.***> wrote:

Hi There,

When executing your code, at Line#86 < https://github.com/Tech-Watt/Python-codes/blob/b723869d9891c23e0b1de5a0005f5ce46ea31f66/Ai%20dumbell%20trainer/main.py#L86 , I get the error:

'NoneType' object is not subscriptable

Can you help should light onto the cause?

Thanks.

— Reply to this email directly, view it on GitHub https://github.com/Tech-Watt/Python-codes/issues/1, or unsubscribe < https://github.com/notifications/unsubscribe-auth/A2D4USSTXY3NBUWDRYNJOPLWT3GLPANCNFSM6AAAAAAUEF3DNQ

. You are receiving this because you are subscribed to this thread.Message ID: @.***>

— Reply to this email directly, view it on GitHub https://github.com/Tech-Watt/Python-codes/issues/1#issuecomment-1400817071, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGCKBYSRPBUA7VDYIPTWY63WT3HD7ANCNFSM6AAAAAAUEF3DNQ . You are receiving this because you authored the thread.Message ID: @.***>

Tech-Watt commented 1 year ago

Okay I will check it out

On Mon, 23 Jan 2023 at 10:40 PM, grabani @.***> wrote:

The "AI Dumbbell Trainer". If you click on the link in my issue it will take you to the exact location.

For you convenience the link is:

https://github.com/Tech-Watt/Python-codes/blob/b723869d9891c23e0b1de5a0005f5ce46ea31f66/Ai%20dumbell%20trainer/main.py#L86

Thanks again.

On Mon, 23 Jan 2023 at 18:49, Nanor Felix @.***> wrote:

Okay glad to hear from you which of the python projects??

On Mon, 23 Jan 2023 at 6:43 PM, grabani @.***> wrote:

Hi There,

When executing your code, at Line#86 <

https://github.com/Tech-Watt/Python-codes/blob/b723869d9891c23e0b1de5a0005f5ce46ea31f66/Ai%20dumbell%20trainer/main.py#L86

, I get the error:

'NoneType' object is not subscriptable

Can you help should light onto the cause?

Thanks.

— Reply to this email directly, view it on GitHub https://github.com/Tech-Watt/Python-codes/issues/1, or unsubscribe <

https://github.com/notifications/unsubscribe-auth/A2D4USSTXY3NBUWDRYNJOPLWT3GLPANCNFSM6AAAAAAUEF3DNQ

. You are receiving this because you are subscribed to this thread.Message ID: @.***>

— Reply to this email directly, view it on GitHub < https://github.com/Tech-Watt/Python-codes/issues/1#issuecomment-1400817071 , or unsubscribe < https://github.com/notifications/unsubscribe-auth/AGCKBYSRPBUA7VDYIPTWY63WT3HD7ANCNFSM6AAAAAAUEF3DNQ

. You are receiving this because you authored the thread.Message ID: @.***>

— Reply to this email directly, view it on GitHub https://github.com/Tech-Watt/Python-codes/issues/1#issuecomment-1401106334, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2D4UST3BXRE74WJGWDLU2DWT4CHHANCNFSM6AAAAAAUEF3DNQ . You are receiving this because you commented.Message ID: @.***>

grabani commented 1 year ago

Hi Nanor,

I got it working by removing the Class and function structure. See code below:

import cv2
from cvzone.PoseModule import PoseDetector
import math
import numpy as np

cap = cv2.VideoCapture(0)
detector = PoseDetector(detectionCon=0.80,trackCon=0.80)

# defining some variables
counter = 0
direction = 0

while True:
    ret, img = cap.read()
    img = cv2.resize(img,(640,480))

    detector.findPose(img,draw=0)
    lmList, bboxInfo = detector.findPosition(img, bboxWithHands=0,draw=False)
    print(type(lmList))
    print(len(lmList))

    #angle1 = angleFinder(lmList,11,13,15,12,14,16,drawPoints=True)
    # Creating Angle finder class
    p1 = 11
    p2 = 13
    p3 = 15
    p4 = 12
    p5 = 14
    p6 = 18
    drawPoints = True

    if len(lmList) != 0:
        point1 = lmList[p1]
        point2 = lmList[p2]
        point3 = lmList[p3]
        point4 = lmList[p4]
        point5 = lmList[p5]
        point6 = lmList[p6]

        x1,y1 = point1[1:-1]
        x2, y2 = point2[1:-1]
        x3, y3 = point3[1:-1]
        x4, y4 = point4[1:-1]
        x5, y5 = point5[1:-1]
        x6, y6 = point6[1:-1]
        leftHandAngle = math.degrees(math.atan2(y3 - y2, x3 - x2) -
                                    math.atan2(y1 - y2, x1 - x2))

        rightHandAngle = math.degrees(math.atan2(y6 - y5, x6 - x5) -
                                    math.atan2(y4 - y5, x4 - x5))

        leftHandAngle = int(np.interp(leftHandAngle, [-170, 180], [100, 0]))
        rightHandAngle = int(np.interp(rightHandAngle, [-50, 20], [100, 0]))

        # drawing circles and lines on selected points
        if drawPoints == True:
            cv2.circle(img, (x1, y1), 10, (0, 255, 255), 5)
            cv2.circle(img, (x1, y1), 15, (0, 255, 0), 6)
            cv2.circle(img, (x2, y2), 10, (0, 255, 255), 5)
            cv2.circle(img, (x2, y2), 15, (0, 255, 0), 6)
            cv2.circle(img, (x3, y3), 10, (0, 255, 255), 5)
            cv2.circle(img, (x3, y3), 15, (0, 255, 0), 6)
            cv2.circle(img, (x4, y4), 10, (0, 255, 255), 5)
            cv2.circle(img, (x4, y4), 15, (0, 255, 0), 6)
            cv2.circle(img, (x5, y5), 10, (0, 255, 255), 5)
            cv2.circle(img, (x5, y5), 15, (0, 255, 0), 6)
            cv2.circle(img, (x6, y6), 10, (0, 255, 255), 5)
            cv2.circle(img, (x6, y6), 15, (0, 255, 0), 6)

            cv2.line(img,(x1,y1),(x2,y2),(0,0,255),4)
            cv2.line(img, (x2, y2), (x3, y3), (0, 0, 255), 4)
            cv2.line(img, (x4, y4), (x5, y5), (0, 0, 255), 4)
            cv2.line(img, (x5, y5), (x6, y6), (0, 0, 255), 4)
            cv2.line(img, (x1, y1), (x4, y4), (0, 0, 255), 4)

            hands = list([leftHandAngle,rightHandAngle])

       # calculating angle for left and right hands

        #hands = angle1.angle()
            left, right = hands[0:]

            # Counting number of shoulder ups
            if left >= 90 and right >= 90:
                if direction == 0:
                    counter += 0.5
                    direction = 1
            if left <= 70 and right <= 70:
                if direction == 1:
                    counter += 0.5
                    direction = 0

        #putting scores on the screen
        cv2.rectangle(img,(0,0),(120,120),(255,0,0),-1)
        cv2.putText(img,str(int(counter)),(1,70),cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,1.6,(0,0,255),6)

        # Converting values for rectangles
        leftval = np.interp(left,[0,100],[400,200])
        rightval = np.interp(right, [0, 100], [400, 200])

        # For color changing
        value_left = np.interp(left, [0, 100], [0, 100])
        value_right = np.interp(right,  [0, 100], [0, 100])

        # Drawing right rectangle and putting text
        cv2.putText(img,'R',(24,195),cv2.FONT_HERSHEY_DUPLEX,1,(255, 0, 0),5)
        cv2.rectangle(img,(8,200),(50,400),(0,255,0),5)
        cv2.rectangle(img, (8, int(rightval)), (50, 400), (255, 0, 0), -1)

        # Drawing right rectangle and putting text
        cv2.putText(img, 'L', (604,195),cv2.FONT_HERSHEY_DUPLEX, 1, (255, 0, 0),5)
        cv2.rectangle(img, (582, 200), (632, 400), (0, 255, 0), 5)
        cv2.rectangle(img, (582, int(leftval)), (632, 400), (255, 0, 0), -1)

        if value_left > 70:
            cv2.rectangle(img, (582, int(leftval)), (632, 400), (0, 0, 255), -1)

        if value_right > 70:
            cv2.rectangle(img, (8, int(rightval)), (50, 400), (0, 0, 255), -1)

    cv2.imshow("Image",img)
    cv2.waitKey(1)
Tech-Watt commented 1 year ago

Awesome, thanks! You did a brilliant job

On Tue, 24 Jan 2023 at 10:51 PM, grabani @.***> wrote:

Hi Nanor,

I got it working by remiving the class and function structure. See code below:

import cv2 from cvzone.PoseModule import PoseDetector import math import numpy as np

cap = cv2.VideoCapture(0) detector = PoseDetector(detectionCon=0.80,trackCon=0.80) defining some variables

counter = 0 direction = 0

while True: ret, img = cap.read() img = cv2.resize(img,(640,480))

detector.findPose(img,draw=0) lmList, bboxInfo = detector.findPosition(img, bboxWithHands=0,draw=False) print(type(lmList)) print(len(lmList))

angle1 = angleFinder(lmList,11,13,15,12,14,16,drawPoints=True)

Creating Angle finder class

p1 = 11 p2 = 13 p3 = 15 p4 = 12 p5 = 14 p6 = 18 drawPoints = True

if len(lmList) != 0: point1 = lmList[p1] point2 = lmList[p2] point3 = lmList[p3] point4 = lmList[p4] point5 = lmList[p5] point6 = lmList[p6]

x1,y1 = point1[1:-1]
x2, y2 = point2[1:-1]
x3, y3 = point3[1:-1]
x4, y4 = point4[1:-1]
x5, y5 = point5[1:-1]
x6, y6 = point6[1:-1]
leftHandAngle = math.degrees(math.atan2(y3 - y2, x3 - x2) -
                            math.atan2(y1 - y2, x1 - x2))

rightHandAngle = math.degrees(math.atan2(y6 - y5, x6 - x5) -
                            math.atan2(y4 - y5, x4 - x5))

leftHandAngle = int(np.interp(leftHandAngle, [-170, 180], [100, 0]))
rightHandAngle = int(np.interp(rightHandAngle, [-50, 20], [100, 0]))

# drawing circles and lines on selected points
if drawPoints == True:
    cv2.circle(img, (x1, y1), 10, (0, 255, 255), 5)
    cv2.circle(img, (x1, y1), 15, (0, 255, 0), 6)
    cv2.circle(img, (x2, y2), 10, (0, 255, 255), 5)
    cv2.circle(img, (x2, y2), 15, (0, 255, 0), 6)
    cv2.circle(img, (x3, y3), 10, (0, 255, 255), 5)
    cv2.circle(img, (x3, y3), 15, (0, 255, 0), 6)
    cv2.circle(img, (x4, y4), 10, (0, 255, 255), 5)
    cv2.circle(img, (x4, y4), 15, (0, 255, 0), 6)
    cv2.circle(img, (x5, y5), 10, (0, 255, 255), 5)
    cv2.circle(img, (x5, y5), 15, (0, 255, 0), 6)
    cv2.circle(img, (x6, y6), 10, (0, 255, 255), 5)
    cv2.circle(img, (x6, y6), 15, (0, 255, 0), 6)

    cv2.line(img,(x1,y1),(x2,y2),(0,0,255),4)
    cv2.line(img, (x2, y2), (x3, y3), (0, 0, 255), 4)
    cv2.line(img, (x4, y4), (x5, y5), (0, 0, 255), 4)
    cv2.line(img, (x5, y5), (x6, y6), (0, 0, 255), 4)
    cv2.line(img, (x1, y1), (x4, y4), (0, 0, 255), 4)

    hands = list([leftHandAngle,rightHandAngle])

calculating angle for left and right hands

#hands = angle1.angle()
    left, right = hands[0:]

    # Counting number of shoulder ups
    if left >= 90 and right >= 90:
        if direction == 0:
            counter += 0.5
            direction = 1
    if left <= 70 and right <= 70:
        if direction == 1:
            counter += 0.5
            direction = 0

#putting scores on the screen
cv2.rectangle(img,(0,0),(120,120),(255,0,0),-1)
cv2.putText(img,str(int(counter)),(1,70),cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,1.6,(0,0,255),6)

# Converting values for rectangles
leftval = np.interp(left,[0,100],[400,200])
rightval = np.interp(right, [0, 100], [400, 200])

# For color changing
value_left = np.interp(left, [0, 100], [0, 100])
value_right = np.interp(right,  [0, 100], [0, 100])

# Drawing right rectangle and putting text
cv2.putText(img,'R',(24,195),cv2.FONT_HERSHEY_DUPLEX,1,(255, 0, 0),5)
cv2.rectangle(img,(8,200),(50,400),(0,255,0),5)
cv2.rectangle(img, (8, int(rightval)), (50, 400), (255, 0, 0), -1)

# Drawing right rectangle and putting text
cv2.putText(img, 'L', (604,195),cv2.FONT_HERSHEY_DUPLEX, 1, (255, 0, 0),5)
cv2.rectangle(img, (582, 200), (632, 400), (0, 255, 0), 5)
cv2.rectangle(img, (582, int(leftval)), (632, 400), (255, 0, 0), -1)

if value_left > 70:
    cv2.rectangle(img, (582, int(leftval)), (632, 400), (0, 0, 255), -1)

if value_right > 70:
    cv2.rectangle(img, (8, int(rightval)), (50, 400), (0, 0, 255), -1)

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

— Reply to this email directly, view it on GitHub https://github.com/Tech-Watt/Python-codes/issues/1#issuecomment-1402792986, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2D4USXGTBLUY2OV542WQWTWUBMGTANCNFSM6AAAAAAUEF3DNQ . You are receiving this because you commented.Message ID: @.***>

grabani commented 1 year ago

Hi Nanor,

It would seem that the reason for the error in the script you provided is, Mediapipe does not (or maybe cannot??) determine pose landmarks of images where a subject's face is not recognised/present. I was testing your script with a video in which the subject's face was not recognised/present in the opening frames, which resulted in the NoneType error. I edited the video so that it did begin with the subject's face in the frame and there was no error.

Hopefully that is helpful.

PS - Do you know whether Mediapip requires the recognition of a subject's face in order to determine landmark positions?

Tech-Watt commented 8 months ago

I have updated the code no more errors