import cv2
import numpy as np
from pyzbar.pyzbar import decode
# img = cv2.imread('fe156491210e36fbe16531dda1bef8b.jpg')
# code = decode(img)
# print(code)
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
while True:
success, img = cap.read()
for barcode in decode(img):
myData = barcode.data.decode('utf-8')
print(myData)
pts = np.array([barcode.polygon], np.int32)
pts = pts.reshape((-1, 1, 2))
cv2.polylines(img, [pts], True, (255, 0, 255), 5)
pts2 = barcode.rect
cv2.putText(img, myData, (pts2[0], pts2[1]), cv2.FONT_HERSHEY_SIMPLEX,
0.9, (255, 0, 255), 2)
cv2.imshow('Result', img)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
上个是qr
下一个是有GUI
import PySimpleGUI as sg #pip install pysimplegui
import cv2 #pip install opencv-python
import numpy as np #pip install numpy
# 背景色
sg.theme('LightGreen')
# 定义窗口布局
layout = [
[sg.Image(filename='', key='image')],
[sg.Radio('None', 'Radio', True, size=(10, 1))],
[sg.Radio('threshold', 'Radio', size=(10, 1), key='thresh'),
sg.Slider((0, 255), 128, 1, orientation='h', size=(40, 15), key='thresh_slider')],
# [sg.Radio('canny', 'Radio', size=(10, 1), key='canny'),
# sg.Slider((0, 255), 128, 1, orientation='h', size=(20, 15), key='canny_slider_a'),
# sg.Slider((0, 255), 128, 1, orientation='h', size=(20, 15), key='canny_slider_b')],
# [sg.Radio('contour', 'Radio', size=(10, 1), key='contour'),
# sg.Slider((0, 255), 128, 1, orientation='h', size=(20, 15), key='contour_slider'),
# sg.Slider((0, 255), 80, 1, orientation='h', size=(20, 15), key='base_slider')],
# [sg.Radio('blur', 'Radio', size=(10, 1), key='blur'),
# sg.Slider((1, 11), 1, 1, orientation='h', size=(40, 15), key='blur_slider')],
# [sg.Radio('hue', 'Radio', size=(10, 1), key='hue'),
# sg.Slider((0, 225), 0, 1, orientation='h', size=(40, 15), key='hue_slider')],
# [sg.Radio('enhance', 'Radio', size=(10, 1), key='enhance'),
# sg.Slider((1, 255), 128, 1, orientation='h', size=(40, 15), key='enhance_slider')],
# [sg.Button('Exit', size=(10, 1))]
]
# 窗口设计
window = sg.Window('OpenCV实时图像处理',
layout,
location=(800, 400),
finalize=True)
# 打开内置摄像头
cap = cv2.VideoCapture(0)
while True:
event, values = window.read(timeout=0, timeout_key='timeout')
# 实时读取图像
ret, frame = cap.read()
# 在这改变图像
frame=cv2.medianBlur(frame,3)
# GUI实时更新
imgbytes = cv2.imencode('.png', frame)[1].tobytes()
window['image'].update(data=imgbytes)
# key_pressed = cv2.waitKey(60)
# if key_pressed == 27:
# break
# cap.release()
# cv2.destoryAllWindows()
window.close()