Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

View webcam feed before it takes a snap?

So my code should detect an object with opencv and once it detects it it should take a snap of it. Which it does fine....However it simply goes to the webcam and doesn't show me the webcam feed. When it detects the object it takes a snap and show the image.

What I want is to see the webcam feed until it detects the object....How can I do that?

Here's my code:

import cv2
cascade = cv2.CascadeClassifier('xcascade.xml')
cap = cv2.VideoCapture(1)

num = 0
while num<1000:
    ret, img = cap.read()
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    cas = cascade.detectMultiScale(gray, 10, 10)
    for(x,y,w,h) in cas:
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.rectangle(img,(x,y), (x+w,y+h),(255,255,0),5)
        cv2.putText(img, 'Something',(x,y-120), font, 1.5, (0,255,255),5, cv2.LINE_AA)
        num = num+1
        cv2.imshow('img',img)
        cv2.waitKey(1000)
        cap.release()
        cv2.desrtoyAllWindows()
        break