6.Running USB Camera With OpenCV - NaimFuad/RaspberryPiWithOpenCV GitHub Wiki
Connecting USB Camera to Raspberry Pi
1.Connect your USB Camera to the USB port of the Raspberry Pi. 2.Create a new Python script by copy paste the given code. On the 4th line edit it to the USB port number based on the image below. i.e., USB is connected to USB port ttyUSB1, the code is as follows:
import cv2
import numpy as np
import time
cap=cv2.VideoCapture(1)
cap.set(3,320) #set heigt >>refer propId
cap.set(4,240) #set width
time.sleep(0.1)
while True:
ret,frame = cap.read()
cv2.imshow('frame',frame)
if cv2.waitKey(1)&0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()