700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > opencv resize_树莓派监控摄像头python+picamera或openCV

opencv resize_树莓派监控摄像头python+picamera或openCV

时间:2020-08-27 09:33:30

相关推荐

opencv resize_树莓派监控摄像头python+picamera或openCV

1、在raspi-config中使能摄像头

打开树莓派终端,输入sudo raspi-config

完成后重启树莓派

2、检查摄像头运行情况

vcgencmd get_camera

raspistill命令行测试拍照

raspistill -v -o test.jpg

执行后如下图操作:

3、上面都是设置开启摄像头及测试,我们接下来要使用python调用摄像头,完成拍照和直播流的功能。

树莓派4B安装opencv:

sudo apt-get install -y libopencv-dev python3-opencv

网络不好的,下载会中断,多执行以上命令几次就好了,段点续传。

安装完成后

输入命令:

python3

import cv2

看到以下无错误显示即可

4、使用opencv调用树莓派拍照

(1)、 打开usb摄像头读取一张照片

import cv2

import matplotlib.pyplot as plt

#opencv调用csi摄像头优先0,然后usb按顺序排列下去

capture = cv2.VideoCapture(0)

# 获取一帧

ret, frame = capture.read()

plt.imshow(frame[:,:,::-1])#BGRtoRGB

plt.show()

# 释放资源

capture.release()

5、Csi排线的可以直接用树莓派官方系统自带的picamera库

(1)、捕捉一个图像输出至文件

使用capture方法可以轻松将捕捉到的图像输出至指定文件。 下面这个实例是捕捉一个分辨率为1024*768的图像,并将之输出到foo.jpg中

import time

import picamera

with picamera.PiCamera() as camera:

camera.resolution = (1024, 768)

camera.start_preview()

#摄像头预热2秒

time.sleep(2)

#camera.capture('foo.jpg', resize=(320, 240))#可改变分辨率

camera.capture('foo.jpg')

(2)、如果你不想使用有损JPEG编码,并希望加快这一解码过程的话,可以使用picamera自带的picamera.array模块。可以使用PiRGBArray类简单的捕获'brg'格式的数据。(假定RGB与BGR是分辨率相同的数据,只是具有相反的颜色)

import time

import picamera

import picamera.array

import cv2

with picamera.PiCamera() as camera:

camera.start_preview()

time.sleep(2)

with picamera.array.PiRGBArray(camera) as stream:

camera.capture(stream, format='rgb')#format类型:bgrgbh264

# 此时就可以获取到bgr的数据流了

image = stream.array

import matplotlib.pyplot as plt

image_resize = cv2.resize(image,(320, 240)) #opencv改变分辨率

plt.imshow(image_resize)

6、使用python做一个简单的远程监控摄像头

使用opencv 和python自带的http库就能完成(为什么不用picamera?因为通用呀,不一定树莓派,只要装了opencv的硬件,这代码就可以运行),使用浏览器,输入树莓派地址:8080直接显示,上代码:

import cv2

from http import server

import time

#做一个响应主页面html

PAGE="""

Video Streaming Demonstration

Video Streaming Demonstration

"""

# 通过opencv获取实时视频流

video = cv2.VideoCapture(0)

def get_frame(v):

success, image = v.read()

# 因为opencv读取的图片并非jpeg格式,因此要用motion JPEG模式需要先将图片转码成jpg格式图片

ret, jpeg = cv2.imencode('.jpg', image)

return jpeg.tobytes()

def gen(camera):

while True:

frame = get_frame(camera)

# 使用generator函数输出视频流, 每次请求输出的content类型是image/jpeg

yield (b'--frame'

b'Content-Type: image/jpeg' + frame + b'')

class HTTPHandler(server.BaseHTTPRequestHandler):

def do_GET(self):#get数据处理

if self.path == '/': #跳转至默认页面

self.send_response(301)

self.send_header('Location', '/index.html')

self.end_headers()

elif self.path == '/index.html':

content = PAGE.encode('utf-8')

self.send_response(200)

self.send_header('Content-Type', 'text/html')

self.send_header('Content-Length', len(content))

self.end_headers()

self.wfile.write(content)

elif self.path == '/video_feed':

self.send_response(200)

self.send_header('Content-Type','multipart/x-mixed-replace; boundary=frame')

self.end_headers()

while True:

self.wfile.write(next(cam)) #必须用next()才能运行生成器

self.wfile.write(b'')

else:

self.send_error(404)

self.end_headers()

cam = gen(video)#生成器

try:

print("http server start...")

address = ('', 8080)

server = server.HTTPServer(address, HTTPHandler)

server.serve_forever()

finally:

print('done')

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。