IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> 如何快速连接Basler工业摄像头获取并保存图像和视频(python+opencv+pypylon) -> 正文阅读

[人工智能]如何快速连接Basler工业摄像头获取并保存图像和视频(python+opencv+pypylon)

写在前面:电脑已经安装anaconda(或者miniconda)
如有需要,请联系:qq:2953392202

1.打开anaconda
在这里插入图片描述
2.搭建虚拟环境

conda create -n yanshi python=3.7

在这里插入图片描述
3.激活虚拟环境

conda activate yanshi

在这里插入图片描述
4.新建文件夹用于存python文件
在这里插入图片描述
在这里插入图片描述
5.进入虚拟环境

cd yanshi

在这里插入图片描述
6.下载opencv-python

pip install opencv-python

在这里插入图片描述
7.下载pypylon

pip3 install pypylon

在这里插入图片描述
8.用记事本新建一个python文件并保存
在这里插入图片描述

在这里插入图片描述
9.将以下python代码复制到新建的文件中

from pypylon import pylon
import cv2
import datetime
import time
# conecting to the first available camera
 
camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
 
# Grabing Continusely (video) with minimal delay
camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)
converter = pylon.ImageFormatConverter()
 
# converting to opencv bgr format
converter.OutputPixelFormat = pylon.PixelType_BGR8packed
converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned
 
while camera.IsGrabbing():
    grabResult = camera.RetrieveResult(1000, pylon.TimeoutHandling_ThrowException)
 
    if grabResult.GrabSucceeded():
        # Access the image data
        image = converter.Convert(grabResult)
        img = image.GetArray()
        cv2.namedWindow('title', cv2.WINDOW_NORMAL)
        cv2.imshow('title', img)
        k = cv2.waitKey(1)
        #按键盘“s"保存图像
        if k == ord('s'):
           #保存图像路径
            cv2.imwrite('D:/learn/photos/a.jpg', img)
           #按键盘‘q'退出
        elif k == ord('q'):
            break
    grabResult.Release()
 
# Releasing the resource
camera.StopGrabbing()
 
cv2.destroyAllWindows()

以上代码是获取摄像头图像

10.在终端中跑python文件

python huoqu.py

在这里插入图片描述
附:
连续获取获取摄像头图像并保存的代码:

#连续输出并保存图像
from pypylon import pylon
import cv2
import datetime
import time
 
# conecting to the first available camera
 
camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
 
# Grabing Continusely (video) with minimal delay
camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)
converter = pylon.ImageFormatConverter()
 
# converting to opencv bgr format
converter.OutputPixelFormat = pylon.PixelType_BGR8packed
converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned
i = 0
while camera.IsGrabbing():
    grabResult = camera.RetrieveResult(1000, pylon.TimeoutHandling_ThrowException)
 
    if grabResult.GrabSucceeded():
        # Access the image data
        image = converter.Convert(grabResult)
        img = image.GetArray()
        cv2.namedWindow('title', cv2.WINDOW_NORMAL)
        cv2.imshow('title', img)
        #连续输出图像路径,按数字顺序给图像命名
        cv2.imwrite('D:/learn/photos/'+str(i)+'.jpg', img)
        i=i+1
        #每隔0.025秒采集一张图像
        time.sleep(0.025)
        k = cv2.waitKey(1)
        if k == ord('s'):
            break
    grabResult.Release()
 
# Releasing the resource
camera.StopGrabbing()
 
cv2.destroyAllWindows()

将图像组合成视频的代码:

#组合图片为视频
import cv2
import os
 
#图片路径
im_dir = 'D:/learn/photos'
#输出视频路径
video_dir = 'D:/learn/photos/video/a.avi'
#帧率
fps = 30
#图片数
num = 232
#图片尺寸
img_size = (1920,1200)
 
#fourcc = cv2.cv.CV_FOURCC('M','J','P','G')#opencv2.4
fourcc = cv2.VideoWriter_fourcc('M','J','P','G') #opencv3.0
videoWriter = cv2.VideoWriter(video_dir, fourcc, fps, img_size)
 
for i in range(1,num):
 im_name = os.path.join(im_dir, str(i)+'.jpg')
 frame = cv2.imread(im_name)
 videoWriter.write(frame)
 
videoWriter.release()
print('finish')

直接获取视频代码:

#连续输出并保存图像
from pypylon import pylon
import cv2
import os

import time
# conecting to the first available camera
 
camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
 
# Grabing Continusely (video) with minimal delay
camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)
converter = pylon.ImageFormatConverter()
 
# converting to opencv bgr format
converter.OutputPixelFormat = pylon.PixelType_BGR8packed
converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned
i = 0
while camera.IsGrabbing():
    grabResult = camera.RetrieveResult(1000, pylon.TimeoutHandling_ThrowException)
 
    if grabResult.GrabSucceeded():
        # Access the image data
        image = converter.Convert(grabResult)
        img = image.GetArray()
        cv2.namedWindow('title', cv2.WINDOW_NORMAL)
        cv2.imshow('title', img)
        cv2.imwrite('D:/learn/photos/'+str(i)+'.jpg', img)
        i=i+1
        time.sleep(0.025)
        k = cv2.waitKey(1)
        if k == ord('s'):
            #图片路径
            im_dir = 'D:/learn/photos'
#输出视频路径
            video_dir = 'D:/learn/photos/video/a.avi'
#帧率
            fps = 40
#图片数
            num = i
#图片尺寸
            img_size = (1920,1200)
            fourcc = cv2.VideoWriter_fourcc('M','J','P','G') #opencv3.0
            videoWriter = cv2.VideoWriter(video_dir, fourcc, fps, img_size)
 
            for i in range(1,num):
                im_name = os.path.join(im_dir, str(i)+'.jpg')
                frame = cv2.imread(im_name)
                videoWriter.write(frame)
            videoWriter.release()
            print('finish')
            break
 
   
grabResult.Release()
# Releasing the resource
camera.StopGrabbing()
cv2.destroyAllWindows()
  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2022-01-04 13:26:12  更:2022-01-04 13:26:34 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/18 22:49:59-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码