I am a rookie at coding and even with that I am probably being generous. I was wondering if there is a way to resize an image, fed by a USB webcam, and resize a window simultaneously? With the Raspberry Pi3, I am trying to run four USB cameras and display the feed on a monitor. The monitor is a 21.5" so I want to have an even vertical and horizontal split in the screen so that I can view all four cameras at once. I created/cut and pasted a script written in Python and using opencv and numpy. Any ideas?
import cv2
import numpy as np
vc = cv2.VideoCapture(0)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
while rval:
cv2.resizeWindow("Left Mold - Cavity 1 & 2", 20, 20)
cv2.moveWindow("Left Mold - Cavity 1 & 2", 1, 1)
cv2.imshow("Left Mold - Cavity 1 & 2", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
cv2.destroyWindow("Left Mold - Cavity 1 & 2")
So I revised the code but I did something wrong along the way because I get some kickbacks. Here is the revised code...
import cv2
import numpy as np
cap1 = cv2.VideoCapture(0)
cap2 = cv2.VideoCapture(1)
cap3 = cv2.VideoCapture(2)
cap4 = cv2.VideoCapture(3)
while(True):
#Read Camera 1
ret, frame = cap1.read()
#Read Camera 2
ret, frame = cap2.read()
#Read Camera 3
ret, frame = cap3.read()
#Read Camera 4
ret, frame = cap4.read()
#Reduce the image size of all camera feeds to half
cap1 = cv2.resize(cap1, (0, 0), None, .5, .5)
cap2 = cv2.resize(cap2, (0, 0), None, .5, .5)
cap3 = cv2.resize(cap3, (0, 0), None, .5, .5)
cap4 = cv2.resize(cap4, (0, 0), None, .5, .5)
#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)
#Display the video feed
cv2.imshow('Left Mold - Cavity 1 & 2', cap1)
cv2.imshow('Left Mold - Cavity 3 & 4', cap2)
cv2.imshow('Right Mold - Cavity 1 & 2', cap3)
cv2.imshow('Right Mold - Cavity 3 & 4', cap4)
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
#When everything is done, release the capture
cap1.release()
cap2.release()
cap3.release()
cap4.release()
cv2.destroyAllWindows()
And here are the errors...
VIDIOC_DQBUF: No such device
libv4l2: error turning on stream: Input/output error
VIDIOC_STREAMON: Input/output error
Traceback (most recent call last):
File "MultipleVid.py", line 20, in <module>
cap1 = cv2.resize(cap1, (0, 0), None, .5, .5)
TypeError: src is not a numpy array, neither a scalar
Unable to stop the stream.: No such device
Unable to stop the stream.: No such device
Unable to stop the stream.: No such device
I'm not sure how to read these errors for what they mean so I can trouble shoot. What do the errors mean?
My third attempt...
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) cap1)
while(cap2.isOpened()):
ret, frame = cap2.read()
if ret==True:
cv2.imshow('Left Mold - Cavity 2 & 3',
cap2) cap2)
while(cap3.isOpened()):
ret, frame = cap3.read()
if ret==True:
cv2.imshow('Right Mold - Cavity 1 & 2',
cap3) cap3)
while(cap4.isOpened()):
ret, frame = cap4.read()
if ret==True:
cv2.imshow('Right Mold - Cavity 3 & 4',
cap4) 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
240);
#Stack camera 1 & 3 vertically and camera 2 & 4 horizontally to 1 &
3 3
numpy_vertical = np.vstack((cap1, cap3))
numpy_horizontal = np.hstack((cap2, cap4))
array([[cap1,cap2],
[cap3,cap4]]) Put
[cap3,cap4]])
#Put the camera feed on the specified
axis axis
numpy_vertical_concat = np.concatenate((cap1, cap3), axis=0)
numpy_horizontal_concat = np.concatenate((cap2, cap4),
axis=1) axis=1)
key = cv2.waitKey(20)
if key == 27: # exit on ESC
sys.exit()sys.exit()
When everything is done, release the capture
cap1.release()
cap2.release()
cap3.release()
cap4.release()
cv2.destroyAllWindows()cv2.destroyAllWindows()