Linux USB摄像头视频捕获解决方案

版权申诉
0 下载量 149 浏览量 更新于2024-10-06 收藏 127KB RAR 举报
资源摘要信息:"Linux下的视频捕获程序与USB摄像头的使用方法" Linux操作系统由于其开源和灵活的特性,在视频捕获方面拥有众多的工具和库。视频捕获程序能够帮助用户实现视频数据的采集,而USB摄像头是进行视频捕捉的常用硬件设备之一。这些程序通常依赖于Linux内核提供的V4L2(Video for Linux Two)驱动程序接口,为开发者提供了丰富的功能。 1. Linux视频捕获程序 在Linux系统中,视频捕获程序一般利用V4L2这一内核子系统,它为视频设备提供了标准的接口。开发者可以使用V4L2 API编写应用程序来访问和控制视频设备,如USB摄像头。这样的视频捕获程序一般具备调整摄像头参数(如亮度、对比度、饱和度等)、捕获原始视频帧、编码视频流等功能。 2. USB摄像头的使用 USB摄像头是一种通过USB接口连接到计算机的视频输入设备,广泛用于视频会议、监控以及个人视频捕获等场合。在Linux系统中,当USB摄像头接入后,系统通过识别其厂商ID和产品ID自动加载相应的驱动程序。常见的驱动程序有uvcvideo,它支持大多数符合USB视频类设备规范的摄像头。 Linux下的视频捕获程序通常需要以下步骤: a. 确认摄像头驱动是否正确加载。可以通过lsusb命令查看USB设备列表,确认摄像头是否被识别。 b. 使用相应的视频捕获工具或库(如ffmpeg, v4l2-utils等)进行视频捕获操作。 c. 利用V4L2 API编写应用程序来控制和捕获视频数据。 d. 调整视频捕获参数,比如分辨率、帧率等。 e. 将捕获的视频数据进行编码、存储或实时传输。 3. Linux视频捕获程序的重要库和工具 Linux视频捕获程序常使用的库和工具包括: - V4L2(Video for Linux Two):Linux内核中用于视频设备的驱动框架。 - ffmpeg:一个完整的、跨平台的多媒体框架,用于视频录制、转换等多种功能。 - v4l2-utils:一组工具,用于操作和调试V4L2设备。 - libv4l:一个库,提供了对V4L2驱动的额外封装,使应用程序更容易访问视频设备。 4. 实际应用示例 例如,使用ffmpeg工具进行视频捕获的基本命令格式如下: ```bash ffmpeg -f v4l2 -i /dev/video0 output.mp4 ``` 上述命令中,“-f v4l2”指定了输入格式为V4L2,即表示来自USB摄像头的数据。“-i /dev/video0”指定了设备文件,通常是系统分配给摄像头的设备文件。“output.mp4”指定了输出文件的名称。 5. 注意事项 在使用Linux视频捕获程序和USB摄像头时,还需注意以下几点: - 确保摄像头与Linux系统的兼容性。 - 根据摄像头的型号和规格,可能需要安装特定的驱动程序。 - 管理好摄像头设备文件的权限,避免安全风险。 - 考虑到多任务处理能力,合理配置系统资源。 通过上述介绍,我们可以看到Linux视频捕获程序和USB摄像头的结合使用为用户提供了强大的视频捕获能力,无论是在个人娱乐还是专业应用中都有着广泛的应用前景。

