2

I want to read from the file /dev/video0 either through c or python,and store the incoming bytes in a different file. Here is my c code:

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
    int fd,wfd;
    fd=open("/dev/video0",O_RDONLY);
    wfd=open("image",O_RDWR|O_CREAT|O_APPEND,S_IRWXU);
    if(fd==-1)
        perror("open");
    while(1)
    {
        char buffer[50];
        int rd;
        rd=read(fd,buffer,50);
        write(wfd,buffer,rd);
    }

    return 0;
}

When i run this code and after some time terminate the program nothing happens except a file name "image" is generated which is usual.

This is my python code:

    image=open("/dev/video0","rb")
    image.read()

and this is my error when my run this snippet:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 22] Invalid argument

I want to know how to do this using pure c or python code.Please no external library suggestions.

user2738777
  • 418
  • 1
  • 5
  • 21
  • Is it possible you need to open the camera in read/write mode and send a command to enable it? – Andrew Corsini Jun 25 '15 at 19:25
  • @AndrewCorsini well i think i should be possible.And why not? – user2738777 Jun 26 '15 at 07:02
  • "This is my python code: `image=open("/dev/video0","rb") image.readline()`": `readline()` is used for text lines terminated in '\n'. Since you read binary data, use read(). See http://stackoverflow.com/questions/8710456/reading-a-binary-file-with-python – boardrider Jun 26 '15 at 08:51
  • @boardrider with read also it is giving the same error. – user2738777 Jun 26 '15 at 15:27
  • If you save the video stream to a file on disk (AVI or MP4), and then just change the `open()` to use that file, does all work as expected? – boardrider Jun 27 '15 at 18:13
  • @boardrider No, it does not work. – user2738777 Jun 27 '15 at 18:17
  • @boardrider I want to know what type of argument does read require. – user2738777 Jun 27 '15 at 18:18
  • There's a very good chance that my example, given in an answer, will be _down-voted_ as *not answering the question*, but since I cannot add _formatted Python code_ inside of a comment - I'll suffer the points loss for the greater good :-) – boardrider Jun 27 '15 at 19:09
  • you need to use a library for that. you can't just do that – Jason Hu Jun 27 '15 at 23:15

2 Answers2

2

It's not so easy.

  1. Most cameras won't work in read/write mode. You need to use Streaming I/O mode - for example memory mapping.
  2. You need to set pixel format - YUYV/RGB/MJPEG, bytes per pixel, resolution.
  3. You have to start grabbing, read and save at least one frame.
Tomasz Myrta
  • 1,114
  • 8
  • 10
1

Further to my comment, here's an example of displaying a video stream from video on disk (see documentation):

import numpy as np
import cv2

video = "../videos/short.avi"

video_capture = cv2.VideoCapture(video)

while(True):
    # Capture frame-by-frame
    ret, frame = video_capture.read()

    # Our operations on the frame comes here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything's done, release the capture
video_capture.release()
cv2.destroyAllWindows()
boardrider
  • 5,882
  • 7
  • 49
  • 86