colinlaney / animal-tracking

Object tracking with OpenCV in open field behavioral test (overhead view maze)
https://youtu.be/GebcshN4OdE
Creative Commons Zero v1.0 Universal
50 stars 23 forks source link

An added GUI with tkinter #14

Closed Hatem-Jr closed 3 years ago

Hatem-Jr commented 3 years ago

Hi I made a GUI and here's it's code: from os import close from tkinter import * from tkinter import filedialog,messagebox import track

FotaGui = Tk() FotaGui.geometry('600x600')

def file_path():

global filepath
filepath = StringVar()
#Fetch the file path of the mp4 file browsed.
if(filepath == ""):
    filepath = filedialog.askopenfilename(title = "select a file", filetypes = [("mp4 files", "*.mp4")])
else:
    filepath = filedialog.askopenfilename( initialdir=filepath, title = "select a file", filetypes = [("mp4 files", "*.mp4")])

Morris Water Maze

def MWM():

#Validation of entry fields, if left empty.
if filepath == "":
    messagebox.showinfo('Info','Please select a video')
else:
    filepathlabel.config(text=filepath)
    exec("track")

Browsebutton = Button(FotaGui,width = 18,text= "Browse",command = file_path) Browsebutton.pack()

MWMbutton = Button(FotaGui,width = 18,text="Morris Water Maze Test",command = MWM) MWMbutton.pack()

filepathlabel = Label(FotaGui,text = "mp4 file path:",font = ('Times 10')) filepathlabel.pack()

FotaGui.mainloop()

and I changed the last couple of lines in track.py to this:

for filename in glob.glob('*.mp4'):

#floorCrop(filename)

for filename in glob.glob('*.mp4'):

#file = open(RELATIVE_DESTINATION_PATH + 'distances.csv', 'a')
#trace(filename)

floorCrop(GUI.filepath) file = open(RELATIVE_DESTINATION_PATH + 'distances.csv', 'a') trace(GUI.filepath)

now there are 2 problems: 1-when the track.py file finishes execution, the .csv file erases previous results and overwrites it with new ones 2-there are no tracing images saved in the trace folder, even though in the normal code it did save tracing images

colinlaney commented 3 years ago

It's hard to read the code formatting. Hard to find entrypoint of original code. Hard to reproduce your problems, because there is no code to run. Try to fork the repository at first and make a working example, please.

Hatem-Jr commented 3 years ago

repo forked

Hatem-Jr commented 3 years ago

so did you look at it ?

colinlaney commented 3 years ago

1-when the track.py file finishes execution, the .csv file erases previous results and overwrites it with new ones

because of w parameter, it truncates the file you open. You have to check the file's existence before adding the header since column names are needed only when you are creating a new table and definitely the header has to be placed in the leading line.

2-there are no tracing images saved in the trace folder, even though in the normal code it did save tracing images

because RELATIVE_DESTINATION_PATH is relative and after you pass the file path into trace() you have to check if the file name is file name, not the path. Just debug where you are saving the images.

Hatem-Jr commented 3 years ago

but the thing is I checked if the cv2.imwrite works or not by adding this:

if not cv2.imwrite(RELATIVE_DESTINATIONPATH + 'traces/' + name + '[distance]=%.2f' % distance + '_[time]=%.1fs' % t + '.png', cv2.resize(imgTrack, (max(HD), max(HD)))): raise Exception("Could not write image")

and the exception is always raised

and If the problem is that I'm passing file path and not just the file name to the trace method then how to get the .mp4 part of the path from "GUI.filepath" only ?

colinlaney commented 3 years ago

and the exception is always raised

and why it will not if you pass the absolute path? What about just print(RELATIVE_DESTINATION_PATH + 'traces/' + name) for sanity check?

and If the problem is that I'm passing file path and not just the file name to the trace method then how to get the .mp4 part of the path from "GUI.filepath" only?

Well, folding of os.path.basename and os.path.splitext, then indexing of the extension.

Hatem-Jr commented 3 years ago

yeah you were right this line: print(RELATIVE_DESTINATION_PATH + 'traces/' + name)

produces this: 2021-07-13_distance/traces/D:/CSEN/Courses/Semester 8/Tests Video Samples/output which means that name is D:/CSEN/Courses/Semester 8/Tests Video Samples/output

now how to make it only output ?

colinlaney commented 3 years ago

now how to make it only output ?

Did you read this one:

folding of os.path.basename and os.path.splitext, then indexing of the extension

?

Nest these functions. In your case use the only os.path.basename.

Hatem-Jr commented 3 years ago

Yes I did, I just didn't understand/realize that this was the answer before doing the print statement 😂