从python中的/dev/video0读取摄像机输入

2024-06-30 15:08:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过c或python读取文件/dev/video0,并将传入的字节存储在另一个文件中。 这是我的c代码:

#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;
}

当我运行这段代码并在一段时间后终止程序时,除了生成一个文件名“image”之外,什么都没有发生,这是很常见的。在

这是我的python代码:

^{pr2}$

这是我运行这个片段时的错误:

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

我想知道如何使用纯c或python实现这一点密码。请没有外部库建议。在


Tags: 文件代码devimage字节includebuffersys
2条回答

没那么容易。在

  1. 大多数相机不能在读/写模式下工作。例如,您需要使用Streaming I/O mode-作为内存映射。在
  2. 您需要设置pixel format-YUYV/RGB/MJPEG,每像素字节数,分辨率。在
  3. 你必须开始抓取,阅读和保存至少一帧。在

关于我的评论,下面是一个在磁盘上显示视频流的示例(请参见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()

相关问题 更多 >