By Music Juggler


2018-06-13 14:37:10 8 Comments

Ok, so I've been having a problem with some tracking code(I'm not too skilled, please bear this in mind). It is meant to track objects(Juggling balls) of one colour and output a .csv data file.

            import cv2
            import numpy as np

            #h,s,v range of the object to be tracked
            h,s,v,h1,s1,v1 =  31,91,0,74,255,255#GREEN
            #h,s,v,h1,s1,v1 = 0, 161, 52, 26 ,255, 255 #Orange
            #h,s,v,h1,s1,v1 = 90, 37, 0, 143, 180, 255 #Blue
            threshold_value = 0

            output_path = ('C:\\Python27')

            cap = cv2.VideoCapture('Users\Tyson\Desktop\MillsMess.mp4')


            #takes an image, and a lower and upper bound
            #returns only the parts of the image in bounds
            def only_color(frame, (b,r,g,b1,r1,g1)):
                # Convert BGR to HSV
                hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
                # define range of blue color in HSV
                lower = np.array([b,r,g])
                upper = np.array([b1,r1,g1])
                # Threshold the HSV image to get only blue colors
                mask = cv2.inRange(hsv, lower, upper)
                # Bitwise-AND mask and original image
                res = cv2.bitwise_and(frame,frame, mask= mask)
                return res, mask

            #finds the largest contour in a list of contours
            #returns a single contour
            def largest_contour(contours):
                c = max(contours, key=cv2.contourArea)
                return c[0]

            #takes an image and the threshold value returns the contours
            def get_contours(im, threshold_value):
                imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
                _ ,thresh = cv2.threshold(imgray,threshold_value,255,0)
                _, contours, _ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
                return contours

            #finds the center of a contour
            #takes a single contour
            #returns (x,y) position of the contour
            def contour_center(c):
                M = cv2.moments(c)
                try: center = int(M['m10']/M['m00']), int(M['m01']/M['m00'])
                except: center = 0,0
                return center

            frame_number = 0
            positions = []
            for i in range(1000000): positions.append((0,0))
            #main loop of the program
            while True:
                #read image from the video
                _, img = cap.read()

                try: l = img.shape
                except: break
                #extract only the flesh tones
                img, mask = only_color(img, (h,s,v,h1,s1,v1))
                #find the contours in the image
                contours = get_contours(img, threshold_value)
                #if there are contours found in the image:
                if len(contours)>0:
                    try:
                        #sort the contours by area
                        c = max(contours, key=cv2.contourArea)
                        img = cv2.drawContours(img, c ,-1, (0,0,255), 14)
                        positions[frame_number] = contour_center(c)
                    except: pass        
                frame_number += 1
                #show the image and wait
                #cv2.imshow('img', img)
                cv2.imshow('img', cv2.resize(img, (480,700)))
                k=cv2.waitKey(1)
                if k==27: break

            #release the video to avoid memory leaks, and close the window
            cap.release()
            cv2.destroyAllWindows()

            #remove unused parts of the list
            positions = positions[:frame_number]

            print 'finished tracking'
            #write data
            import csv
            with open(output_path, 'w') as csvfile:
                fieldnames = ['x_position', 'y_position']
                writer = csv.DictWriter(csvfile, fieldnames = fieldnames)
                writer.writeheader()
                for position in positions:
                    x, y = position[0], position[1]
                    writer.writerow({'x_position': x, 'y_position': y})

            print 'finished writing data'
            print output_path

And I get this Error

finished tracking

Traceback (most recent call last):
  File "C:\Users\Tyson\Desktop\Code\Tracker.py", line 90, in <module>
    with open(output_path, 'w') as csvfile:
IOError: [Errno 13] Permission denied: 'C:\\Python27'

I have tried fixing it, but nothing seems to work. Is there a native folder I could save it to that is writable? Or how can I give permission?

Thanks in advance!

1 comments

@root 2018-06-13 14:40:29

The line output_path = ('C:\\Python27') later causes open(output_path, 'w') to fail. Set output_path to something else.

@Music Juggler 2018-06-13 23:07:28

Where would I set it to?

@root 2018-06-14 00:13:10

Looks like it should be a CSV file name (not a directory name) and it should be one to which you have writing permissions. 'C:\myfile.csv' or 'C:\Documents\myfile.csv' or so.

Related Questions

Sponsored Content

8 Answered Questions

[SOLVED] Python - IOError: [Errno 13] Permission denied:

  • 2012-05-13 22:29:48
  • Itzik984
  • 255102 View
  • 42 Score
  • 8 Answer
  • Tags:   python file io

2 Answered Questions

[SOLVED] IOError: [Errno 13] Permission denied

  • 2015-03-29 16:43:25
  • João Pedro
  • 74862 View
  • 5 Score
  • 2 Answer
  • Tags:   python

1 Answered Questions

1 Answered Questions

[SOLVED] Opening CSV IOError: [Errno 13] Permission Denied

  • 2017-04-26 13:20:44
  • ALUW
  • 1975 View
  • 1 Score
  • 1 Answer
  • Tags:   python csv

1 Answered Questions

[SOLVED] openCV: cannot detect small shapes using findContours

  • 2017-10-08 07:40:30
  • Vic
  • 493 View
  • 1 Score
  • 1 Answer
  • Tags:   python opencv

0 Answered Questions

IOError: [Errno 13] Permission denied:

1 Answered Questions

[SOLVED] Python / OpenCV - Intermittent error

1 Answered Questions

[SOLVED] IOError: [Errno 13] Permission denied

0 Answered Questions

IOError: [Errno 13] Permission denied while reading csv file in python

  • 2013-04-22 13:16:47
  • PBD
  • 2177 View
  • 0 Score
  • 0 Answer
  • Tags:   python

2 Answered Questions

[SOLVED] IOError: [Errno 13] Permission denied:

Sponsored Content