1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| import cv2 from pyzbar import pyzbar
camera=cv2.VideoCapture(0) camera.set(3,1280) camera.set(4,768) while True: (grabbed,frame)=camera.read() h1,w1= frame.shape[0],frame.shape[1] dst = frame text = pyzbar.decode(dst) for texts in text: textdate = texts.data.decode('utf-8') print(textdate) (x, y, w, h) = texts.rect print('识别内容:'+textdate) cx = int(x + w / 2) cy = int(y + h / 2) cv2.circle(dst, (cx, cy), 2, (0, 255, 0), 8) print('中间点坐标:',cx,cy) coordinate=(cx,cy) cv2.putText(dst,'QRcode_location'+str(coordinate),(20,20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) cv2.line(dst, (cx,cy),(int(w1/2),int(h1/2)), (255, 0, 0), 2) cv2.line(dst, texts.polygon[0], texts.polygon[1], (255, 0, 0), 2) cv2.line(dst, texts.polygon[1], texts.polygon[2], (255, 0, 0), 2) cv2.line(dst, texts.polygon[2], texts.polygon[3], (255, 0, 0), 2) cv2.line(dst, texts.polygon[3], texts.polygon[0], (255, 0, 0), 2) txt = '(' + texts.type + ') ' + textdate cv2.putText(dst, txt, (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 50, 255), 2) cv2.imshow('dst',dst) if cv2.waitKey(1) & 0xFF == ord('q'): cv2.imwrite("./frame.jpg", frame) break camera.release() cv2.destroyAllWindows()
|