Ask Your Question
0

View webcam feed before it takes a snap?

asked 2018-05-18 13:38:22 -0600

Philia gravatar image

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
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-05-18 13:50:48 -0600

berak gravatar image

all it needs is to reorganise the logic. right now, you're only showing an image, IF there was a detection. change that !

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)
    if (cv2.waitKey(1000) == 27): # escape pressed
           break

cap.release()
cv2.destroyAllWindows() # your code *never reached *that typo ;)
edit flag offensive delete link more

Comments

I tried your code...and it keeps taking images....But I want it to stop after taking one image. It should take a snap if there was a detection....however until there is a detection it should show the web cam feed.

Philia gravatar imagePhilia ( 2018-05-19 05:07:32 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-05-18 13:38:22 -0600

Seen: 304 times

Last updated: May 18 '18