Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I recently started on a ball tracking project using open cv and python. I got the tracking to work but I wanted to store the position of the ball every frame in a txt file. Since I am a new developer I had no idea what to do. If you know how to do this please comment the updated code in this post.

Here is my code:

Python
from collections import deque
import numpy as np
import argparse
import imutils
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help="path to the (optional) video file")
args = vars(ap.parse_args())

greenLower = (16, 55, 54)
greenUpper = (35, 90, 112)

if not args.get("video", False):
    camera = cv2.VideoCapture(1)
else:
    camera = cv2.VideoCapture(args["video"])

while True:
    (grabbed, frame) = camera.read()

    if args.get("video") and not grabbed:
        break

    frame = imutils.resize(frame, width=1280)
    frame = imutils.resize(frame, height=720)
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    mask = cv2.inRange(hsv, greenLower, greenUpper)
    mask = cv2.erode(mask, None, iterations=2)
    mask = cv2.dilate(mask, None, iterations=2)

    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
    center = None

    if len(cnts) > 0:
        c = max(cnts, key=cv2.contourArea)
        ((x, y), radius) = cv2.minEnclosingCircle(c)

        if radius > 10:
            cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2)

    cv2.imshow("Frame", frame)
    cv2.imshow("Mask", mask)

    key = cv2.waitKey(1) & 0xFF

    if key == ord("q"):
        break

camera.release()
cv2.destroyAllWindows()


What I have tried:

I am a new developer who has only just started to learn how to code. I have no idea how to store the coordinates of the the ball and I tried looking on YouTube for help but I found nothing.
Posted
Updated 24-Jan-22 21:45pm

1 solution

See 7. Input and Output — Python 3.9.10 documentation[^], which explains how to format values into text, and how to write to a file. You may also like to look at json — JSON encoder and decoder — Python 3.9.10 documentation[^] which is a good way of saving and restoring information.
 
Share this answer
 
Comments
Isaac Nimco 25-Jan-22 17:47pm    
Thankyou. I'll try to get this to work. I'll let you know if it doesn't
Isaac Nimco 26-Jan-22 17:43pm    
I don't know how I would code this in. Could you please help me.
Richard MacCutchan 27-Jan-22 4:13am    
You need to explain what your problem is. The links I gave you above explain in simple terms how to save information in a file. So each time the ball moves you need to write the X and Y values to your file in order that you can later read the file back and recreate the path of the ball; assuming that is what you want your program to be able to do.
Isaac Nimco 27-Jan-22 18:12pm    
My question is how do I find find the X and Y values from this code.
Richard MacCutchan 28-Jan-22 3:17am    
Sorry, no idea. Where is the code that handles the ball movements?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900