Ask Your Question
2

Video over the Network

asked 2013-08-18 07:50:52 -0500

subhendusc gravatar image

I have a laptop running Windows 7 with Python 2.7. I have a webcam attached to the laptop. The laptop has no other webcam. The video is transmitted from this webcam using Yawcam software. The software can transmit videos in streaming , http and ftp protocols. I have arduino micro controller attached to it. I also have a python / twisted socket server running on it to communicate with my desktop. In my desktop I also have windows , python and openCV installed. I want to grab this video in real time and do image processing on it. I used this code to grab the video but with no success

import cv2.cv as cv

cv.NamedWindow("camera", 1)
capture = cv.CaptureFromCAM("ftp://@192.168.1.6:86/");
##capture = cv.CaptureFromCAM(0);
while True:
    img = cv.QueryFrame(capture)
    cv.ShowImage("camera", img)
    if cv.WaitKey(10) == 27:
        break
cv.DestroyWindow("camera")

Ideally i wanted to have openCV in my laptop and transmit videos in realtime using websockets . But I have no clue of doing it. Please help. The best solution would be having openCV at both the ends without any commercial software.

edit retag flag offensive close merge delete

Comments

  • please use the cv2 api, not the old one.
  • ftp sounds, like it's transmitting only single images, try the stream option instead
berak gravatar imageberak ( 2013-08-19 04:18:44 -0500 )edit

@berak - I have used cv2 also. and used http instead of ftp . But nothing is hapening. The error it is giving is Type Error : an integer is required. Can you provide me with a simple program or solution.

subhendusc gravatar imagesubhendusc ( 2013-08-19 08:21:39 -0500 )edit

3 answers

Sort by » oldest newest most voted
1

answered 2013-08-19 08:59:34 -0500

updated 2013-08-19 09:39:56 -0500

Very clear that it will give error...

CvCapture* cvCaptureFromCAM(int index)

Initializes capturing a video from a camera.
Parameter:    index – Index of the camera to be used. If there is only

one camera or it does not matter what camera is used -1 may be passed.

try that LINK

p.p. code, found somewhere... for example:

//MJPEG network stream to OpenCV 2

#include "opencv/cv.h"
#include "opencv/highgui.h"
#include <iostream>

int main(int, char**) {
    cv::VideoCapture vcap;
    cv::Mat image;

    const std::string videoStreamAddress = "ip.ip.ip.ip";

    if(!vcap.open(videoStreamAddress)) {
        std::cout << "Error opening video stream or file" << std::endl;
        return -1;
    }

    for(;;) {
        if(!vcap.read(image)) {
            std::cout << "No frame" << std::endl;
            cv::waitKey();
        }
        cv::imshow("Output Window", image);
        if(cv::waitKey(1) >= 0) break;
    }   
}
edit flag offensive delete link more
0

answered 2018-08-06 23:37:34 -0500

CT83 gravatar image

After months of searching the internet, this is what I came up with, I have neatly packaged it into classes, with unit tests and documentation as SmoothStream check it out, it was the only simple and working version of streaming I could find anywhere.

I used this code and wrapped mine around it.

Viewer.py

import cv2
import zmq
import base64
import numpy as np

context = zmq.Context()
footage_socket = context.socket(zmq.SUB)
footage_socket.bind('tcp://*:5555')
footage_socket.setsockopt_string(zmq.SUBSCRIBE, np.unicode(''))

while True:
    try:
        frame = footage_socket.recv_string()
        img = base64.b64decode(frame)
        npimg = np.fromstring(img, dtype=np.uint8)
        source = cv2.imdecode(npimg, 1)
        cv2.imshow("Stream", source)
        cv2.waitKey(1)

    except KeyboardInterrupt:
        cv2.destroyAllWindows()
        break

Streamer.py

import base64
import cv2
import zmq

context = zmq.Context()
footage_socket = context.socket(zmq.PUB)
footage_socket.connect('tcp://localhost:5555')

camera = cv2.VideoCapture(0)  # init the camera

while True:
    try:
        grabbed, frame = camera.read()  # grab the current frame
        frame = cv2.resize(frame, (640, 480))  # resize the frame
        encoded, buffer = cv2.imencode('.jpg', frame)
        jpg_as_text = base64.b64encode(buffer)
        footage_socket.send(jpg_as_text)

    except KeyboardInterrupt:
        camera.release()
        cv2.destroyAllWindows()
        break
edit flag offensive delete link more
0

answered 2013-08-19 09:48:46 -0500

berak gravatar image

updated 2013-08-19 09:51:25 -0500

like Spas Hristov said, cv.CaptureFromCAM(int id) is used for (local) cameras, not for streams/urls.

try:

   import cv2
   import numpy

   cap = cv2.VideoCapture("http://@192.168.1.6:86/")
   while cap.isOpened():
       _,frame = cap.read()
       cv2.imshow("stream",frame)
       c = cv2.waitKey(1)
       if c == 27:
           break
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-08-18 07:50:52 -0500

Seen: 11,017 times

Last updated: Aug 19 '13