Advertisement
Guest User

Untitled

a guest
Apr 29th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.93 KB | None | 0 0
  1. import RPi.GPIO as gp
  2. import os
  3. import sys
  4. import pathlib
  5. import datetime
  6. import time
  7. from mysql.connector import (connection)
  8.  
  9. datasetName = ""
  10. path = "/mnt/storage/ddr/DDR"
  11. tags = []
  12. imagesNames = []
  13.  
  14. gp.setwarnings(False)
  15. gp.setmode(gp.BOARD)
  16.  
  17. gp.setup(7, gp.OUT)
  18. gp.setup(11, gp.OUT)
  19. gp.setup(12, gp.OUT)
  20. gp.setup(15, gp.OUT)
  21. gp.setup(16, gp.OUT)
  22. gp.setup(21, gp.OUT)
  23. gp.setup(22, gp.OUT)
  24.  
  25. def cleanUp():
  26.     gp.output(7, False)
  27.     gp.output(11, False)
  28.     gp.output(12, True)
  29.  
  30. def selectCamOne():
  31.     gp.output(7, False)
  32.     gp.output(11, False)
  33.     gp.output(12, True)  
  34.  
  35. def capture(imagesDir ,name,):
  36.     ts = time.time()
  37.     ts = int(ts)
  38.     imageName = "%s_%s.jpg" % (str(name), str(ts))
  39.     path = os.path.join(imagesDir, imageName)
  40.     cmd = "raspistill  -sh 100 -sa 90 -o "  + path
  41.     done = os.system(cmd)
  42.     return imageName
  43.  
  44. def takeSnapshot(cam, name):
  45.     selectCamOne()
  46.     return capture(cam, name)
  47.  
  48. def createTagsFile(fullPath, name ,tags):
  49.     path = os.path.dirname(fullPath)
  50.     filename = os.path.join(path, '{}_tags.txt'.format(name))
  51.     with open(filename, mode='wt', encoding='utf-8') as myfile:
  52.         myfile.write('\n'.join(tags))
  53.  
  54. def createImagesFile(fullPath, name, images):
  55.     path = os.path.dirname(fullPath)
  56.     filename = os.path.join(path, '{}_images.txt'.format(name))
  57.     if os.path.exists(filename):
  58.         mode = 'a'
  59.     else:
  60.         mode = 'w'
  61.     with open(filename, mode=mode, encoding='utf-8') as myfile:
  62.         for image in images:
  63.             imagePath = os.path.join(fullPath, image)
  64.             myfile.write(imagePath + '\n')
  65.  
  66. def createFolders(path, datasetFolder):
  67.     now = datetime.datetime.now()
  68.     currDate = now.strftime("%Y_%m_%d")
  69.     datasetPath = os.path.join(path, datasetFolder, currDate)
  70.     pathlib.Path(datasetPath).mkdir(parents=True, exist_ok=True)
  71.     return datasetPath
  72.  
  73. def addTagsIfNotExists(cursor, tags):
  74.     query = "INSERT IGNORE INTO tags (name) VALUES ('{}')"  
  75.     for tag in tags:
  76.         cursor.execute(query.format(tag))
  77.  
  78. def addDatasetIfNotExists(cursor, setName):
  79.     query = "INSERT IGNORE INTO sets (name) VALUES ('{}')".format(setName)
  80.     cursor.execute(query)
  81.  
  82. def addTagsDatasetMap(cursor, setName, tags):
  83.     query = "SELECT id FROM sets WHERE name = '{}'".format(setName)
  84.     cursor.execute(query)
  85.     id = cursor.fetchone()
  86.     print(id)
  87.     for tag in tags:
  88.         tagIdQuery = "SELECT id FROM tags WHERE name = '{}'".format(tag)
  89.         cursor.execute(tagIdQuery)
  90.         tagId = cursor.fetchone()
  91.         insertQuery = "INSERT IGNORE INTO sets_tags_map (set_id, tag_id) VALUES ({}, {})".format(id[0], tagId[0])
  92.         cursor.execute(insertQuery)
  93. if __name__ == "__main__":
  94.  
  95.     cnx = connection.MySQLConnection(user='root', password='swnyF74W9b', host='192.168.1.153', database='datasets')
  96.  
  97.     mycursor = cnx.cursor()
  98.  
  99.     datasetName = input('podaj nazwę datasetu: ')
  100.     tag  = input("Wpisz 'q' aby zakończyć wpisywanie tagów. podaj taga: ")
  101.     while tag != 'q':
  102.         tags.append(tag)
  103.         tag  = input("Wpisz 'q' aby zakończyć wpisywanie tagów. podaj taga: ")
  104.  
  105.     print( "Czy chcesz utwożyć dataset {} w folderze {} z tagami: {} ?".format(datasetName, path, tags))
  106.     create  = input("Tak / Nie ?")
  107.     if (create == "Tak" or create == "tak" or create == "t" or create == "T"):
  108.         addTagsIfNotExists(mycursor, tags)
  109.         addDatasetIfNotExists(mycursor, datasetName)
  110.         addTagsDatasetMap(mycursor, datasetName, tags)
  111.         cnx.commit()
  112.  
  113.         fullPath = createFolders(path, datasetName)
  114.         createTagsFile(fullPath, datasetName, tags)
  115.         action  = input("Wpisz 'q' aby zakończyć, x aby zrobić zdjęcie ")
  116.         while action != 'q':
  117.             imagesNames.append( capture(fullPath, datasetName) )
  118.             action  = input("Wpisz 'q' aby zakończyć, x aby zrobić zdjęcie ")
  119.  
  120.         createImagesFile(fullPath, datasetName, imagesNames)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement