Ask Your Question
0

i want to use my usb camera in raspberry pi and i have written this code , its not getting any error but still my video output is not coming plz help me.

asked 2018-02-16 05:48:14 -0600

Ahmed.20 gravatar image
import cv2,io,imutils
import numpy
from imutils.video import VideoStream

 print("Starting Camera...........")
webcam = VideoStream(0).start()
webcam.resolution = (640, 320)
detector= cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(1)


def getFrame():
jpegBuffer = io.BytesIO()
webcam.capture(jpegBuffer, format='jpeg')
buff = numpy.fromstring(jpegBuffer.getvalue(), dtype=numpy.uint8)
return cv2.imdecode(buff, 1)

 while(True):
ret,img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
   img= cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

cv2.imshow('Screen',img)
if cv2.waitKey(1) & 0xFF == ord('q'):
    break

cap.release()
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

1

try to use either VideoCapture(0) or VideoStream(0) , but not both (assuming, you only have a single webcam)

berak gravatar imageberak ( 2018-02-16 06:16:21 -0600 )edit

Above source can be used both either webcam or picamera for merely raspberry pi . To use webcam change this cap = cv2.VideoCapture(1) to cap = cv2.VideoCapture(0)

supra56 gravatar imagesupra56 ( 2018-02-16 06:27:13 -0600 )edit

Didn't u wrote the code by urself?

supra56 gravatar imagesupra56 ( 2018-02-16 07:26:10 -0600 )edit
1

It worked for me. I can used either webcam or picamera.

supra56 gravatar imagesupra56 ( 2018-02-16 07:30:42 -0600 )edit
1

@berak, said. U can disable picamera or unplug picamera. But I don't used imutils library. The VideoStream is for webcam. So u set cap = cv2.VideoCapture(0). The pi VideoStream is for picamera.

supra56 gravatar imagesupra56 ( 2018-02-16 09:35:02 -0600 )edit

U never put this on window.

img= cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
supra56 gravatar imagesupra56 ( 2018-02-16 21:21:34 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-02-16 21:23:15 -0600

supra56 gravatar image

updated 2018-02-16 21:26:40 -0600

Here is code:

import cv2,io,imutils
import numpy
from imutils.video import VideoStream

print("Starting Camera...........")
detector= cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
cap.start()

while(True):
    ret,img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = detector.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

    cv2.imshow('Screen',img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-02-16 05:48:14 -0600

Seen: 8,364 times

Last updated: Feb 16 '18