python恶搞代码打开对方摄像头,python bokeh:从网络摄像头获取图像并将其显示在仪表板中...

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)

update: according to @bigreddot 's comment

figure 1

bonfN.png

figure 2

fxvES.png

figure 3

VQRmE.png

figure 4

vKhYT.png

the bokeh version I use is 0.13 the python version 3.6.0

解决方案

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)

Generates:

qIFUr.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值