Python Forum
Take a snapshot with the webcam with python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Take a snapshot with the webcam with python?
#1
So I am trying a write a program to detect objects using openCV libraries using Python.

The code to detect object is somewhat satisfactory. When I run the code, it opens the webcam and when the object the system has been trained to detect is visible with the cam it detects it and draws a rectangle and writes what the object is. So far so good. However I want the program to take a snap for the first time it detects something. And close the webcam and shows the pic instead.

For example if the system is trained to detect apples, when it sees an apple from the webcam, it should detect it and take a snap of the apple with the detection features like rectangle and the text 'apple' printed on the image. Then it should close the webcam and open the snap it took instead.

Here's the code I have written so far to detect the object:

import cv2
import numpy as np


object_cascade = cv2.CascadeClassifier('xcascade.xml')


cap = cv2.VideoCapture(0)

while True:
    ret, img = cap.read()
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
                
    objects = object_cascade.detectMultiScale(gray, 10, 10)
    for(x,y,w,h) in objects:
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.rectangle(img,(x,y), (x+w,y+h),(255,255,0),5)
        cv2.putText(img, 'Apple',(x,y-40), font, 1.5, (255,255,0),5, cv2.LINE_AA)
        

    cv2.imshow('img',img)
    k = cv2.waitKey(30)& 0xff
    if k == 27:
        break

cap.release()
cv2.desrtoyAllWindows()

    
Thanks in advance.
Reply
#2
Guys help me out a bit....

I did this much so far:

 
    import cv2
    import numpy as np


    cascade1 = cv2.CascadeClassifier('xcascade.xml')
    cascade2 = cv2.CascadeClassifier('ycascade.xml')

    def TakeSnapAndSave():
        cap = cv2.VideoCapture(0)

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

            object1 = xcascade.detectMultiScale(gray, 10, 10)
            for(x,y,w,h) in object1:
                font = cv2.FONT_HERSHEY_SIMPLEX
                cv2.rectangle(img,(x,y), (x+w,y+h),(255,255,0),5)
                cv2.putText(img, 'Something',(x,y-40), font, 1.5, (255,255,0),5, cv2.LINE_AA)
                object2 = ycascade.detectMultiScale(gray, 15, 15)
                for(x,y,w,h) in object2:
                    font = cv2.FONT_HERSHEY_SIMPLEX
                    cv2.rectangle(img,(x,y), (x+w,y+h),(0,255,255),5)
                    cv2.putText(img, 'Something Else',(x+100,y+80), font, 0.8, (0,0,255),2, cv2.LINE_AA)        


            cv2.imwrite('opencv'+str(num)+'.jpg',img)
            num = num+1
        cap.release()
        cv2.destroyAllWindows()
The code keeps taking snapshots without doing any detection.....

Help me out please.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020