Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sqlite3
  4. from sqlite3 import Error
  5. import cv2
  6. import numpy as np
  7.  
  8.  
  9. def create_connection(db_file):
  10.     """ create a database connection to the SQLite database
  11.        specified by the db_file
  12.    :param db_file: database file
  13.    :return: Connection object or None
  14.    """
  15.     try:
  16.         conn = sqlite3.connect(db_file)
  17.         return conn
  18.     except Error as e:
  19.         print(e)
  20.  
  21.     return None
  22.  
  23.  
  24. def select_all_tasks(conn):
  25.     """
  26.    Query all rows in the tasks table
  27.    :param conn: the Connection object
  28.    :return:
  29.    """
  30.     cur = conn.cursor()
  31.     cur.execute("SELECT * FROM ImageData WHERE ImageId >= 1 AND ImageId <= 100")
  32.  
  33.     rows = cur.fetchall()
  34.  
  35.     for row in rows:
  36.         print(row)
  37.  
  38.  
  39. def select_task_by_priority(conn, priority):
  40.     """
  41.    Query tasks by priority
  42.    :param conn: the Connection object
  43.    :param priority:
  44.    :return:
  45.    """
  46.     cur = conn.cursor()
  47.     cur.execute("SELECT * FROM tasks WHERE priority=?", (priority,))
  48.  
  49.     rows = cur.fetchall()
  50.  
  51.     for row in rows:
  52.         print(row)
  53.  
  54. def update_task(conn, task):
  55.     """
  56.    update priority, begin_date, and end date of a task
  57.    :param conn:
  58.    :param task:
  59.    :return: project id
  60.    """
  61.     sql = '''UPDATE Images SET CellInfo = ? WHERE ImageId = ?'''
  62.     cur = conn.cursor()
  63.     cur.execute(sql, task)
  64.     conn.commit()
  65.  
  66. def read_image_from_sqlite(conn, task):
  67.     cur = conn.cursor()
  68.     cur.execute("SELECT Content from ImageData where ImageId = 1")
  69.     rows = cur.fetchall()
  70.     for item in rows:
  71.  
  72.         #--- Decode blob
  73.         print("lenght of item")
  74.         print(type(item[0]))
  75.  
  76.         nparr  = np.fromstring(item[0], np.uint8)
  77.         img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
  78.  
  79.         #--- Display image
  80.         cv2.imshow('image',img_np)
  81.         k = cv2.waitKey(0)
  82.         if k == 27:         # wait for ESC key to exit
  83.             cv2.destroyAllWindows()
  84.             break
  85.  
  86. def main():
  87.     sqlite_db_path = "E:/Demo Timelapse/MTL-DEMO-0F9F-25DA/FP00.sqlite"
  88.     database = sqlite_db_path
  89.  
  90.     # create a database connection
  91.     conn = create_connection(database)
  92.     with conn:
  93.  #       print("1. Query task by priority:")
  94.  #       select_task_by_priority(conn,1)
  95.  
  96.         print("2. Query all tasks")
  97.         #select_all_tasks(conn)
  98.    
  99.   #  update_task(conn,(5,2))
  100.         read_image_from_sqlite(conn, "")
  101.  
  102.  
  103. if __name__ == '__main__':
  104.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement