1

Given below is the Python script I am using for accessing an IP Camera and saving the incoming video stream:

import cv2
from cv2 import cv
writer=cv2.VideoWriter("vid.avi",cv.CV_FOURCC('X', '2', '6', '4'), 24, (320,240))
def cvloop():    
    stream=urllib.urlopen('URL of the camera')
    bytes=''
    while True:
        bytes+=stream.read(1024)
        a = bytes.find('\xff\xd8')
        b = bytes.find('\xff\xd9')
        if a!=-1 and b!=-1:
            jpg = bytes[a:b+2]
            bytes= bytes[b+2:]
            i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR)            
            tki = ImageTk.PhotoImage(Image.fromarray(cv2.cvtColor(i, cv2.COLOR_BGR2RGB)))
            cv2.imshow('i',i)
            writer.write(i)
            if cv2.waitKey(1) ==27:
                exit(0)

Now, if you want to know why I am parsing the jpg images from the stream the answer can be found here. The answer explains everything.

As for the video writer script, I have referred OpenCV's documentation which can be found here.

Now instead of using the default list of codecs I am using H-264 but I have tried all the codecs installed on my system.

Question: Now the problem is on executing the program the video file is being made, on running it I see nothing. Also, on using the software GSpot to analyse the video, I am getting the number of frames in the video. Same is the case with FFMPEG. Where am I going wrong?

Camera: Axis IP Camera M1103
OS: Windows 7 32bit
Community
  • 1
  • 1
praxmon
  • 5,009
  • 22
  • 74
  • 121
  • Have you tried passing -1 as the fourcc parameter of the constructor of the VideoWriter? writer=cv2.VideoWriter("vid.avi",-1, 24, (320,240)) This should open a dialog and let you choose from the installed codecs, and modify the video compression parameters as well. – b_m Apr 17 '14 at 07:37
  • That's what I meant when I said I have tried all the codecs installed on my system. No joy there. – praxmon Apr 17 '14 at 07:38
  • if you see nothing, try to increase the `waitKey` duration e.g. `cv2.waitKey(30) ==27`. If you still see nothing there's probably a problem with your image `i` ?!? – Micka Apr 17 '14 at 12:25
  • @b_m I did run the program and the error on running with VLC was: `VLC does not support the audio or video format "undf". Unfortunately there is no way for you to fix this.` Also I did try other formats and the same thing happens, the file is made but I can't play it. How do I find if there is something else that is wrong? Like the image `i`? – praxmon Apr 17 '14 at 12:28
  • @PrakharMohanSrivastava during program execution, you should see a window called i with the images (after you increase the waitKey duration). If you don't see the image (e.g. the window "i" doesnt have the correct size), then the image might be empty. you could alternatively print i.size in your terminal output and check if the size if ok. At the end I would try to isolate "image generation" and "image writing" to find out where the problem occurs (input/processing/output). – Micka Apr 17 '14 at 12:38
  • @b_m I can see the video alright the only problem is I cannot save it. I'll print the size and then try to isolate them to see what is going on. – praxmon Apr 17 '14 at 12:41
  • @b_m I tried to save the images being received. I am able to do so. Now the problem can be the video writer. How do I know it is working properly? – praxmon Apr 17 '14 at 13:15

0 Answers0