Advertisement
NaMLiM

getUdpImage.py

Jul 25th, 2023
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.79 KB | Source Code | 0 0
  1. # Written by Brian Ejike (2017)
  2. # Distributed under the MIT License
  3.  
  4. import serial, time, socket
  5.  
  6. WIDTH = 256
  7. HEIGHT = 288
  8. READ_LEN = int(WIDTH * HEIGHT / 2)
  9.  
  10. DEPTH = 8
  11. HEADER_SZ = 54
  12.  
  13. portSettings = ["", 0]
  14.  
  15.  
  16. print("----------Extract Fingerprint Image------------")
  17. print()
  18.  
  19.  
  20. # assemble bmp header for a grayscale image
  21. def assembleHeader(width, height, depth, cTable=False):
  22.     header = bytearray(HEADER_SZ)
  23.     header[0:2] = b"BM"  # bmp signature
  24.     byte_width = int((depth * width + 31) / 32) * 4
  25.     if cTable:
  26.         header[2:6] = ((byte_width * height) + (2**depth) * 4 + HEADER_SZ).to_bytes(
  27.             4, byteorder="little"
  28.         )  # file size
  29.     else:
  30.         header[2:6] = ((byte_width * height) + HEADER_SZ).to_bytes(
  31.             4, byteorder="little"
  32.         )  # file size
  33.     # header[6:10] = (0).to_bytes(4, byteorder='little')
  34.     if cTable:
  35.         header[10:14] = ((2**depth) * 4 + HEADER_SZ).to_bytes(
  36.             4, byteorder="little"
  37.         )  # offset
  38.     else:
  39.         header[10:14] = (HEADER_SZ).to_bytes(4, byteorder="little")  # offset
  40.  
  41.     header[14:18] = (40).to_bytes(4, byteorder="little")  # file header size
  42.     header[18:22] = width.to_bytes(4, byteorder="little")  # width
  43.     header[22:26] = (-height).to_bytes(4, byteorder="little", signed=True)  # height
  44.     header[26:28] = (1).to_bytes(2, byteorder="little")  # no of planes
  45.     header[28:30] = depth.to_bytes(2, byteorder="little")  # depth
  46.     # header[30:34] = (0).to_bytes(4, byteorder='little')
  47.     header[34:38] = (byte_width * height).to_bytes(4, byteorder="little")  # image size
  48.     header[38:42] = (1).to_bytes(4, byteorder="little")  # resolution
  49.     header[42:46] = (1).to_bytes(4, byteorder="little")
  50.     # header[46:50] = (0).to_bytes(4, byteorder='little')
  51.     # header[50:54] = (0).to_bytes(4, byteorder='little')
  52.     return header
  53.  
  54.  
  55. def options():
  56.     print("Options:")
  57.     print("\tPress 1 to enter serial port settings")
  58.     print("\tPress 2 to scan a fingerprint and save the image")
  59.     print("\tPress 3 to view help")
  60.     print("\tPress 4 to exit")
  61.     print()
  62.     choice = input(">> ")
  63.     print()
  64.     return choice
  65.  
  66.  
  67. # def getSettings():
  68. #     portSettings[0] = input("Enter Arduino serial port number: ")
  69. #     portSettings[1] = int(input("Enter serial port baud rate: "))
  70. #     print()
  71.  
  72.  
  73. def getPrint():
  74.     """
  75.    First enter the port settings with menu option 1:
  76.    >>> Enter Arduino serial port number: COM13
  77.    >>> Enter serial port baud rate: 57600
  78.  
  79.    Then enter the filename of the image with menu option 2:
  80.    >>> Enter filename/path of output file (without extension): myprints
  81.    Found fingerprint sensor!
  82.    .
  83.    .
  84.    .
  85.    (Here you communicate with the Arduino and follow instructions)
  86.    .
  87.    .
  88.    .
  89.    Extracting image...saved as <filename>.bmp
  90.  
  91.    """
  92.     out = open(
  93.         input("Enter filename/path of output file (without extension): ") + ".bmp", "wb"
  94.     )
  95.     # assemble and write the BMP header to the file
  96.     out.write(assembleHeader(WIDTH, HEIGHT, DEPTH, True))
  97.     try:
  98.         sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  99.         sock.bind(("192.168.1.9", 9999))
  100.     except socket.error:
  101.         print("Error socket")
  102.     for i in range(256):
  103.         # write the colour palette
  104.         out.write(i.to_bytes(1, byteorder="little") * 4)
  105.     while True:
  106.         try:
  107.             data, addr = sock.recvfrom(4096)
  108.             print(data, end="")
  109.             for i in range(READ_LEN):  # start recving image
  110.                 # if we get nothing after the 1 sec timeout period
  111.                 if not data:
  112.                     print("Timeout!")
  113.                     out.close()  # close port and file
  114.                     return False
  115.                 # make each nibble a high nibble
  116.                 out.write((data[0] & 0xF0).to_bytes(1, byteorder="little"))
  117.                 out.write(((data[0] & 0x0F) << 4).to_bytes(1, byteorder="little"))
  118.  
  119.             out.close()
  120.             sock.close()  # close file
  121.             print("Image saved as", out.name)
  122.             return True
  123.         except Exception as e:
  124.             print("Read failed: ", e)
  125.             out.close()
  126.             sock.close()
  127.             return False
  128.         except KeyboardInterrupt:
  129.             print("Closing port.")
  130.             out.close()
  131.             sock.close()
  132.             return False
  133.  
  134.  
  135. while True:
  136.     chose = options()
  137.     if chose == "4":
  138.         break
  139.     # elif chose == "1":
  140.     #     getSettings()
  141.     elif chose == "2":
  142.         res = getPrint()
  143.         if not res:
  144.             print("Image extraction failed!")
  145.         continue
  146.     elif chose == "3":
  147.         print("================= HELP ==================")
  148.         print(getPrint.__doc__)
  149.         print("=========================================")
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement