Advertisement
musman2015

Untitled

Jul 26th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. """
  2. Created on Tue Oct 7 11:41:42 2018
  3.  
  4. @author: Caihao.Cui
  5. """
  6. from __future__ import print_function
  7.  
  8. import pyzbar.pyzbar as pyzbar
  9. import numpy as np
  10. import cv2
  11. import time
  12.  
  13. # get the webcam:
  14. cap = cv2.VideoCapture(0)
  15.  
  16. cap.set(3,640)
  17. cap.set(4,480)
  18. #160.0 x 120.0
  19. #176.0 x 144.0
  20. #320.0 x 240.0
  21. #352.0 x 288.0
  22. #640.0 x 480.0
  23. #1024.0 x 768.0
  24. #1280.0 x 1024.0
  25. time.sleep(2)
  26.  
  27. def decode(im) :
  28. # Find barcodes and QR codes
  29. decodedObjects = pyzbar.decode(im)
  30. # Print results
  31. for obj in decodedObjects:
  32. print('Type : ', obj.type)
  33. print('Data : ', obj.data,'\n')
  34. return decodedObjects
  35.  
  36.  
  37. font = cv2.FONT_HERSHEY_SIMPLEX
  38.  
  39. while(cap.isOpened()):
  40. # Capture frame-by-frame
  41. ret, frame = cap.read()
  42. # Our operations on the frame come here
  43. im = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  44.  
  45. decodedObjects = decode(im)
  46.  
  47. for decodedObject in decodedObjects:
  48. points = decodedObject.polygon
  49.  
  50. # If the points do not form a quad, find convex hull
  51. if len(points) > 4 :
  52. hull = cv2.convexHull(np.array([point for point in points], dtype=np.float32))
  53. hull = list(map(tuple, np.squeeze(hull)))
  54. else :
  55. hull = points;
  56.  
  57. # Number of points in the convex hull
  58. n = len(hull)
  59. # Draw the convext hull
  60. for j in range(0,n):
  61. cv2.line(frame, hull[j], hull[ (j+1) % n], (255,0,0), 3)
  62.  
  63. x = decodedObject.rect.left
  64. y = decodedObject.rect.top
  65.  
  66. print(x, y)
  67.  
  68. print('Type : ', decodedObject.type)
  69. print('Data : ', decodedObject.data,'\n')
  70.  
  71. barCode = str(decodedObject.data)
  72. cv2.putText(frame, barCode, (x, y), font, 1, (0,255,255), 2, cv2.LINE_AA)
  73.  
  74. # Display the resulting frame
  75. cv2.imshow('frame',frame)
  76. key = cv2.waitKey(1)
  77. if key & 0xFF == ord('q'):
  78. break
  79. elif key & 0xFF == ord('s'): # wait for 's' key to save
  80. cv2.imwrite('Capture.png', frame)
  81.  
  82. # When everything done, release the capture
  83. cap.release()
  84. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement