python bokeh:从网络摄像头获取图像并将其显示在仪表板中 [英] python bokeh: get image from webcam and show it in dashboard

查看:209
本文介绍了python bokeh:从网络摄像头获取图像并将其显示在仪表板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示图像-例如使用网络摄像头捕获-散景.我尝试了image_url和image_rgba,但是都无法正常工作. image_url没有显示任何内容,image_rgb显示了一些内容,但是似乎有一些索引shif.

I want to display a an image - e.g. capture with the webcam - in bokeh. I tried image_url and image_rgba, but both are not working. Image_url is showing nothing, image_rgb shows somethin, but there seems to be some index shif.

    # -*- coding: utf-8 -*-

from bokeh.plotting import figure, show
import scipy.misc
import cv2
import matplotlib.pyplot as plt
import os

# capture video frame from webcam
#vc = cv2.VideoCapture(1)
vc = cv2.VideoCapture(-0)
rval, frame = vc.read()
vc.release()

# show captured image (figure 1)
fig = plt.figure()
ax = plt.Axes(fig,[0,0,1,1])
ax.set_axis_off()
fig.add_axes(ax)
ax.imshow(frame)
plt.show()

# save captured image
scipy.misc.imsave('outfile.jpg', frame)
mypath = os.path.join(os.getcwd(),'outfile.jpg')

# 1. try using image_url ... not working (figure 2)
p = figure()
p.image_url(url=[mypath],x=[0],y=[1],w=[10],h=[10])
show(p)

# 2. try using image_rgba ... also not working (figure 3)
p = figure(x_range=(0,10), y_range=(0,10))
p.image_rgba(image=[frame], x=0, y=0, dw=10, dh=10)
show(p)

# 3. solution provided by @bigreddot ... flipped but also not working (figure4)
img = scipy.misc.imread(mypath)[::-1] # flip y-direction for Bokeh
p = figure(x_range=(0,10), y_range=(0,10))
p.image_rgba(image=[img], x=0, y=0, dw=10, dh=10)
show(p)

更新: :根据@bigreddot的评论

update: according to @bigreddot 's comment

图1

图2

图3

图4

我使用的bokeh版本是0.13,python版本3.6.0

the bokeh version I use is 0.13 the python version 3.6.0

推荐答案

经过调查,OpenCV的返回结果是一个Numpy字节数组,其形状为(M,N,3),即RGB 元组. Bokeh期望的是一个形状为(M,N)的Numpy数组,它表示RGBA值的32位整数.因此,您需要从一种格式转换为另一种格式.这是一个完整的示例:

After investigation, the return result from OpenCV is a Numpy array of bytes with shape (M, N, 3), i.e. RGB tuples. What Bokeh expects is a Numpy array of shape (M, N) 32-bit integers representing RGBA values. So you need to convert from one format to the other. Here is a complete example:

from bokeh.plotting import figure, show
import scipy.misc
import cv2
import os
import numpy as np

#vc = cv2.VideoCapture(1)
vc = cv2.VideoCapture(-0)
rval, frame = vc.read()
vc.release()

M, N, _ = frame.shape
img = np.empty((M, N), dtype=np.uint32)
view = img.view(dtype=np.uint8).reshape((M, N, 4))
view[:,:,0] = frame[:,:,0] # copy red channel
view[:,:,1] = frame[:,:,1] # copy blue channel
view[:,:,2] = frame[:,:,2] # copy green channel
view[:,:,3] = 255

img = img[::-1] # flip for Bokeh

p = figure()
p.image_rgba(image=[img],x=0,y=0, dw=10, dh=10)
show(p)

生成:

这篇关于python bokeh:从网络摄像头获取图像并将其显示在仪表板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