import cv2 import sys import os import time from PyQt5 import QtGui #重新导入 from PyQt5 import QtCore #重新导入 from showPic import Ui_MainWindow from PyQt5.QtCore import * from PyQt5.QtGui import * #导入的外面 from PyQt5.QtWidgets import * camera_path = 0 # 0:自带摄像头 1:外接摄像头 "xxx.mp4" "rtsp://admin:pwd@192.168.2.10/cam/..." capture = cv2.VideoCapture(camera_path) # 初始化播放器 流媒体 fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V') # XVID/DIVX MPEG MJPG X264 video_writer = cv2.VideoWriter("image/myself.mp4", fourcc, 25, (960, 540)) # 存放路径、、帧率fps、尺寸(且保证下面的frame也是这个尺寸) class videoShow(QMainWindow, Ui_MainWindow): def __init__(self): super().__init__() self.setupUi(self) self.dir_path = r"E:\pycharm\new_subject\image/" self.pushButton_play.clicked.connect(self.play_video) self.pushButton_pause.clicked.connect(self.pause_video) def play_video(self): self.playing = True def pause_video(self): self.playing = False def timer_pic(self): image_name = self.dir_path + self.file_list[self.n] url = image_name pic_image = cv2.imread(url) pic_image = cv2.cvtColor(pic_image, cv2.COLOR_BGR2RGB) # 将BGR格式图像转换成RGB height, width = pic_image.shape[:2] pixMap = QImage(pic_image.data, width, height, width*3, QImage.Format_RGB888) # 将RGB格式图像转换为八位图 pixMap = QPixmap.fromImage(pixMap) ratio = max(width/self.label.width(), height/self.label.height()) pixMap.setDevicePixelRatio(ratio) # 根据图片比例显示 self.label.setAlignment(Qt.AlignCenter) # 设置居中 self.label.setPixmap(pixMap) if self.playing: flag, frame = capture.read() if flag is False: return frame = cv2.resize(frame, (960, 540)) video_writer.write(frame) cv2.namedWindow("video", 0) cv2.imshow("video", frame) key = cv2.waitKey(25) if key == 27: video_writer.release() cv2.destroyAllWindows() sys.exit(0) if __name__ == '__main__': app = QApplication(sys.argv) ui = videoShow() ui.show() sys.exit(app.exec_()) 优化这段代码,实现录制视频以及点击按钮实现录制、播放、暂停

2023-07-15 上传

import cv2 import face_recognition import numpy as np from PIL import Image, ImageDraw,ImageFont video_capture = cv2.VideoCapture(r'C:/Users/ALIENWARE/123.mp4')#如果输入是(0)为摄像头输入 #现输入为MP4进行识别检测人脸 first_image = face_recognition.load_image_file("1.jpg") first_face_encoding = face_recognition.face_encodings(first_image)[0] Second_image = face_recognition.load_image_file("2.jpg") Second_face_encoding = face_recognition.face_encodings(Second_image)[0] third_image = face_recognition.load_image_file("3.jpg") third_face_encoding = face_recognition.face_encodings(third_image)[0] inside_face_encodings = [first_face_encoding,Second_face_encoding,third_face_encoding] inside_face_names = ['A','B','C'] face_locations = [] face_encodings = [] face_names = [] process_this_frame = True while True: ret, frame = video_capture.read() small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) rgb_small_frame = small_frame[:, :, ::-1] if process_this_frame: face_locations = face_recognition.face_locations(rgb_small_frame) face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations) face_names = [] for face_encoding in face_encodings: matches = face_recognition.compare_faces(inside_face_encodings, face_encoding) name = '未录入人脸' if True in matches: first_match_index = matches.index(True) name = inside_face_names[first_match_index] face_names.append(name) process_this_frame = not process_this_frame for (top, right, bottom, left), name in zip(face_locations, face_names): top *= 4 right *= 4 bottom *= 4 left *= 4 cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) img_pil = Image.fromarray(frame) draw = ImageDraw.Draw(img_pil) fontStyle = ImageFont.truetype("C:/Windows/Fonts/simsun.ttc", 32, encoding="utf-8") draw.text((left + 6, bottom - 6), name, (0, 200, 0), font=fontStyle) frame = np.asarray(np.array(img_pil)) cv2.imshow('face_out', frame) if cv2.waitKey(1) & 0xFF == ord('q'): #退出需要按下Q键否则内核会崩溃 break video_capture.release() cv2.destroyAllWindows()

2023-06-07 上传