Commit 41a2c279 authored by Kieran Hymas's avatar Kieran Hymas

Fixed several bugs to do with video capture. Now, capture window can be safely...

Fixed several bugs to do with video capture. Now, capture window can be safely destroy without crashing
parent 1991b4ed
#Imports
#from videocapture import *
from videostream import *
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
......@@ -15,9 +15,10 @@ class Application:
self.window.geometry(str(self.width) + "x0+0+0")
#Videostream options
self.videostreamplaying = False
self.videostream = 0 #Initialise an empty video VideoStream
self.availableStreams = self.getPossibleVideoStreams()
self.delay = 15
self.delay = 10
#Image options
self.options = {'BnW' : False}
......@@ -106,11 +107,6 @@ class Application:
#Menubar options
#File
def onNew(self):
self.vid = VideoStream()
self.canvas = Canvas(self.window, width=self.vid.width, height=self.vid.height)
self.canvas.pack()
def onExit(self):
self.window.quit()
......@@ -143,6 +139,7 @@ class Application:
try:
ret, cap = cv2.VideoCapture(ind).read()
if ret:
print("[+] Found camera source: ", ind)
arr.append(ind)
ind += 1
else:
......@@ -153,42 +150,26 @@ class Application:
def launchVideo(self, video_source):
self.vid = VideoStream(video_source)
self.videostreamplaying = True
self.newwin = Toplevel(self.window)
self.newwin.title("Pycroscope: Video source 0")
self.canvas = Canvas(self.newwin, width=self.vid.width, height=self.vid.height)
self.canvas.pack()
self.updateVideo()
self.newwin.protocol("WM_DELETE_WINDOW", self.cleanUpVideo)
def updateVideo(self):
if self.vid.vidcap.isOpened():
ret, frame = self.vid.get_frame(self.options)
if ret:
self.photo = ImageTk.PhotoImage(image=Image.fromarray(frame))
self.canvas.create_image(0, 0, image=self.photo, anchor=NW)
self.window.after(self.delay, self.updateVideo)
class VideoStream:
def __init__(self, video_source=0):
self.vidcap = cv2.VideoCapture(video_source)
if not self.vidcap.isOpened():
raise ValueError("Unable to open video source:", video_source)
self.width = self.vidcap.get(cv2.CAP_PROP_FRAME_WIDTH)
self.height = self.vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT)
def __del__(self):
if self.vidcap.isOpened():
self.vidcap.release()
def get_frame(self, options):
if self.vidcap.isOpened():
ret, frame = self.vidcap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
if options['BnW']:
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
if ret:
return (ret, frame)
else:
return (ret, None)
else:
return (ret, None)
if self.videostreamplaying:
if self.vid.vidcap.isOpened():
ret, frame = self.vid.get_frame(self.options)
if ret:
self.photo = ImageTk.PhotoImage(image=Image.fromarray(frame))
self.canvas.create_image(0, 0, image=self.photo, anchor=NW)
self.window.after(self.delay, self.updateVideo)
def cleanUpVideo(self):
self.vid.__del__()
self.newwin.destroy()
self.videostreamplaying = False
self.vid = 0
import cv2
class VideoStream:
def __init__(self, video_source=0):
self.vidcap = cv2.VideoCapture(video_source)
if not self.vidcap.isOpened():
raise ValueError("Unable to open video source:", video_source)
self.width = self.vidcap.get(cv2.CAP_PROP_FRAME_WIDTH)
self.height = self.vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT)
def __del__(self):
if self.vidcap.isOpened():
self.vidcap.release()
def get_frame(self, options):
if self.vidcap.isOpened():
ret, frame = self.vidcap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
if options['BnW']:
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
if ret:
return (ret, frame)
else:
return (ret, None)
else:
return (ret, None)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment