Hello i tried to run 2 HD Cameras (Logitech C310) together using this code:
import cv2
def main():
# Open the first webcam (usually index 0)
cap1 = cv2.VideoCapture(0,cv2.CAP_DSHOW) # Use dshow backend
if not cap1.isOpened():
print("Error: Could not open webcam 1")
return
# Open the second webcam (usually index 1)
cap2 = cv2.VideoCapture(1,cv2.CAP_DSHOW) # Use dshow backend
if not cap2.isOpened():
print("Error: Could not open webcam 2")
cap1.release() # Release the first webcam
return
print("masuk")
# Set MJPG as FourCC for both webcams
cap1.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG'))
cap2.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG'))
# cap1.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
# cap1.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
# cap2.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
# cap2.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
while True:
# Read frames from the webcams
ret1, frame1 = cap1.read()
ret2, frame2 = cap2.read()
if not ret1 or not ret2:
print("Error: Could not read frames from webcams")
break
# Display the frames
cv2.imshow("Webcam 1", frame1)
cv2.imshow("Webcam 2", frame2)
# Exit loop when 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the webcams and close OpenCV windows
cap1.release()
cap2.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
After i run this code, i always get the same error:
Error: Could not open webcam 2
I already tried to change the USB port and use USB 3.0, but there was always an error in the webcam 2. This program works when I change the VideoCapture(0) device with another camera that is not HD and use the MSMF backend for VideoCapture (0). But when this camera uses the DSHOW event, if it uses a non-HD camera, it will error. But when I use this scheme with two HD cameras, this program will give me errors in the above.