Caga tió is a character in Catalan mythology relating to a Christmas tradition widespread in Catalonia. Created to expand the Catalan tradition without the need of traveling with a huge tronc in a plane/car, this code created a virtual reality caga tió. The program uses the cv2 library that does motion tracking. Detecting the different parts of the body, we are going to interact with the pictures that appear on the screen.

The steps of the code are based on the caga tió tradition process:

Step 1:

Take the stick: If the coordinates of the hand pass through the stick, then print the stick in the coordinates of the hand.

Step 2:

Heat the stick: If the coordinates of the stick are inside the oven, make a countdown of 5 seconds.

Step 3:

Hit the caga tió: If the coordinates of the stick are touching the caga tió, it counts as a hit. The program takes around 12-15 hits to make the caga tió poop.

Step 4:

Open the presents: Before executing the code, there is a variable “location_presents” to write where the presents are and make the person go and get them.

Code:

import cv2
import mediapipe as mp
import time

location_presents = "Under the bed"

cap = cv2.VideoCapture(0)


mpDraw = mp.solutions.drawing_utils
mpPose = mp.solutions.pose
pose = mpPose.Pose()

count = 0
hoven = 0
stick = 0
passs = 0

# Restat variables:
pTime = 0
last_seg = 61
i = 0

x_list = []
y_list = []

while True:
    # get the imatge from the camera and convert it to RGB
    success, img = cap.read()
    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    results = pose.process(imgRGB)
    
    if results.pose_landmarks:
        #Enumerate the difertns part ofthe body:
        for id, lm in enumerate(results.pose_landmarks.landmark):
            h, w, c = img.shape
            cx, cy = int(lm.x * w) ,int(lm.y * h)
            
            
            
            #First level to take the stick:
            if (hoven == 0) and (stick == 0):
                
                # Text:
                cv2.putText(img, "Take the stick :) ", (40, 150), cv2.FONT_HERSHEY_PLAIN, 3, (0,0,255), 3)
                
                # Stick:
                cv2.line(img, (100, 250), (175, 150), (19,69,139), thickness = 10)
      
            # If left hand pass throug the stick, print it in the hand and level one passed:
            if (id == 20) and (cx > 100) and (cx < 175) and (cy > 150 ) and (cy < 250) and (stick == 0):
                
                # Position of the stick:
                stick_x_1 = cx - 75
                stick_y_1 = cy - 100
                
                #Print stick:
                cv2.line(img, (cx, cy), (stick_x_1, stick_y_1), (45,82,160), thickness = 10)
                
                # Pass the level
                stick = 1
            
            # Level 2 put the stick in the hoven:
            if (hoven == 0 ) and (stick == 1) and (id == 20):
                
                # Text:
                cv2.putText(img, "Put the stick in the oven", (10, 150), cv2.FONT_HERSHEY_PLAIN, 2, (255,255,255), 3)
                
                # Positions stick:
                stick_x_2 = cx - 90
                stick_y_2 = cy - 10
                
                #Print new stick
                cv2.line(img, (cx, cy), (stick_x_2, stick_y_2), (45,82,160), thickness = 10)
                
                
                ## Print the Hoven 
                cv2.rectangle(img, pt1=(0,500), pt2=(350,250), color=(220,220,220), thickness= -1)               
                cv2.rectangle(img, pt1=(50,450), pt2=(300,300), color=(0,0,0), thickness= -1)
                cv2.rectangle(img, pt1=(50,360), pt2=(300,360), color=(90,90,90), thickness= -1)
                cv2.rectangle(img, pt1=(50,390), pt2=(300,390), color=(90,90,90), thickness= -1)
                cv2.rectangle(img, pt1=(50,420), pt2=(300,420), color=(90,90,90), thickness= -1)
                cv2.rectangle(img, pt1=(150,310), pt2=(200,315), color=(100,100,100), thickness= -1)
                
                #cv2.line(img, (0, 300), (130, 300), (19,69,139), thickness = 9)  
                
                # Counter
                passs = passs + 1
     
                
                # Pass to the Level 3:
                if (passs >= 100):                    
                    hoven = 1  
            
            
            
            
            if (count <= 2500) and (hoven == 1):
            
                # Body
                cv2.line(img, (100, 250), (200, 260), (19,69,139), thickness = 110)
                
                # Barret:
                cv2.circle(img,(102, 195), 25, (0,0,250), cv2.FILLED)
            
                # Face 
                cv2.circle(img,(100, 250), 55, (135,184,222), cv2.FILLED)
            
                # Nouse
                cv2.circle(img,(100, 250), 5, (19,69,139), cv2.FILLED)
                # Ulls:
                cv2.circle(img,(115, 230), 8, (255,255,255), cv2.FILLED)
                cv2.circle(img,(85, 230), 8, (255,255,255), cv2.FILLED)
                cv2.circle(img,(115, 230), 2, (0,0,0), cv2.FILLED)
                cv2.circle(img,(85, 230), 2, (0,0,0), cv2.FILLED)
            
                # Patas:
                cv2.line(img, (165, 345), (130, 295), (19,69,139), thickness = 9)            
                cv2.line(img, (35, 345), (70, 295), (19,69,139), thickness = 9)
                
                # Barret:
                cv2.line(img, (80, 195), (125, 195), (0,0,0), thickness = 9)  
            
              
                # Detect hand and get the poistion to print stick:
                if (id == 20):
                
                    # Position of the stick and position to print the stick:
                    stick_x = cx - 75
                    stick_y = cy - 100
                
                    #Print stick:
                    cv2.line(img, (cx, cy), (stick_x, stick_y), (45,82,160), thickness = 10)
            
            
           
                # DEtect if the stick is touching the Caga tió.
                if (stick_x > 80) and (stick_x < 220) and (stick_y > 200 ) and (stick_y < 305):
                    
                    # Count if is touching the tio.
                    count = count + 1 
                    #cv2.putText(img, str(int(count)), (100,40), cv2.FONT_HERSHEY_PLAIN, 3, (255,0,0), 3)
                
                    # Write ouch if it touch it:
                    cv2.putText(img, "OUCH!!!!", (150, 150), cv2.FONT_HERSHEY_PLAIN, 3, (0,0,255), 3)
               
            
            
            #Display where are the presents:
            if (count > 2500):
                
                cv2.putText(img, "The caga Tio has pooped!!", (40, 150), cv2.FONT_HERSHEY_PLAIN, 2, (0,0,0), 3)                #time.sleep(30)
                
                cv2.putText(img, "Where are the presents??", (40, 250), cv2.FONT_HERSHEY_PLAIN, 2, (0,0,0), 3)
                
                cv2.putText(img, location_presents, (40, 350), cv2.FONT_HERSHEY_PLAIN, 2, (0,0,0), 3)
                
            
                # Get the x and y cor every second
            s = time.strftime("%S")
            s = int(s)         
                           
    
    cv2.imshow("Caga tió", img)
    
    # Press esc to close the windows
    key = cv2.waitKey(1)    
    if key == 27:
        break
        
cap.release()
cv2.destroyAllWindows()

Leave a Comment

Your email address will not be published. Required fields are marked *