Ask Your Question
0

Error: "mat is not a numpy array, neither a scalar"?

asked 2017-12-06 14:40:31 -0600

MoyliePi gravatar image

I am trying to run 4 USB cameras and feed them to a monitor. I would like to have the frames of each camera split vertically and horizontally on the monitor so one can see all four feeds at once. I am getting the error noted in the "question". What does this mean? PS - I am a newbie at coding and my knowledge is limited.

import cv2 
import numpy as np

cap1 = cv2.VideoCapture(0)
cap2 = cv2.VideoCapture(1)
cap3 = cv2.VideoCapture(2)
cap4 = cv2.VideoCapture(3)

while(cap1.isOpened()):
ret, frame = cap1.read()
if ret==True:
cv2.imshow('Left Mold - Cavity 1 & 2', cap1)

while(cap2.isOpened()):
ret, frame = cap2.read()
if ret==True:
cv2.imshow('Left Mold - Cavity 2 & 3', cap2)

while(cap3.isOpened()):
ret, frame = cap3.read()
if ret==True:
cv2.imshow('Right Mold - Cavity 1 & 2', cap3)

while(cap4.isOpened()):
ret, frame = cap4.read()
if ret==True:
cv2.imshow('Right Mold - Cavity 3 & 4', cap4)

cap1.set(cv2.CAP_PROP_FRAME_WIDTH, 320);
cap1.set(cv2.CAP_PROP_FRAME_HEIGHT, 240);
cap2.set(cv2.CAP_PROP_FRAME_WIDTH, 320); 
cap2.set(cv2.CAP_PROP_FRAME_HEIGHT, 240);
cap3.set(cv2.CAP_PROP_FRAME_WIDTH, 320);
cap3.set(cv2.CAP_PROP_FRAME_HEIGHT, 240);
cap4.set(cv2.CAP_PROP_FRAME_WIDTH, 320);
cap4.set(cv2.CAP_PROP_FRAME_HEIGHT, 240);

#Stack camera 1 & 3 vertically and camera 2 & 4 horizontally to 1 & 3
numpy_vertical = np.vstack((cap1, cap3))
numpy_horizontal = np.hstack((cap2, cap4))
array([[cap1,cap2],
[cap3,cap4]])

#Put the camera feed on the specified axis
numpy_vertical_concat = np.concatenate((cap1, cap3), axis=0)
numpy_horizontal_concat = np.concatenate((cap2, cap4), axis=1)

key = cv2.waitKey(20)
if key == 27: # exit on ESC
sys.exit()

#When everything is done, release the capture

cap1.release()
cap2.release()
cap3.release()
cap4.release()
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

which part of the code is throwing that error? Plus there are so many things wrong with this code which I will address once you have answered my question

eshirima gravatar imageeshirima ( 2017-12-06 15:15:20 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-12-06 15:25:22 -0600

I've found the issue already.

  1. cv2.VideoCapture() just give you access to the camera. The actual image is retrieved when you do .read(). In your code, you are stacking cameras that is why your compiler is yelling at you.

  2. You have so many infinite loops. Just one will suffice.

Your general layout should be like this

import cv2 
import numpy as np

cap1 = cv2.VideoCapture(0)
cap2 = cv2.VideoCapture(1)
cap3 = cv2.VideoCapture(2)
cap4 = cv2.VideoCapture(3)

cap1.set(cv2.CAP_PROP_FRAME_WIDTH, 320);
cap1.set(cv2.CAP_PROP_FRAME_HEIGHT, 240);
cap2.set(cv2.CAP_PROP_FRAME_WIDTH, 320); 
cap2.set(cv2.CAP_PROP_FRAME_HEIGHT, 240);
cap3.set(cv2.CAP_PROP_FRAME_WIDTH, 320);
cap3.set(cv2.CAP_PROP_FRAME_HEIGHT, 240);
cap4.set(cv2.CAP_PROP_FRAME_WIDTH, 320);
cap4.set(cv2.CAP_PROP_FRAME_HEIGHT, 240);

while cap1.isOpened() and cap2.isOpened() and cap3.isOpened() and cap4.isOpened():
    ret1, frame1 = cap1.read()
    ret2, frame2 = ......


    #finish the rest
    if ret1 == True and ret2 == True:
        # do your stuff and stack frame1, frame2 etc instead
edit flag offensive delete link more

Comments

Thanks for the help again! I'll take another swing at this.

MoyliePi gravatar imageMoyliePi ( 2017-12-07 07:11:51 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-12-06 14:40:31 -0600

Seen: 18,048 times

Last updated: Dec 06 '17