python + opencv在imagepy中开发USB相机插件
Posted 薛定猫啊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python + opencv在imagepy中开发USB相机插件相关的知识,希望对你有一定的参考价值。
最近发现了imagepy这个宝藏,正好师兄毕业了,留下了一个USB相机,为了方便使用,结合imagepy开发一个USB相机插件,在此记录一下。
持续更新……
文章目录
- imagepy
- 开发USB相机插件
- 搭建界面、绑定功能
- 开发功能、
- 结果展示
- 错误解决
- [ WARN:0] terminating async callback
ImagePy是一个用Python编写的开源图像处理框架。其UI界面、图像数据结构和表数据结构分别是基于wxpython的,基于numpy的和基于panda的。此外,它支持任何基于Numpy和Pandas的插件,兼容scipy、Ndimage, scikit-image, simpleitk, opencv等图像处理库。
imagepy使开发者不需要关注UI和交互设计,而只关注算法本身,对于学习图像处理来说简直太舒服了!
开发USB相机插件搭建界面、绑定功能
相机界面比较简单,满足日常使用就行,比如打开相机、接收画面、读取一帧、关闭相机
import wx, numpy as np
from sciapp.object import Image
import cv2.cv2 as cv2
class Plugin(wx.Panel):
title = cameraSimple
def __init__(self, parent, app=None):
wx.Panel.__init__(self, parent)
self.app = app
sizer = wx.BoxSizer(wx.VERTICAL)
self.lable = wx.StaticText(self, wx.ID_ANY, Camera Demo)
self.lable.Wrap(-1)
sizer.Add(self.lable, 0, wx.ALIGN_CENTER | wx.ALL, 5)
self.btn_open = wx.Button(self, wx.ID_ANY, 打开相机)
sizer.Add(self.btn_open, 0, wx.ALIGN_CENTER | wx.ALL, 5)
self.btn_grapImgs = wx.Button(self, wx.ID_ANY, 接收画面)
sizer.Add(self.btn_grapImgs, 0, wx.ALIGN_CENTER | wx.ALL, 5)
self.btn_close = wx.Button(self, wx.ID_ANY, 关闭相机)
sizer.Add(self.btn_close, 0, wx.ALIGN_CENTER | wx.ALL, 5)
self.btn_grapOne = wx.Button(self, wx.ID_ANY, 获取一帧)
sizer.Add(self.btn_grapOne, 0, wx.ALIGN_CENTER | wx.ALL, 5)
self.SetSizer(sizer)
self.Layout()
self.Fit()
self.btn_open.Bind(wx.EVT_BUTTON, self.open)
self.btn_grapImgs.Bind(wx.EVT_BUTTON, self.grapImgs)
self.btn_close.Bind(wx.EVT_BUTTON, self.close)
self.btn_grapOne.Bind(wx.EVT_BUTTON, self.frame)
self.isopen = False
界面:
哈哈,比较简陋,自己够用就行了 U•ェ•*U
开发功能、
def grapImgs(self, event=None):结果展示
print(grap Images......)
self.app.info(grap Images......)
while True:
if self.isopen:
hasData, img = self.camera.read()
if hasData:
cv2.imshow(camera, img)
cv2.waitKey(10)
else:
self.app.info(no data!)
print(no data!)
break
else:
self.app.info(please open a camera!)
print(please open a camera!)
break
def frame(self, event=None):
if self.isopen:
hasData, img = self.camera.read()
if hasData:
myimg = Image([img[..., ::-1]])
self.app.show_img(myimg, frame)
print(grap One Image......)
self.app.info(grap One Image......)
else:
self.app.info(no data!)
print(no data!)
else:
self.app.info(please open a camera!)
print(please open a camera!)
def open(self, event=None):
if self.isopen:
self.app.info(camera has opened!)
print(camera has opened!)
else:
self.camera = cv2.VideoCapture(0, cv2.CAP_DSHOW)
self.isopen = True
self.app.info(camera opened!)
print(camera opened!)
def close(self, event=None):
if self.isopen:
self.isopen = False
cv2.waitKey(20)
self.camera.release()
cv2.destroyAllWindows()
self.app.info(camera closed!)
print(camera closed!)
else:
self.app.info(camera has closed!)
print(camera has closed!)
后续考虑开发HIK Camera、Basler Camera相机插件。
ヾ(◍°∇°◍)ノ゙
错误解决[ WARN:0] terminating async callback
解决方法:
camera = cv2.VideoCapture(camera_port,cv2.CAP_DSHOW)
参考:https://stackoverflow.com/questions/53888878/cv2-warn0-terminating-async-callback-when-attempting-to-take-a-picture
以上是关于python + opencv在imagepy中开发USB相机插件的主要内容,如果未能解决你的问题,请参考以下文章