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

@@ -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: