1

I am working on one of my college project i.e object(car) detection in opencv python ,i am using opencv 3 and python 3.4. I have a code for it but when i run the code the output is not displayed. It shows that the code is error free but still unable to get the output. I am new to image processing ,so it will be a great help if someone tries to sort out my problem. The code is given below`

import cv2
import numpy as np
import argparse
ap = argparse.ArgumentParser()
ap.add_agrument("-v","--video",
help = "path to the (optional) video file")
args = vars(ap.parse_agrs())
camera = cv2.VideoCapture(agrs["video"])
car_cascade = cv2.CascadeClassifier("cars.xml")
while true:
ret,frames = camera.read(),cv2.rectangle()
gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
cars = car_cascade.detectionMultiScale(gray, 1.1,1)
for (x,y,w,h) in cars:
cv2.rectangular()frames,(x,y),(x+w,y+h), (0,0,255),2)
cv2.imshow ('video',frames)
cv2.waitkey(0)
4
  • 1
    You have serious indentation problems. Check it first Commented Mar 22, 2017 at 8:35
  • You say your code is error free, then how does this : cv2.rectangular()frames,(x,y),(x=w,y+h), (0,o,255),2) not throw an error. I seriously doubt you ran the code. Commented Mar 22, 2017 at 8:37
  • Giving the benefit of doubt the indentation problem is just because of poor formatting when pasting in SO, you have at least two syntax issues. Line 11, lack of coma b/n read() and c2; line 15 - the arguments are not in the method rectangular() (remove the closing bracket )) Commented Mar 25, 2017 at 5:58
  • Logical error - in argparse you mark --video as optional, but what will happen in line 8 if it is not provided. Commented Mar 25, 2017 at 6:00

4 Answers 4

2

I just remove the argparse command and edited the code little bit and it is working quit well.To see the output click here : https://www.youtube.com/watch?v=phG9inHoAKg

And the code files are uploaded to my github account https://github.com/Juzer2012/Car-detection

Sign up to request clarification or add additional context in comments.

Comments

1

You write: "It shows that the code is error free" ...

It isn't (and this multiple times) as for example here:

  ap.add_agrument(...

where it should be

  ap.add_argument(...

Just check again for more of such syntax errors. Happy coding :) .

1 Comment

hey claudio thnx for the comment it would be great if you provide me an example of how to load videos using argparse for image processing. As a tried by adding path to argument as commented by you but still didn't get the output.please help.
1

Here the by you requested code example which uses argparse for image processing - it works both with python2.x and python3.x showing a video stream for processing in a for this purpose opened window. If you can see the video stream output, just mark this as a valid answer to your question. Thanks in advance (y). Happy coding :) .

import cv2
def showVideoStream_fromWebCam(argsVideo, webCamID=0, showVideoStream=True):
    cv2_VideoCaptureObj_webCam = cv2.VideoCapture(webCamID)
    while True:
        retVal, imshowImgObj = cv2_VideoCaptureObj_webCam.read()
        if showVideoStream: 
            imshowImgObj = cv2.flip(imshowImgObj, 1)
            cv2.imshow('webCamVideoStream', imshowImgObj)
        #:if
        if cv2.waitKey(1) == 27: 
            break  # [Esc] to quit
        #:if
    #:while        
    cv2.destroyAllWindows()
#:def
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-v","--video", help = "webCamID (= 0)")
args = vars(ap.parse_args())
showVideoStream_fromWebCam(args["video"])

4 Comments

hey claudio thanks for the reply i tried your code and is running good but the problem is that it is not able to read by video file and just as your code say if video file condition is not satisfy then open webcam, so every time i edit and try your code it open the webcam for the input. I am using opencv python on windows 10 , so is their any problem with that?
Try following change to the last line of the provided code: showVideoStream_fromWebCam(args["video"], "c:/fullPath/NameOf/yourVideoFile.avi") to display a video file instead of the webCam video stream (video will run very FAST, unless you change also cv2.waitKey(1) to for example cv2.waitKey(100) ).
it is showing an error like this showVideoStream_fromWebCam(args['video'],"C:/Users/Juzer/Desktop/python_10Day/video.avi") KeyError: 'video'
and if tried to remove or edit anything regarding that it again open the webcam as an input
0

Let's make the code even a bit more perfect by running the video at approximately it's original speed (25 frames/second), taking out what is not necessary and drawing all the rectangles first, then showing the frame:

import cv2

camera = cv2.VideoCapture("video.avi")
car_cascade = cv2.CascadeClassifier('cars.xml')

# Get frames per second from video file. Syntax depends on OpenCV version: 
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
if int(major_ver)  < 3 :
    fps = camera.get(cv2.cv.CV_CAP_PROP_FPS)
else :
    fps = camera.get(cv2.CAP_PROP_FPS)
#:if
intTimeToNextFrame=int(1000.0/fps)-12 # '-12' estimation of time for processing
while True:
    (grabbed,frame) = camera.read()
    grayvideo = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cars = car_cascade.detectMultiScale(grayvideo, 1.1, 1)
    for (x,y,w,h) in cars:
        cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,255),1)
    cv2.imshow("video",frame)
    if cv2.waitKey(intTimeToNextFrame)== ord('q'):
        break
camera.release()
cv2.destroyAllWindows()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.