Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion examples/simple_example_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@
from eyeGestures import EyeGestures_v2

gestures = EyeGestures_v2()
cap = VideoCapture(0)

try:
cap = VideoCapture(0)
except RuntimeError as e:
print(f"Camera Initialization failed : {e}")
pygame.quit()
cv2.destroyAllWindows()
sys.exit()

x = np.arange(0, 1.1, 0.2)
y = np.arange(0, 1.1, 0.2)
Expand Down
8 changes: 7 additions & 1 deletion examples/simple_example_v2_fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@
from eyeGestures import EyeGestures_v2

gestures = EyeGestures_v2()
cap = VideoCapture(0)
try:
cap = VideoCapture(0)
except RuntimeError as e:
print(f"Camera Initialization failed : {e}")
pygame.quit()
cv2.destroyAllWindows()
sys.exit()

x = np.arange(0, 1.1, 0.5)
y = np.arange(0, 1.1, 0.5)
Expand Down
8 changes: 7 additions & 1 deletion examples/simple_example_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@
from eyeGestures import EyeGestures_v3

gestures = EyeGestures_v3()
cap = VideoCapture(0)
try:
cap = VideoCapture(0)
except RuntimeError as e:
print(f"Camera Initialization failed : {e}")
pygame.quit()
cv2.destroyAllWindows()
sys.exit()

x = np.arange(0, 1.1, 0.2)
y = np.arange(0, 1.1, 0.2)
Expand Down
8 changes: 7 additions & 1 deletion examples/troubleshooting_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@

from eyeGestures.utils import VideoCapture

cap = VideoCapture(0)
try:
cap = VideoCapture(0)
except RuntimeError as e:
print(f"Camera Initialization failed : {e}")
pygame.quit()
cv2.destroyAllWindows()
sys.exit()

x = np.arange(0, 1.1, 0.2)
y = np.arange(0, 1.1, 0.2)
Expand Down
83 changes: 63 additions & 20 deletions eyeGestures/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
import platform
import queue
import threading
import sys
import time
import traceback
from string import capwords

import cv2
import numpy as np
Expand Down Expand Up @@ -181,36 +184,71 @@ def __init__(self, name, bufforless=True):

if self.stream:
self.prev_frame = None

self.__openCam(name)

self.q = queue.Queue()
self.t = threading.Thread(target=self.__reader)
self.t = threading.Thread(target=self.__reader, daemon = True)
self.t.start()
else:
self.frames = []
with open(name, "rb") as file:
self.frames = pickle.load(file)

def __openCam(self, name):
if isinstance(name, int):
if "Windows" in platform.system():
self.cap = cv2.VideoCapture(name, cv2.CAP_DSHOW)
else:
self.cap = cv2.VideoCapture(name)
max_index = 10

if self.cap is None or not self.cap.isOpened():
print(f"Was unable to open camera: {name}.")
print(f"Trying to open camera: {name}.")
if name + 1 < 10:
self.__openCam(name + 1)
if isinstance(name, int):
for cam_index in range(name, max_index):
if "Windows" in platform.system():
cap = cv2.VideoCapture(cam_index, cv2.CAP_DSHOW)
else:
cap = cv2.VideoCapture(cam_index)

if cap is None or not cap.isOpened():
print(f"Was unable to open camera: {name}.")
print(f"Trying to open camera: {name}.")
continue

# testing an actual read
ret, frame = cap.read()
if not ret or frame is None:
cap.release()
continue
num_black_pixels = np.count_nonzero(frame == 0)
frame_size = frame.size
black_pixel_ratio = num_black_pixels / frame_size
print(frame)
print(f"black_pixels = {num_black_pixels}, frame_size = {frame_size}")
print(f"black_pixel_ratio = {black_pixel_ratio}")
if black_pixel_ratio > 0.9:
raise RuntimeError("Camera Frame not captured:")
print(f"Opened camera: {name}")
self.cap = cap
return
raise RuntimeError("No available camera found or camera busy")
else:
self.cap = cv2.VideoCapture(name)
cap = cv2.VideoCapture(name)
if not cap.isOpened():
raise RuntimeError(f"Unable to open video source:{name}")
ret, frame = cap.read()
if not ret:
cap.release()
raise RuntimeError("Camera opened but cannot read ( busy camera )")
num_black_pixels = np.count_nonzero(frame == 0)
frame_size = frame.size
black_pixel_ratio = num_black_pixels / frame_size
print(frame)
print(f"black_pixels = {num_black_pixels}, frame_size = {frame_size}")
print(f"black_pixel_ratio = {black_pixel_ratio}")
if black_pixel_ratio > 0.9:
raise RuntimeError("Camera Frame not captured:")
self.cap = cap

def __reader(self):
while self.run:
ret, frame = self.cap.read()
if not ret:
print("Camera read failed. Possibly in use or disconnected")
self.run = False
break
if not self.q.empty() and self.bufforless:
try:
Expand All @@ -225,16 +263,21 @@ def flush(self):
while not self.q.empty():
self.q.get()

def read(self):
"""Function returning latest frame"""
def read(self, timeout = 2):
if self.stream:
return self.q.get()
try:
return self.q.get(timeout = timeout)
except queue.Empty:
return (False, None)
if len(self.frames) < 1:
return (False, None)
frame = self.frames.pop(0)
self.frames.pop(0)
return ((len(self.frames) >= 1), frame)

def close(self):
"""Function closing stream"""
self.run = False
self.t.join()
self.cap.release()
if self.stream:
self.t.join()
if self.cap.isOpened():
self.cap.release()