|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
用flask写了一个小程序,结合cv2可以实现在网页上开摄像头
- import cv2
- from flask import Flask, render_template, Response
- import time
- from pyzbar import pyzbar
- import numpy as np
- app = Flask(__name__)
- @app.route('/')
- def index():
- return render_template(r'index.html')
- def gen():
- vid = cv2.VideoCapture(0)
- while True:
- return_value, frame = vid.read()
- image = cv2.imencode('.jpg', frame)[1].tobytes()
- yield (b'--frame\r\n'
- b'Content-Type: image/jpeg\r\n\r\n' + image + b'\r\n')
- # print(image)
- img_data = cv2.imread(image.decode("ascii"))
- # 转为灰度图像
- gray = cv2.cvtColor(img_data, cv2.COLOR_BGR2GRAY)
- barcodes = pyzbar.decode(gray)[0].data.decode("utf-8")# 二维码内容
- print(barcodes)
- time.sleep(0.1)
- @app.route('/sm')
- def video_feed():
- return Response(gen(), mimetype='multipart/x-mixed-replace; boundary=frame')
- if __name__ == '__main__':
- app.run(host='0.0.0.0', port=80, debug = True)
复制代码
原理应该是保存每一张从摄像头拍到图片,然后将图片传到html,就实现了在网页端调去摄像头
既然这样,我就检测一下这些图片是否有二维码(有的话识别)
毕竟它也是一个while,就省事很多了
然后我就找了一个解析二维码的语句- gray = cv2.cvtColor(img_data, cv2.COLOR_BGR2GRAY)
- barcodes = pyzbar.decode(gray)[0].data.decode("utf-8")# 二维码内容
- print(barcodes)
复制代码
看前面有个image- image = cv2.imencode('.jpg', frame)[1].tobytes()
复制代码
就想直接拿来用
结果发现不行,好像需要解码
但是试过了.decode("utf-8")和decode("ascii")都不行
现在来请教一下大佬们如何解码到解析二维码库可以识别的样子 |
|