fix: index out of range error on camera initialization on MacOS

This commit is contained in:
esimov
2022-11-10 11:10:50 +02:00
parent 0388e93ed9
commit 0c9a71c262
7 changed files with 43 additions and 30 deletions

View File

@@ -47,9 +47,10 @@ def process_frame(pixs):
return dets
# initialize the camera
width, height = 640, 480
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
show_pupil = True
show_eyes = False
@@ -61,8 +62,9 @@ while(True):
pixs = np.ascontiguousarray(frame[:, :, 1].reshape((frame.shape[0], frame.shape[1])))
pixs = pixs.flatten()
# Verify if camera is intialized by checking if pixel array is not empty.
if np.any(pixs):
# We need to make sure that the whole frame size is transfered over Go,
# otherwise we might getting an index out of range panic error.
if len(pixs) == width*height:
dets = process_frame(pixs) # pixs needs to be numpy.uint8 array
if dets is not None:
@@ -123,7 +125,7 @@ while(True):
(0, 255, 0), 2
)
cv2.imshow('', frame)
cv2.imshow('Blink detector', frame)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):

View File

@@ -41,17 +41,19 @@ def process_frame(pixs):
return dets
# initialize the camera
width, height = 640, 480
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
while(True):
ret, frame = cap.read()
pixs = np.ascontiguousarray(frame[:, :, 1].reshape((frame.shape[0], frame.shape[1])))
pixs = pixs.flatten()
# Verify if camera is intialized by checking if pixel array is not empty.
if np.any(pixs):
# We need to make sure that we are transfering the whole frame size to Go,
# otherwise we are getting an index out of range error.
if len(pixs) == width*height:
dets = process_frame(pixs) # pixs needs to be numpy.uint8 array
if dets is not None:
for det in dets:

View File

@@ -41,9 +41,9 @@ def process_frame(pixs):
return dets
width, height = 640, 480
# initialize the camera
width, height = 640, 480
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
@@ -52,8 +52,9 @@ while(True):
ret, frame = cap.read()
pixs = np.ascontiguousarray(frame[:, :, 1]).flatten()
# Verify if camera is intialized by checking if pixel array is not empty.
if np.any(pixs):
# We need to make sure that the whole frame size is transfered over Go,
# otherwise we might getting an index out of range panic error.
if len(pixs) == width*height:
dets = process_frame(pixs) # pixs needs to be np.uint8 array
if dets is not None:
for det in dets:

View File

@@ -46,9 +46,10 @@ def process_frame(pixs):
return dets
# initialize the camera
width, height = 640, 480
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
showPupil = True
showEyes = False
@@ -59,8 +60,9 @@ while(True):
pixs = np.ascontiguousarray(frame[:, :, 1].reshape((frame.shape[0], frame.shape[1])))
pixs = pixs.flatten()
# Verify if camera is intialized by checking if pixel array is not empty.
if np.any(pixs):
# We need to make sure that the whole frame size is transfered over Go,
# otherwise we might getting an index out of range panic error.
if len(pixs) == width*height:
dets = process_frame(pixs) # pixs needs to be numpy.uint8 array
if dets is not None:
@@ -86,7 +88,7 @@ while(True):
if showLandmarkPoints:
cv2.circle(frame, (int(det[1]), int(det[0])), 4, (0, 255, 0), -1, 8, 0)
cv2.imshow('', frame)
cv2.imshow('Facial landmark detector', frame)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):

View File

@@ -46,9 +46,10 @@ def process_frame(pixs):
return dets
# initialize the camera
width, height = 640, 480
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
showPupil = True
showEyes = False
@@ -58,8 +59,9 @@ while(True):
pixs = np.ascontiguousarray(frame[:, :, 1].reshape((frame.shape[0], frame.shape[1])))
pixs = pixs.flatten()
# Verify if camera is intialized by checking if pixel array is not empty.
if np.any(pixs):
# We need to make sure that the whole frame size is transfered over Go,
# otherwise we might getting an index out of range panic error.
if len(pixs) == width*height:
dets = process_frame(pixs) # pixs needs to be numpy.uint8 array
if dets is not None:

View File

@@ -59,17 +59,19 @@ def process_frame(pixs):
return dets
# initialize the camera
width, height = 640, 480
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
while(True):
ret, frame = cap.read()
pixs = np.ascontiguousarray(frame[:, :, 1].reshape((frame.shape[0], frame.shape[1])))
pixs = pixs.flatten()
# Verify if camera is intialized by checking if pixel array is not empty.
if np.any(pixs):
# We need to make sure that the whole frame size is transfered over Go,
# otherwise we might getting an index out of range panic error.
if len(pixs) == width*height:
dets = process_frame(pixs) # pixs needs to be numpy.uint8 array
if dets is not None:

View File

@@ -74,9 +74,10 @@ def process_frame(pixs):
return dets
# initialize the camera
width, height = 640, 480
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
showFaceDet = False
showPupil = True
@@ -91,8 +92,9 @@ while(True):
frame[:, :, 1].reshape((frame.shape[0], frame.shape[1])))
pixs = pixs.flatten()
# Verify if camera is intialized by checking if pixel array is not empty.
if np.any(pixs):
# We need to make sure that the whole frame size is transfered over Go,
# otherwise we might getting an index out of range panic error.
if len(pixs) == width*height:
dets = process_frame(pixs) # pixs needs to be numpy.uint8 array
if dets is not None:
@@ -130,7 +132,7 @@ while(True):
cv2.putText(frame, "Person talking...", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 1)
cv2.imshow('Talk detection demo', frame)
cv2.imshow('Talk detector', frame)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):