usb摄像头问题

如何将摄像头拍到的画面以全部的屏幕展示出,使用了Display函数还是不行

你好,可以粘贴下具体的代码和方案,使用应用实例是否有问题呢?

#二维码识别

#!/usr/bin

from hobot_dnn import pyeasy_dnn as dnn

from hobot_vio import libsrcampy as srcampy

import cv2

import time

import numpy as np

import os

from pyzbar import pyzbar

def get_display_res():

if os.path.exists(“/usr/bin/8618_get_edid”) == False:

return 1920, 1080

import subprocess

p = subprocess.Popen([“/usr/bin/8618_get_edid”], stdout=subprocess.PIPE)

result = p.communicate()

res = result[0].split(b’,')

res[1] = max(min(int(res[1]), 1920), 0)

res[0] = max(min(int(res[0]), 1080), 0)

return int(res[1]), int(res[0])

def bgr2nv12_opencv(image):

height, width = image.shape[0], image.shape[1]

area = height * width

yuv420p = cv2.cvtColor(image, cv2.COLOR_BGR2YUV_I420).reshape((area * 3 // 2,))

y = yuv420p[:area]

uv_planar = yuv420p[area:].reshape((2, area // 4))

uv_packed = uv_planar.transpose((1, 0)).reshape((area // 2,))

nv12 = np.zeros_like(yuv420p)

nv12[:height * width] = y

nv12[height * width:] = uv_packed

return nv12

capture = cv2.VideoCapture(0)

# capture.set(cv2.CAP_PROP_FRAME_WIDTH,360)

# capture.set(cv2.CAP_PROP_FRAME_HEIGHT,360)

if __name__==‘__main__’:

disp1 = srcampy.Display()

disp_w, disp_h = get_display_res()

ret1 = disp1.display(0, disp_w, disp_h, 0, 1)

disp2 = srcampy.Display()

ret2 = disp2.display(0, disp_w, disp_h, 0, 1)

while capture.isOpened():

# 首先我们要用刚才实例化的摄像头来采集实时的照片,

_, frame = capture.read()

frame_resize = cv2.resize(frame, (disp_w, disp_h), interpolation=cv2.INTER_AREA)

nv12_data = bgr2nv12_opencv(frame_resize)

ret1 = disp1.set_img(nv12_data.tobytes())

# 用pyzbar的函数解析图像里的二维码和条形码

test = pyzbar.decode(frame)

# 循环检测到的条形码

for tests in test:

disp1.close()

# 获取到的条形码数据转换成字符串

testdate = tests.data.decode(‘utf-8’)

# 获取条形码类型

testtype = tests.type

# 绘出图像上条形码的数据和条形码类型

printout = “{}”.format(testdate)

src = cv2.imread(‘/home/sunrise/python/white.webp’)

#img = src[0:200, 0:200]

# 调用cv.putText()添加文字

if len(printout) >= 4:

text1 = printout.split(“+”)

text = text1[0] + “4” + text1[1]

else:

text = printout

print(text)

AddText = src.copy()

res = cv2.putText(AddText, printout, (300, 300), cv2.FONT_HERSHEY_COMPLEX, 5, (0, 0, 0), 5)

img_resize=cv2.resize(res,(disp_w,disp_h),interpolation=cv2.INTER_AREA)

nv12_data=bgr2nv12_opencv(img_resize)

ret2=disp2.set_img(nv12_data.tobytes())

# 显示预览窗口

#cv2.imshow(‘Scanning’, frame)

# 按q退出

key=cv2.waitKey(1)

print(key)

if key==27:

disp2.close()

exit(-1)

key=cv2.waitKey(1)

if key==27:

break

disp2.close()

# 释放摄像头

capture.release()

# 释放所有显示窗口

cv2.destroyAllWindows()

这个代码可以先打开全屏摄像头拍到的数据,但是,一旦拍到二维码之后就直接结束了,而我想要让他全屏显示出二维码的数字

并且,这个我按下了Esc后,也没有任何反应

代码运行到那里结束了呢?

#二维码识别-
#!/usr/bin-
from hobot_dnn import pyeasy_dnn as dnn-
from hobot_vio import libsrcampy as srcampy-
import cv2-
import time-
import numpy as np-
import os-
from pyzbar import pyzbar-

def get_display_res():-
if os.path.exists(“/usr/bin/8618_get_edid”) == False:-
return 1920, 1080-

import subprocess-
p = subprocess.Popen([“/usr/bin/8618_get_edid”], stdout=subprocess.PIPE)-
result = p.communicate()-
res = result[0].split(b’,')-
res[1] = max(min(int(res[1]), 1920), 0)-
res[0] = max(min(int(res[0]), 1080), 0)-
return int(res[1]), int(res[0])-

def bgr2nv12_opencv(image):-
height, width = image.shape[0], image.shape[1]-
area = height * width-
yuv420p = cv2.cvtColor(image, cv2.COLOR_BGR2YUV_I420).reshape((area * 3 // 2,))-
y = yuv420p[:area]-
uv_planar = yuv420p[area:].reshape((2, area // 4))-
uv_packed = uv_planar.transpose((1, 0)).reshape((area // 2,))-

nv12 = np.zeros_like(yuv420p)-
nv12[:height * width] = y-
nv12[height * width:] = uv_packed-
return nv12-
capture = cv2.VideoCapture(0)-
# capture.set(cv2.CAP_PROP_FRAME_WIDTH,360)-
# capture.set(cv2.CAP_PROP_FRAME_HEIGHT,360)-
if __name__==‘__main__’:-
disp1 = srcampy.Display()-
disp_w, disp_h = get_display_res()-
ret1 = disp1.display(0, disp_w, disp_h, 0, 1)-
disp2 = srcampy.Display()-
ret2 = disp2.display(0, disp_w, disp_h, 0, 1)-
while capture.isOpened():-
# 首先我们要用刚才实例化的摄像头来采集实时的照片,-

_, frame = capture.read()-
frame_resize = cv2.resize(frame, (disp_w, disp_h), interpolation=cv2.INTER_AREA)-
nv12_data1 = bgr2nv12_opencv(frame_resize)-
ret1 = disp1.set_img(nv12_data1.tobytes())-
# 用pyzbar的函数解析图像里的二维码和条形码-
test = pyzbar.decode(frame)-
print(test)-
if test!=:-
disp1.close()-
print(1)-
# 循环检测到的条形码-
for tests in test:-
# 获取到的条形码数据转换成字符串-
testdate = tests.data.decode(‘utf-8’)-
# 获取条形码类型-
testtype = tests.type-

# 绘出图像上条形码的数据和条形码类型-
printout = “{}”.format(testdate)-
src = cv2.imread(‘/home/sunrise/python/white.webp’)-
#img = src[0:200, 0:200]-

# 调用cv.putText()添加文字-

if len(printout) >= 4:-
text1 = printout.split(“+”)-
text = text1[0] + “4” + text1[1]-
else:-
text = printout-
print(text)-
AddText = src.copy()-
print(2)-
res = cv2.putText(AddText, printout, (300, 300), cv2.FONT_HERSHEY_COMPLEX, 5, (0, 0, 0), 5)-
print(3)-
img_resize=cv2.resize(res,(disp_w,disp_h),interpolation=cv2.INTER_AREA)-
print(4)-
nv12_data2=bgr2nv12_opencv(img_resize)-
print(5)-
ret2=disp2.set_img(nv12_data2.tobytes())-
print(6)-

# 显示预览窗口-
#cv2.imshow(‘Scanning’, frame)-
# 按q退出-
key=cv2.waitKey(1)-
if key==113:-
break-

disp2.close()-
# 释放摄像头-
capture.release()-
# 释放所有显示窗口-
cv2.destroyAllWindows()-

然后,输出的结果是

1

1234321

2

3

4

5

Segmentation fault

多次调用disp模块,去掉前面的,然后看一下数据是否对齐