Skip to content

Commit

Permalink
pause, play ...
Browse files Browse the repository at this point in the history
  • Loading branch information
aguirrejuan authored Oct 5, 2020
1 parent da2cd28 commit fb455aa
Showing 1 changed file with 71 additions and 48 deletions.
119 changes: 71 additions & 48 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from tkinter import filedialog
from PIL import Image, ImageTk
import numpy as np
#from classDeepSort import DeepSort

class App:
def __init__(self, master):
Expand All @@ -22,10 +21,14 @@ def __init__(self, master):

self.MainMenu = tk.Menu(master)
self.FileMenu = tk.Menu(self.MainMenu, tearoff=0)
self.FileMenu.add_command(label="Open Image", command=self.filemenu_openi)
self.FileMenu.add_command(label="Open Video", command=self.filemenu_openv)
self.FileMenu.add_command(label="Open Stream", command=self.filemenu_opens)
self.FileMenu.add_command(label="Save Annotations")
self.FileMenu.add_command(
label="Open Image", command=self.filemenu_openi)
self.FileMenu.add_command(
label="Open Video", command=self.filemenu_openv)
self.FileMenu.add_command(
label="Open Stream", command=self.filemenu_opens)
self.FileMenu.add_command(
label="Save Annotations")
self.FileMenu.add_command(label="Save Detected File")
self.FileMenu.add_command(label="Quit", command=self.window.quit)
self.MainMenu.add_cascade(label="File", menu=self.FileMenu)
Expand All @@ -36,30 +39,30 @@ def __init__(self, master):
self.FrameLeft = tk.Frame(master, relief=tk.RAISED, bg="black")
self.FrameRight = tk.Frame(master)
self.FrameLeft.pack(side=tk.LEFT, padx=5)
self.FrameRight.pack(side=tk.RIGHT)
self.FrameChecks = tk.Frame(master, relief=tk.RAISED, bg="black")
self.FrameChecks.pack(side=tk.LEFT, padx=5)


self.FrameRight.pack(side=tk.LEFT)

# Botones
self.ButtonReproduce = tk.Button(self.FrameLeft, text="sdasd\uf04b") # 
self.ButtonStop = tk.Button(self.FrameLeft, text="\uf04d") # 
self.ButtonPause = tk.Button(self.FrameLeft, text="\uf04c") # 
self.ButtonReproduce = tk.Button(self.FrameLeft,
command=self.button_reproduce,
text="\uf04b") # 
self.ButtonStop = tk.Button(self.FrameLeft,
command=self.button_stop,
text="\uf04d") # 
self.ButtonPause = tk.Button(self.FrameLeft,
command=self.button_pause,
text="\uf04c") # 
self.ButtonRecord = tk.Button(self.FrameLeft, text="\uf94a") # 壘

#checkboxes
self.checkboxdetect = tk.Checkbutton(self.FrameChecks, text="Detector")
self.checkboxdetect.grid(row=0, column=0, padx=5, pady=5)

self.ButtonReproduce.grid(row=0, column=0, padx=5, pady=5)
self.ButtonPause.grid(row=0, column=1, padx=5, pady=5)
self.ButtonStop.grid(row=0, column=2, padx=5, pady=5)
self.ButtonRecord.grid(row=1, column=0, padx=5, pady=5)
# Image Options
# Video Options
self.MODE_VIDEO_REPRODUCE = True

# Stream Options
# Canvas

self.CanvasMainImage = tk.Canvas(
self.FrameRight,
width=self.IMG_WIDTH,
Expand All @@ -69,28 +72,19 @@ def __init__(self, master):
padx=10, pady=10,
sticky=tk.W + tk.S,
)

#variables tracker
#self.tracker = DeepSort(detector= False)



# Variables internas
self.frame = np.zeros((self.IMG_HEIGHT,self.IMG_WIDTH,3),dtype=np.uint8)
self.photo = None
self.caption = None
self.mode = None


# variables para guardar video


# Main
self.show()
self.window.mainloop()
try:
self.window.destroy()
except:
pass

self.window.destroy()


# Funciones generales
def show(self):
Expand All @@ -100,39 +94,55 @@ def show(self):
self.caption.release()
except AttributeError:
pass
frame = cv2.imread(self.filepath)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

self.frame = cv2.imread(self.filepath)
self.frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
self.frame = cv2.resize(
self.frame,
dsize=(self.IMG_WIDTH, self.IMG_HEIGHT),
interpolation=cv2.INTER_AREA)

elif self.mode == self.MODE_STREAM:
ret, frame = self.caption.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
ret, self.frame = self.caption.read()
self.frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
self.frame = cv2.resize(
self.frame,
dsize=(self.IMG_WIDTH, self.IMG_HEIGHT),
interpolation=cv2.INTER_AREA)

elif self.mode == self.MODE_VIDEO:
fps = self.caption.get(cv2.CAP_PROP_FPS)
ret, frame = self.caption.read()
ret, self.frame = self.video_handler()
if ret == True:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
self.frame = cv2.resize(
self.frame,
dsize=(self.IMG_WIDTH, self.IMG_HEIGHT),
interpolation=cv2.INTER_AREA)
else:
self.mode = None

else:
ValueError(f"invalid self.mode value: {self.mode}")

if self.mode == None:
frame = np.zeros((self.IMG_HEIGHT,self.IMG_WIDTH,3), dtype=np.uint8)
try:
self.caption.release()
except AttributeError:
pass


#frame,_ = self.tracker(frame)
frame = cv2.resize(frame,dsize=(self.IMG_WIDTH, self.IMG_HEIGHT),interpolation=cv2.INTER_AREA)
self.photo = ImageTk.PhotoImage(image=Image.fromarray(frame))

self.photo = ImageTk.PhotoImage(image=Image.fromarray(self.frame))
self.CanvasMainImage.create_image(0, 0, image=self.photo, anchor=tk.NW)
self.window.after(int(1000 * (1/fps)), self.show)


def video_handler(self):
if self.MODE_VIDEO_REPRODUCE or self.photo == None:
return self.caption.read()
else:
return True, cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)



# Funciones widgets

# Menus
Expand All @@ -147,8 +157,6 @@ def filemenu_openi(self):
("png files", "*.png"),))

self.mode = self.MODE_IMG
self.tracker = DeepSort(detector= True)


def filemenu_openv(self):
"""Open Video"""
Expand All @@ -158,17 +166,32 @@ def filemenu_openv(self):
filetypes = (
("avi files", "*.avi"),
("mp4 files", "*.mp4"),))


self.mode = self.MODE_VIDEO
self.caption = cv2.VideoCapture(self.filepath)

def filemenu_opens(self):
"""Open stream"""
"""Open Stream"""
self.mode = self.MODE_STREAM
self.caption = cv2.VideoCapture(0)

# Botones
def button_reproduce(self):
self.MODE_VIDEO_REPRODUCE = True

def button_pause(self):
self.MODE_VIDEO_REPRODUCE = False

def button_stop(self):
if self.mode == self.MODE_VIDEO:
self.caption = cv2.VideoCapture(self.filepath)
self.MODE_VIDEO_REPRODUCE = True




if __name__=="__main__":
root = tk.Tk()
app = App(root)
#root.mainloop()
#root.destroy()

0 comments on commit fb455aa

Please sign in to comment.