Apr-16-2018, 06:21 AM
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:
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.